为什么球会像他们那样表现?

时间:2014-01-06 16:02:59

标签: 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, "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()

1 个答案:

答案 0 :(得分:2)

屏幕右侧的问题是由update()中此行中的拼写错误导致的:

if self.position["x"] <= 0 or self.position["x"] >= screen_y:
                                                         # ^ should be x

这可以防止Ball进入640 - 480 == 160的最右侧screen像素。

所有球的行为都相同,因为当您第一次创建randint时,您只需拨打velocity获取一次随机值。尝试将randint电话移至__init__,例如

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()。在balls.append(Ball(size, position)) 中,您可以调用:

Ball

使用随机position在鼠标velocity处添加新的position

在旁注中,您可以将velocity(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)) 的电话可能是:

{{1}}