为什么球在画布中的速度会变化

问题描述 投票:0回答:1

所以我在canvas python上制作打砖块游戏,我能够创建一个移动的球和一个移动的 栏以及球与栏的碰撞,现在我以这样的方式编写代码:如果球移动 到画布底部,分数会扣除,球会被删除并再次出现 在运行游戏时之前的初始位置,速度是固定的 对于球来说,但在它落到画布上后的另一种外观上,它的速度会发生变化 或者你可以说它的速度增加并给出列表的索引错误,我无法理解 错误或球的行为方式,我在下面发布代码,请查看它 我还需要帮助来使球的运动更加一致,就像在实际游戏中一样

代码:

import tkinter as tk
from tkinter import *
import time

score = 0
x_velocity = 30
y_velocity = 20

def left(event):

    if canvas.coords(paddle)[0] < 0:
        canvas.move(paddle, 0,0)
    else:
        canvas.move(paddle, -10,0)

def right(event):

    if canvas.coords(paddle)[2] > 800:
        canvas.move(paddle, 0, 0)
    else:
        canvas.move(paddle, 10,0)

def ball_formation():

    global ball
    ball = canvas.create_oval(150,150,170,170, fill = 'white')
    window.update()
    window.after(300,ball_motion())

def ball_motion():

    global score

    if canvas.coords(ball)[0] <= 0 or canvas.coords(ball)[2] >= 800:
        global x_velocity
        x_velocity = - x_velocity

    elif canvas.coords(ball)[1] <= 0:
        global y_velocity
        y_velocity = - y_velocity

    elif canvas.coords(ball)[1] >= 500: # this is where the ball gets 
                                        # made again
                                        # and somehow its velocity 
                                        # changes
        if score > 200 :
            score-= 200

        else:
            score = 0

        score_label.config(text = 'Score : {}'.format(score))

        canvas.delete(ball)

        window.update()
        window.after(300,ball_formation())


    elif collision_of_ball_with_paddle() is True:
        score += 250
        score_label.config(text = 'Score : {}'.format(score))
    
    canvas.move(ball, x_velocity, y_velocity)
    window.after(300,ball_motion)       

def collision_of_ball_with_paddle():

    if canvas.coords(ball)[2] >= canvas.coords(paddle)[0] and canvas.coords(ball)[2] <= canvas.coords(paddle)[2] and canvas.coords(ball)[3] >= 395:                                    
        global y_velocity                  
        y_velocity = - y_velocity
        return True

    else:
        return False

window = tk.Tk()
window.title('Game')
window.geometry('900x600+135+0')

score_label = Label(window, font = ('consolas',40), text = 'Score : {}'.format(score))
score_label.pack(side = 'top')

canvas = Canvas(window, width = 800, height = 500, bg = 'black')
canvas.pack()

paddle = canvas.create_rectangle(10,400,110,405, fill = 'white')
window.bind('<a>',left)
window.bind('<d>',right)

ball_formation()

window.mainloop()

错误-

if canvas.coords(ball)[0] <= 0 or canvas.coords(ball)[2] >= 800:
   ~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
python canvas move
1个回答
0
投票

主要问题是您删除了

ball
但代码仍然运行
canvas.coords(ball)
这给出了空结果
但代码尝试获取
[0]
[2]
,这会引发错误。

删除后

ball
然后可以设置
ball = None

然后在尝试使用
if ball is not None
执行某些操作之前,请始终检查
ball

或者你可以使用其他变量,即。
ball_exists
并设置
True
(当您创建
ball
时)或
False
(当您删除
ball
时) 在尝试使用
if ball_exists
 之前,请务必检查 
ball

© www.soinside.com 2019 - 2024. All rights reserved.