我有一个游戏,我让世界向你移动,你可以同时跳跃。这两个功能分开工作,但它们在一起使用时不起作用。有时它会给我一个错误(ValueError:无法将字符串转换为float:expected)无法转换,有时字符会产生奇怪的抖动,有时它会同时发生,有时它会崩溃。这是我的代码
import Tkinter as tkinter
import thread
from time import sleep
def is_even(n):
if n % 2 == 0:
retVal = True
else:
retVal = False
return retVal
class gameScreen:
def move(self, event):
d = self.getdir(event.keysym)
self.canvas.move(self.char, d[0], d[1])
self.canvas.update()
if self.collision():
self.canvas.move(self.char, -d[0], -d[1])
def fall(self):
speed = .01
x = 0
while not self.collision():
self.canvas.move(self.char, 0, 1)
self.canvas.update()
sleep(speed)
if x == 2:
speed -= speed *.04
x = 0
else:
x += 1
self.canvas.move(self.char, 0, -1)
def jump(self):
j = 0
from time import sleep
jump = [6, 6, 6, 6, 4, 4, 2, 1, 0]
for i in range(0, (len(jump)*2)):
x = jump[j]
self.canvas.move(self.char, 0, -x)
if not is_even(i):
j +=1
self.canvas.update()
sleep(.05)
self.fall()
def getLast(self, lvl):
x = 0
for i in lvl:
coords = self.canvas.coords(i)
if x < coords[3]:
x = coords[3]
last = i
return last
def gameThread(self, event):
thread.start_new_thread(self.gameStart, ())
def gameStart(self, event=None):
from time import sleep
last = self.getLast(self.lvl1)
coords = self.canvas.coords(last)
while coords[2] > 0:
print coords[2]
for i in self.lvl1:
self.canvas.move(i, -1, 0)
coords = self.canvas.coords(last)
self.canvas.update()
sleep(.002)
if not self.touchingGround(event):
self.fall()
def touchingGround(self, event = None):
self.canvas.move(self.char, 0, 5)
if self.collision():
retVal = True
else:
retVal = False
self.canvas.move(self.char, 0, -5)
return retVal
def onKey(self, event):
if self.touchingGround(event):
thread.start_new_thread(self.jump, ())
def collision(self):
coords = self.canvas.coords(self.char)
collision = len(self.canvas.find_overlapping(coords[0]-1,
coords[1]-1,
coords[2]-1,
coords[3]-1)) >=2
return collision
def getdir(self, s):
direction = {"Left":[-5, 0], "Right":[5, 0]}
try:
retVal = direction[s]
except KeyError:
retVal =False
return retVal
def __init__(self, master):
self.lvl1 = []
self.objects = []
self.master = master
master.attributes("-fullscreen", True)
master.title("Game")
self.width=master.winfo_screenwidth()
self.height=master.winfo_screenheight()
self.canvas = tkinter.Canvas(master, width=self.width,
height=self.height)
self.canvas.pack(expand="YES",fill="both")
self.objects.append(self.canvas.create_rectangle(0, self.height,
self.width,
self.height-100,
fill="grey"))
self.lvl1.append(self.canvas.create_rectangle(self.width,
self.height-100,
self.width + 100,
self.height-150,
fill="grey"))
self.objects.extend(self.lvl1)
self.char = self.canvas.create_rectangle((self.width/2)-25,
self.height- 100,
(self.width/2)+25,
self.height-150,
fill="red")
self.x = 0
self.y = 0
master.bind("<Key-Up>", self.onKey)
master.bind("<Return>", self.onKey)
def destroy(event):
root.destroy()
root = tkinter.Tk()
my_gui = gameScreen(root)
root.bind("<Escape>", destroy)
root.mainloop()
我尝试添加像tadhg所说的后来,但它仍然只有时跳,并且大多数时候它完全跳跃,或者只是上升一到两个像素,然后再回落,然后重复该数量应该跳的时间。