私はPygameをかなり新しくしています。Pygameスプライトが移動する代わりに複製を作成する
Pygameを使用してPythonでPongを再作成しようとしていますが、私はロードブロッキングを起こしました。ここで
は私のコードです:
import pygame
pygame.init()
UP = "up"
DOWN = "down"
white = (255,255,255)
black = (0,0,0)
pygame.mouse.set_visible(True)
resolution = (800,600)
window = pygame.display.set_mode(resolution)
pygame.display.set_caption("Pong!")
clock = pygame.time.Clock()
sprites_list = pygame.sprite.Group()
running = True
class Paddle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((25,100))
self.image.fill(white)
pygame.draw.rect(self.image,white, (0,0,25,100))
self.rect = self.image.get_rect()
def move(self,direction,pixels):
if direction == DOWN:
self.rect.y -= pixels
if direction == UP:
self.rect.y += pixels
player1 = Paddle()
player1.rect.x = 0
player1.rect.y = 200
sprites_list.add(player1)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
player1.move(DOWN, 5)
if keys[pygame.K_UP]:
player1.move(UP, 5)
sprites_list.update()
sprites_list.draw(window)
pygame.display.flip()
clock.tick(60)
pygame.quit()
私は、player1を作るためにパドルオブジェクトをしようとしています、押されたキーに応じて上下に移動します。このコードを実行すると、上下の矢印を押した後にplayer1が伸びます。 5ピクセルがplayer1.rectに追加されます。
私は間違っていますが、なぜsprites_list.update()はplayer1を新しい位置に配置しませんか?
追加することです。黒色で塗りつぶす 'window.fill((0,0,0))' – furas