【问题标题】:pygame Sprite on raspberry pi issuepygame Sprite 在树莓派问题上
【发布时间】:2015-02-03 21:00:03
【问题描述】:

我已经开始使用 raspberry-pi B+,我正在关注this guide

特别是在 pdf 的第 68 页(书的 52 页)有一个使用 Sprite 的脚本。脚本如下:

import pygame

class Ball(pygame.sprite.Sprite):
    def __init__(self, x, y, xdir, ydir, speed): 
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([20, 20])
        self.image.fill(pygame.Color(255, 255, 255))          
        pygame.draw.circle(self.image,
             pygame.Color(255,0,0),
             (10,10), 10, 0)
        self.rect = self.image.get_rect()
        self.x, self.y = x, y
        self.xdir, self.ydir = xdir, ydir
        self.speed = speed

    def update(self):
        self.x = self.x + (self.xdir * self.speed)
        self.y = self.y + (self.ydir * self.speed)
        if (self.x < 10) | (self.x > 490):
            self.xdir = self.xdir * -1
        if (self.y < 10) | (self.y > 490):
            self.ydir = self.ydir * -1
        self.rect.center = (self.x, self.y)

pygame.init()
fps = pygame.time.Clock()
window = pygame.display.set_mode((500, 500))
ball = Ball(100, 250, 1, 1, 5)
ball2 = Ball(400, 10, -1, -1, 8)

while True: ball.update()
    ball2.update() 
    window.fill(pygame.Color(255,255, 255))
    window.blit(ball.image, ball.rect)
    window.blit(ball2.image, ball2.rect)
    pygame.display.update() fps.tick(30)

脚本编译但不工作,窗口不工作也不更新。

你有什么想法吗? 提前致谢

【问题讨论】:

  • 为什么投反对票?请解释一下。

标签: python pygame raspberry-pi sprite


【解决方案1】:

我不熟悉 PyGame,但请尝试将 Ball 类中的 self.xself.y 分别替换为 self.rect.xself.rect.y

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多