如何使物体从画布边框反弹? [英] How do I have an object rebound off the canvas border?

查看:87
本文介绍了如何使物体从画布边框反弹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tkinter的canvas小部件创建一个椭圆,并使其在画布中四处移动。

I am using the canvas widget from tkinter to create an ellipse and have it move around in the canvas.

但是,当椭圆与边界接触时,它会粘在墙上而不是弹起。

However when the ellipse comes in contact with the border it gets stuck to wall instead of bouncing off.

我正在努力调试代码,谢谢!!

I'm struggling with debugging the code, thanks in advance!

from tkinter import *
from time import *
import numpy as np
root = Tk()
root.wm_title("Bouncing Ball")
canvas = Canvas(root, width=400, height=400, bg="black")
canvas.grid()
size=10
x = 50
y = 50
myBall = canvas.create_oval(x-size, y-size, x+size, y+size, fill = "red")
while True:
    root.update()
    root.after(50)
    dx = 5
    dy = 0
#separating x and y cooridnates from tuple of canvas.coords
    x = canvas.coords(myBall)[0]+10
    y = canvas.coords(myBall)[1]+10
    coordinates = np.array([x, y], dtype = int)
#Checking boundaries
    if coordinates[0]-size <= 0:
        dx = -1*dx
    if coordinates[0]+size >= 400:
        dx = -1*dx
    if coordinates[1]-size <= 0:
        dy = -1*dy
    if coordinates[1]+size >= 400:
        dy = -1*dy
    print(coordinates) #Used to see what coordinates are doing
    canvas.move(myBall, dx, dy) #Move ball by dx and dy


推荐答案

这是组织弹跳球程序并让您开始使用GUI的简单方法编程:

Here is a simple way to organize your bouncing ball program, and get you started with GUI programming:


  • 虽然循环不能与GUI主循环配合使用;也不必调用 update mainloop 会处理该更新。

重复操作最好是 root.after 的句柄。

Repeated actions are best handles with root.after.

我在函数 bounce 中提取了弹跳逻辑,该函数使用进行自我调用root.after 。您会看到我简化了逻辑。

I extracted the bounce logic inside a function bounce that calls itself using root.after. You will see that I simplified the logic.

我还参数化了画布大小。

I also parametrized the canvas size.

初始速度分量 dx dy 是从可能值的列表中随机选择的,因此游戏不太可能

The initial speed components dx and dy are randomly chosen from a list of possible values so the game is not too boring.

外观如下:

import tkinter as tk   # <-- avoid star imports
import numpy as np
import random

WIDTH = 400
HEIGHT = 400
initial_speeds = [-6, -5, -4, 4, 5, 6]
dx, dy = 0, 0
while dx == dy:
    dx, dy = random.choice(initial_speeds), random.choice(initial_speeds) 

def bounce():
    global dx, dy
    x0, y0, x1, y1 = canvas.coords(my_ball)
    if x0 <= 0 or x1 >= WIDTH:    # compare to left of ball bounding box on the left wall, and to the right on the right wall
        dx = -dx
    if y0 <= 0 or y1 >= HEIGHT:   # same for top and bottom walls
        dy = -dy
    canvas.move(my_ball, dx, dy)
    root.after(50, bounce)

if __name__ == '__main__':

    root = tk.Tk()
    root.wm_title("Bouncing Ball")
    canvas = tk.Canvas(root, width=400, height=400, bg="black")
    canvas.pack(expand=True, fill=tk.BOTH)

    size=10
    x = 50
    y = 50
    my_ball = canvas.create_oval(x-size, y-size, x+size, y+size, fill="red")

    bounce()
    root.mainloop()

这篇关于如何使物体从画布边框反弹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆