简体   繁体   English

在屏幕上弹起球

[英]Making a Ball Bounce on the Screen

I'm in the process of trying to make a very simple ball and paddle game in Python and have the ball moving on the screen with a paddle that moves from left to right. 我正在尝试用Python创建一个非常简单的球和桨游戏,并用一个从左向右移动的桨在屏幕上移动球。 But, the ball moves and hits the bottom of the screen and doesn't change direction, it just stops and slides against the wall. 但是,球会移动并撞击屏幕的底部,并且不会改变方向,它只会停下来并在墙上滑动。

Any help or suggestions would be appreciated. 任何帮助或建议,将不胜感激。 Here is the code: 这是代码:

from tkinter import Canvas, Tk
import random

canvas_width = 800
canvas_height = 400

root = Tk()
root.title("Ball and Paddle")
c = Canvas(root,width=canvas_width,height=canvas_height,background='orange')
c.pack()

paddle_color = 'blue'
paddle_width = 60
paddle_height = 10
paddle_start_x = canvas_width/2 - paddle_width/2
paddle_start_y = canvas_height - paddle_height - 20
paddle_start_x2 = canvas_width/2 + paddle_width/2
paddle_start_y2 = canvas_height - 20

paddle = c.create_rectangle(paddle_start_x,paddle_start_y,\
                        paddle_start_x2,paddle_start_y2,\
                        fill='blue', width=0)

ball_color = 'green'
ball_width = 20
ball_height = 20
ball_start_x1 = canvas_width/2 - ball_width/2
ball_start_y1 = canvas_height/2 - ball_height/2
ball_start_x2 = canvas_width/2 + ball_width/2
ball_start_y2 = canvas_height/2 + ball_height/2
ball_speed = 5

ball = c.create_oval(ball_start_x1, ball_start_y1, \
                 ball_start_x2, ball_start_y2, \
                 fill = ball_color, width = 0)
dx = 1
dy = 1

def ball_move():
    global dx, dy
    (bx1,by1,bx2,by2) = c.coords(ball)
    c.move(ball, dx, dy)
    if bx1 <= 0:
    dx = -dx
    if bx2 >= canvas_width:
        dx = -dx
    if by1 <= 0:
        dy = -dy
    if by2 >= canvas_height:
        dy = -dy
    root.after(ball_speed, ball_move)

def move_left(event):
    (x1,y1,x2,y2) = c.coords(paddle)
    if x1>0:
        c.move(paddle,-20,0)

def move_right(event):
    (x1,y1,x2,y2) = c.coords(paddle)
    if x2 < canvas_width:
        c.move(paddle, 20, 0)

c.bind('<Left>',move_left)
c.bind('<Right>',move_right)
c.focus_set()

root.after(ball_speed, ball_move)

root.mainloop()

(I'm assuming that the if bx1 <= 0: indentation problem isn't actually present in your real code, and was just a transcription error when writing this post) (我假设if bx1 <= 0:缩进问题实际上没有出现在您的真实代码中,并且在撰写本文时只是一个转录错误)

This occurs because the ball is getting "lodged" into the border of the screen, when by2 has a value of 401. You reverse dy, which makes it move up to 400 at the next frame. 发生这种情况的原因是,当by2的值为401时,球正“滞留”在屏幕的边框中。反转dy,使其在下一帧上移至400。 But then you reverse dy again , so it moves back down to 401. 但是然后您再次反转dy,因此它又移回401。

This happens because you're creating the b* variables, then moving the ball, then doing boundary checking on the b* variables. 发生这种情况是因为您正在创建b *变量,然后移动球,然后对b *变量进行边界检查。 You're effectively inspecting the position of the ball from the previous frame. 您正在有效地检查前一帧中球的位置。

Try moving the move call to after the boundary check. 尝试在边界检查之后将move调用移至。

def ball_move():
    global dx, dy
    (bx1,by1,bx2,by2) = c.coords(ball)
    if bx1 <= 0:
        dx = -dx
    if bx2 >= canvas_width:
        dx = -dx
    if by1 <= 0:
        dy = -dy
    if by2 >= canvas_height:
        dy = -dy
    c.move(ball, dx, dy)
    root.after(ball_speed, ball_move)

Now your ball should bounce. 现在您的球应该反弹了。

在此处输入图片说明

Indentation issue: 缩进问题:

def ball_move():
    global dx, dy
    (bx1,by1,bx2,by2) = c.coords(ball)
    c.move(ball, dx, dy)
    if bx1 <= 0:
    dx = -dx
    if bx2 >= canvas_width:
    ..snippet..

should be: 应该:

def ball_move():
    global dx, dy
    (bx1,by1,bx2,by2) = c.coords(ball)
    c.move(ball, dx, dy)
    if bx1 <= 0:
        dx = -dx              # indent corrected here!
    if bx2 >= canvas_width:
    ..snippet..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM