为什么球的行为方式如此? [英] Why do the balls behave the way they do?
问题描述
我希望每个球都能独立移动.我认为问题与它们都具有相同的速度有关,但我不知道它们为什么会这样,或者这是否是问题所在.另外,为什么屏幕的右侧部分会以这种方式运行?我希望球能够在整个屏幕上正确移动.
I want each ball to move independently. I think the problem has something to do with them all having the same velocity, but I don't know why they do or if that's even the problem. Also, why does the right portion of the screen behave the way it does? I want the balls to be able to move all over the screen properly.
import sys
import pygame
import random
screen_size = (screen_x, screen_y) = (640, 480)
screen = pygame.display.set_mode(screen_size)
size = {"width": 10, "height": 10}
velocity = {"x": {"mag": random.randint(3,7), "dir": random.randrange(-1,2,2)}, "y": {"mag": random.randint(3,7), "dir": random.randrange(-1,2,2)}}
class Ball(object):
def __init__(self, size, position, velocity):
self.size = size
self.position = position
self.velocity = velocity
self.color = (255, 255, 255)
def update(self):
self.position["x"] += (self.velocity["x"]["mag"] * self.velocity["x"]["dir"])
self.position["y"] += (self.velocity["y"]["mag"] * self.velocity["y"]["dir"])
if self.position["x"] <= 0 or self.position["x"] >= screen_y:
self.velocity["x"]["dir"] *= -1
if self.position["y"] <= 0 or self.position["y"] >= screen_y:
self.velocity["y"]["dir"] *= -1
self.rect = pygame.Rect(self.position["x"], self.position["y"], size["width"], size["height"])
def display(self):
pygame.draw.rect(screen, self.color, self.rect)
def main():
pygame.init()
fps = 30
clock = pygame.time.Clock()
balls = []
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
(x, y) = event.pos
position = {"x": x, "y": y}
new_ball = Ball(size, position, velocity)
balls.append(new_ball)
for ball in balls:
ball.update()
screen.fill((0,0,0))
for ball in balls:
ball.display()
pygame.display.update()
clock.tick(fps)
if __name__ == "__main__":
main()
推荐答案
您的屏幕右侧问题是由于 update()
中这一行的拼写错误造成的:
Your problem with the right of the screen is due to a typo in this line in update()
:
if self.position["x"] <= 0 or self.position["x"] >= screen_y:
# ^ should be x
这可以防止您的 Ball
进入 screen
最右边的 640 - 480 == 160
像素.
This prevents your Ball
s from entering the rightmost 640 - 480 == 160
pixels of screen
.
球的行为都是一样的,因为当你第一次创建 velocity
时,你只调用 randint
来获得一次随机值.尝试将 randint
调用移动到 __init__
中,例如
The balls all behave the same because you only call randint
to get random values once, when you first create velocity
. Try moving the randint
call into __init__
, e.g.
def __init__(self, size, position, velocity=None):
if velocity is None:
velocity = {"x": {"mag": random.randint(3,7),
"dir": random.randrange(-1,2,2)},
"y": {"mag": random.randint(3,7),
"dir": random.randrange(-1,2,2)}}
self.size = size
self.position = position
self.velocity = velocity
self.color = (255, 255, 255)
这允许您提供 velocity
或随机分配一个.在 main()
中,你可以调用:
This allows you to provide a velocity
or be assigned a random one. In main()
, you can then call:
balls.append(Ball(size, position))
以随机速度
在鼠标position
处添加一个新的Ball
.
to add a new Ball
at the mouse position
with random velocity
.
顺便说一句,您可以将 position
和 velocity
属性简化为 (x, y)
元组,如 pygame
,而不是你的 dict
结构,即:
On a side-note, you could simplify your position
and velocity
attributes to (x, y)
tuples, as used in pygame
, rather than your dict
structures, i.e.:
velocity == (velocity['x']['mag'] * velocity['x']['dir'],
velocity['y']['mag'] * velocity['y']['dir'])
position == (position['x'], position['y'])
那么你在 main()
中的调用可能是:
Then your call in main()
could be:
balls.append(Ball(size, event.pos))
这篇关于为什么球的行为方式如此?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!