Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 弹跳球游戏tkinter帆布_Python_Animation_Canvas_Tkinter_Game Physics - Fatal编程技术网

Python 弹跳球游戏tkinter帆布

Python 弹跳球游戏tkinter帆布,python,animation,canvas,tkinter,game-physics,Python,Animation,Canvas,Tkinter,Game Physics,我用python编写了一个游戏,其中的目标是将球弹离平台。 一切都很好,但平台的运动不是那么平稳。你能帮我把站台移动得更平稳些吗?如果代码不太清楚,我很抱歉,但我是python新手 import tkinter as tk import random root = tk.Tk() width = 900 height = 500 canvas = tk.Canvas(root, bg='white', width=width, height=height) canvas.pack() x

我用python编写了一个游戏,其中的目标是将球弹离平台。 一切都很好,但平台的运动不是那么平稳。你能帮我把站台移动得更平稳些吗?如果代码不太清楚,我很抱歉,但我是python新手

import tkinter as tk
import random

root = tk.Tk()

width = 900
height = 500

canvas = tk.Canvas(root, bg='white', width=width, height=height)
canvas.pack()

x = random.randrange(700)

ball = canvas.create_oval(x+10, 10, x+50, 50, fill='green')

platform_y = height - 20
platform = canvas.create_rectangle(width//2-50, platform_y, width//2+50, platform_y+10, fill='black')

xspeed = 2
yspeed = 2
skore = 0
body = 0

def move_ball():
  global xspeed
  global yspeed
  x1, y1, x2, y2 = canvas.coords(ball)
  if x1 <= 0 or x2 >= width:
    xspeed = -xspeed
  if y1 <= 0:
    yspeed = 10
  elif y2 == platform_y: 
    cx = (x1 + x2) // 2
    px1, _, px2, _ = canvas.coords(platform)
    if px1 <= cx <= px2:
      yspeed = -10
    else:
      canvas.create_text(width//2, height//2, text='Game Over', font=('Arial Bold', 32), fill='red')
      return
  canvas.move(ball, xspeed, yspeed)
  canvas.after(20, move_ball)

def board_right(event):
  x1, y1, x2, y2 = canvas.coords(platform) 
  if x2 < width:
    dx = min(width-x2, 10)
    canvas.move(platform, dx, 0)

def board_left(event):
  x1, y1, x2, y2 = canvas.coords(platform)
  if x1 > 0:
    dx = min(x1, 10)
    canvas.move(platform, -dx, 0)

canvas.bind_all('<Right>', board_right)
canvas.bind_all('<Left>', board_left)

move_ball()

root.mainloop()
将tkinter作为tk导入
随机输入
root=tk.tk()
宽度=900
高度=500
canvas=tk.canvas(根,bg='white',宽度=宽度,高度=高度)
canvas.pack()
x=随机。随机范围(700)
球=画布。创建椭圆(x+10,10,x+50,50,填充为绿色)
平台y=高度-20
平台=画布。创建矩形(宽度//2-50,平台y,宽度//2+50,平台y+10,填充为黑色)
xspeed=2
Y速度=2
skore=0
body=0
def移动球():
全局X速度
全球速度
x1,y1,x2,y2=画布坐标(球)
如果x1=宽度:
xspeed=-xspeed

如果y1问题在于平台的速度取决于键盘的自动重复速度

不要为每个
事件移动一次,而是使用按键开始平台向所需方向移动,释放按键停止平台移动。然后,在
之后使用
,沿给定方向重复移动平台

例如:

after_id = None
def platform_move(direction):
    """
    direction should be -1 to move left, +1 to move right,
    or 0 to stop moving
    """
    global after_id
    speed = 10
    if direction == 0:
        canvas.after_cancel(after_id)
        after_id = None
    else:
        canvas.move(platform, direction*speed, 0)
        after_id = canvas.after(5, platform_move, direction)

canvas.bind_all("<KeyPress-Right>", lambda event: platform_move(1))
canvas.bind_all("<KeyRelease-Right>", lambda event: platform_move(0))
canvas.bind_all("<KeyPress-Left>", lambda event: platform_move(-1))
canvas.bind_all("<KeyRelease-Left>", lambda event: platform_move(0))
after\u id=None
def平台_移动(方向):
"""
向左移动方向应为-1,向右移动方向应为+1,
或0停止移动
"""
全局后处理id
速度=10
如果方向==0:
canvas.after\u取消(在\u id之后)
在\u id=无之后
其他:
画布移动(平台,方向*速度,0)
after_id=画布。after(5,平台移动,方向)
canvas.bind_all(“,lambda事件:平台移动(1))
canvas.bind_all(“,lambda事件:平台_移动(0))
canvas.bind_all(“,lambda事件:平台移动(-1))
canvas.bind_all(“,lambda事件:平台_移动(0))

上面的代码不能处理您可能同时按下两个键的情况,但是可以使用一些额外的逻辑来处理。重点是演示如何使用关键点启动和停止动画

如果你想要压制运动,那么你需要减少赛后时间,比如说10毫秒,然后还要将球移动的距离减少一半。这将提高平滑度。因此,我们的想法是更频繁地移动更短的距离。这是我所知道的提高运动平稳性的唯一方法。但是,当我按住向右或向左箭头时,它会快速移动,不会停止。您可以使用
speed
变量将其加速或减速,同时在
之后更改提供给
5
。至于不停下来,那只是检查坐标并在到达边缘时让它停下来的问题。重点是显示您可以设置动画循环,然后让事件停止并启动动画,而不是让事件成为动画。