Python 为什么球会这样?
我希望每个球都能独立移动。我认为问题与它们的速度相同有关,但我不知道它们为什么会这样,或者这是否是问题所在。另外,为什么屏幕的右侧部分会有这样的行为?我希望球能够在整个屏幕上正常移动Python 为什么球会这样?,python,pygame,Python,Pygame,我希望每个球都能独立移动。我认为问题与它们的速度相同有关,但我不知道它们为什么会这样,或者这是否是问题所在。另外,为什么屏幕的右侧部分会有这样的行为?我希望球能够在整个屏幕上正常移动 import sys import pygame import random screen_size = (screen_x, screen_y) = (640, 480) screen = pygame.display.set_mode(screen_size) size = {"width": 10, "h
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()
导入系统
导入pygame
随机输入
屏幕大小=(屏幕x,屏幕y)=(640480)
screen=pygame.display.set_模式(屏幕大小)
大小={“宽度”:10,“高度”:10}
速度={“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=大小
self.position=位置
自速度=速度
self.color=(255、255、255)
def更新(自我):
自速度[“x”]+=(自速度[“x”][“mag”]*自速度[“x”][“dir”])
自速度[“y”]+=(自速度[“y”][“mag”]*自速度[“y”][“dir”])
如果自身位置[“x”]=屏幕y:
自速度[“x”][“dir”]*=-1
如果自身位置[“y”]=屏幕y:
自速度[“y”][“dir”]*=-1
self.rect=pygame.rect(self.position[“x”]、self.position[“y”]、size[“width”]、size[“height”])
def显示(自):
pygame.draw.rect(屏幕,self.color,self.rect)
def main():
pygame.init()
fps=30
clock=pygame.time.clock()
球=[]
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type==pygame.MOUSEBUTTONDOWN:
(x,y)=事件位置
位置={“x”:x,“y”:y}
新球=球(大小、位置、速度)
balls.append(新球)
对于球中球:
ball.update()
屏幕填充((0,0,0))
对于球中球:
ball.display()
pygame.display.update()
时钟滴答声(fps)
如果名称=“\uuuuu main\uuuuuuuu”:
main()
屏幕右侧的问题是由于
update()
中的这一行输入错误造成的:
这允许您提供速度
或分配一个随机速度。在main()
中,您可以调用:
balls.append(Ball(size, position))
使用随机速度
在鼠标位置
添加新的球
另一方面,您可以将位置
和速度
属性简化为(x,y)
元组,如pygame
中使用的,而不是dict
结构,即:
velocity == (velocity['x']['mag'] * velocity['x']['dir'],
velocity['y']['mag'] * velocity['y']['dir'])
position == (position['x'], position['y'])
然后您在main()
中的调用可能是:
balls.append(Ball(size, event.pos))
velocity == (velocity['x']['mag'] * velocity['x']['dir'],
velocity['y']['mag'] * velocity['y']['dir'])
position == (position['x'], position['y'])
balls.append(Ball(size, event.pos))