【发布时间】:2021-11-27 00:33:14
【问题描述】:
我正在寻找这个圆圈从屏幕边缘反弹并反弹回画布中。我知道你需要输入坐标只需要一个公式,这样我就可以理解我在看什么。
import tkinter # Used to draw on canvas (2D plot)
class MyCircle:
def __init__(self, x, y, dx, dy, cnum): # "self" always 1st parameter in python methods
self.x = x
self.y = y
self.dx = dx # "delta-x" or "change in x" or "x velocity"
self.dy = dy
self.cnum = cnum # Add circle number as parameter for log
# delete the log files to retry
# https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
def logData(self):
# Filename uses circle number.
with open('logfile_' + str(self.cnum) + '.txt', 'a') as f:
f.write("x=" + str(self.x) +
" y=" + str(self.y) +
" dx=" + str(self.dx) +
" dy=" + str(self.dy) + '\n')
def MoveIt(self, coordinates=tkinter.Canvas):
self.x = self.x + self.dx
self.y = self.y + self.dy
def DrawIt(self, w):
w.create_arc(self.x, self.y, self.x + 20, self.y + 20, start=0, extent=359,
fill='red')
【问题讨论】:
-
除了球的初始位置之外,您还需要一个方向矢量和速度(可以组合),以及墙壁的位置。请参阅deflection 上的维基百科文章
-
代码已经有了位置 x 和 y 以及速度 dx 和 dy。 dx 和 dy 将包含方向信息。我错了吗?
-
感谢您的帮助。我刚刚进入编码的第一年,有很多信息需要处理和理解。不过,令人着迷的东西。