0

我认为这个错误是不言自明的,所以我只发布代码和发生的事情的屏幕截图

'''
Created on Oct 29, 2012

@author: pipsqueaker
'''
import pygame, sys, random
from pygame.locals import *

frapsClock = pygame.time.Clock()

class Alien:
    def __init__(self, arg1, arg2):
        self.x = arg1
        self.y = arg2
    def move(self, craftx, crafty):
        if self.x < craftx:
            self.x += 1
        elif self.x > craftx:
            self.x -= 1
    if self.y < crafty:
        self.y += 1
    elif self.y > crafty:
        self.y -= 1

class PlayShip():
    def __init__(self, ag1, ag2):
        self.x = ag1
        self.y = ag2

class mainLopp():
    Board = pygame.display.set_mode((650, 500))
    pygame.display.set_caption("This")
    pygame.init()

    player = PlayShip(random.randrange(1, 650), random.randrange(1, 500))
    alienList = [Alien(random.randrange(1, 650), random.randrange(1, 500))]

    while True:
        pygame.draw.rect(Board, (0, 0, 255), (player.x, player.y, 12, 12))

        for currAlien in alienList:
            currAlien.move(player.x, player.y)
            pygame.draw.rect(Board, (255, 0, 0), (currAlien.x, currAlien.y, 16, 16))

        for event in pygame.event.get():
            if event.type == QUIT:
                print(alienList)
                pygame.quit()
                sys.exit()
        pygame.display.update()  
        frapsClock.tick(100)

move() 方法应该(随着时间的推移)将外星人(红色)移动到玩家(蓝色)。但是当该代码运行时,就会发生这种情况它不应该像这样连续

它不应该像那样连续。这引出了一个问题,我该如何解决?

4

1 回答 1

3

快速修复:

在主循环之前预先创建一种颜色:

black = pygame.Color('black')

在你的主循环中:

while True:
    Board.fill(black)
    pygame.draw.rect(Board, (0, 0, 255), (player.x, player.y, 12, 12))

这将在整个屏幕上涂黑,有效地擦除所有内容。这没关系,因为您在擦除后立即重新绘制所有内容。

在更高级的绘图代码中,您可以:

  • 在旧精灵的顶部绘制一个黑色矩形
  • 然后更新精灵的位置
  • 然后重新绘制精灵。
于 2012-10-29T18:59:38.120 回答