-2
我正在與tkinter進行遊戲,玩家必須躲避酸雨。 我差不多完成了它,我只是需要一種在按下Up鍵後重新啓動整個遊戲的方法。我已經做了一個雨後擊中玩家的功能。這個函數檢查一個事件是否按下了向上鍵,並觸發比重新啓動遊戲的代碼的重新啓動功能。所有內部的遊戲類。 下面的代碼:如何重新開始遊戲?(tkinter)
import random
import time
from winsound import *
from tkinter import *
class Game():
def __init__(self):
self.high_file = open('HIGHSCORE.txt','r')
self.high = self.high_file.read()
self.tk = Tk()
self.tk.title('DeadlyRain')
self.tk.resizable(0,1)
self.tk.wm_attributes('-topmost',1)
self.canvas =
Canvas(self.tk,width=400,height=500,highlightthickness=0)
self.canvas.pack()
self.tk.update()
self.bg = PhotoImage(file="bg.gif")
self.canvas.create_image(0,0,image=self.bg,anchor='nw')
self.score_txt = self.canvas.create_text(320,12,text='Score: 0', \
fill='white',font=('Fixedsys',17))
self.high_txt = self.canvas.create_text(310,30, \
text='HighScore: %s' % self.high,\
fill='white',font=('Fixedsys',16))
self.player_img = PhotoImage(file='player\\player0.gif')
self.player = self.canvas.create_image(150,396, \
image=self.player_img,
anchor='nw')
self.rain_img = [PhotoImage(file='rain\\rain1.gif'),
PhotoImage(file='rain\\rain2.gif'),
PhotoImage(file='rain\\rain3.gif')]
self.images_left = [
PhotoImage(file="player\\playerL1.gif"),
PhotoImage(file="player\\playerL2.gif"),
PhotoImage(file="player\\playerL3.gif")]
self.images_right = [
PhotoImage(file="player\\playerR1.gif"),
PhotoImage(file="player\\playerR2.gif"),
PhotoImage(file="player\\playerR3.gif")]
self.current_image = 0
self.current_image_add = 1
self.last_time = time.time()
self.new_high = False
self.player_x = 0
self.rain_drops = []
self.rain_count = 0
self.points = 0
self.speed = 2
self.list = list(range(0,9999))
self.needed_rain_count = 100
self.running = True
self.canvas.bind_all('<KeyPress-Left>',self.left)
self.canvas.bind_all('<KeyPress-Right>',self.right)
def animate(self):
if self.player_x != 0 :
if time.time() - self.last_time > 0.1:
self.last_time = time.time()
self.current_image += self.current_image_add
if self.current_image >= 2:
self.current_image_add = -1
if self.current_image <= 0:
self.current_image_add = 1
if self.player_x < 0:
self.canvas.itemconfig(self.player,
image=self.images_left[self.current_image])
elif self.player_x > 0:
self.canvas.itemconfig(self.player,
image=self.images_right[self.current_image])
def score(self):
self.points += 1
if self.points > int(self.high):
self.canvas.itemconfig(self.high_txt, \
text='HighScore: %s' % self.points)
self.new_high = True
self.canvas.itemconfig(self.score_txt, \
text='Score: %s' % self.points)
if self.points >= 20 and self.points <= 34:
self.needed_rain_count = 80
elif self.points >= 35 and self.points <= 49:
self.needed_rain_count = 60
elif self.points >= 50 and self.points <= 64:
self.needed_rain_count = 40
elif self.points >= 65 and self.points <= 79:
self.needed_rain_count = 20
def gameover(self):
self.running = False
self.canvas.create_text(200,250,text='GAMEOVER')
if self.new_high == True:
new_high = open('HIGHSCORE.txt','w')
new_high.write(str(self.points))
new_high.close()
self.high_file.close()
self.canvas.bind_all('<KeyPress-Up>',self.restart) #restart func
is called
self.high_file.close()
def restart(self,evt):
#insert code for restarting game here
def left(self,evt):
self.player_x = -2
def right(self,evt):
self.player_x = 2
def move(self):
self.animate()
self.canvas.move(self.player,self.player_x,0)
pos = self.canvas.coords(self.player)
if pos[0] <= -8:
self.player_x = 0
elif pos[0] >= self.canvas.winfo_width() - 40:
self.player_x = 0
def rain(self):
if self.rain_count == self.needed_rain_count:
for x in self.list:
x =self.canvas.create_image(random.randint(0,501), \
0,image=random.choice(self.rain_img))
self.rain_drops.append(x)
self.rain_count = 0
self.list.remove(x)
break
def rain_fall(self,id):
co = self.canvas.coords(id)
if co[1] > 500:
self.rain_drops.remove(id)
self.canvas.delete(id)
self.score()
self.canvas.move(id,0,self.speed)
def rain_hit(self,id):
rpos = self.canvas.coords(id)
ppos = self.canvas.coords(self.player)
if rpos[1] >= ppos[1] and rpos[1] <= ppos[1]+68:
if rpos[0] >= ppos[0] and rpos[0] <= ppos[0]+48:
self.gameover()
def mainloop(self):
PlaySound("sound\\rain.wav", SND_ASYNC)
while 1:
if self.running == True:
self.rain_count += 1
self.move()
self.rain()
for rain in self.rain_drops:
self.rain_hit(rain)
self.rain_fall(rain)
self.tk.update_idletasks()
self.tk.update()
time.sleep(0.01)
g = Game()
g.mainloop()
預先感謝您!
它只是創建一個新的空白窗口,並在我拋出這個錯誤: – Fichtenholzz
回溯(最近通話最後一個): 文件「C:\用戶\惠普Pavilion G6 \桌面\ python的\ DeadlyRain \ DeadlyRain.py」,行147, g =遊戲() 文件「C:\ Users \ HP Pavilion G6 \ Desktop \ python \ DeadlyRain \ DeadlyRain.py」,第20行,在__init__中 self.canvas.create_image(0,0,image = self.bg,錨= 'NW') 文件 「C:\ Python34 \ lib中\ Tkinter的\ __ init__.py」,線2332,在create_image 返回self._create( '圖像',指定參數時,千瓦) 文件「 C:\ Python34 \ lib \ tkinter \ __ init__.py「,第2323行,在_create *(args + self._options(cnf,kw)))) _tkinter.TclError:image「pyimage13」不存在 –
Fichtenholzz