0
我試圖在屏幕上顯示一組精靈,但是,我的下面的代碼顯示一個空白屏幕。我一直在這一段時間,我不知道爲什麼這不工作的邏輯似乎是正確的。主要問題發生在精靈方法中,我試圖在隨機位置添加精靈。然後用gameLoop方法繪製精靈。將精靈添加到組
有什麼建議嗎?
import pygame, sys, random
pygame.init()
troll = pygame.image.load("image.png")
black = (0, 0, 0)
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 400
sprite_width = 5
sprite_height = 5
spriteCount = 5
class Sprite(pygame.sprite.Sprite):
pygame.init()
sprite = pygame.sprite.Group()
clock = pygame.time.Clock()
def __init__(self):
pygame.sprite.Sprite.__init__(self)
def __init__(self, Image, pos):
pygame.sprite.Sprite.__init__(self)
self.image =pygame.Surface([0, 0])
self.rect = self.image.get_rect()
self.rect.topleft = pos
@classmethod
def sprite(self):
for i in range(spriteCount):
tmp_x = random.randrange(0, SCREEN_WIDTH)
tmp_y = random.randrange(0, SCREEN_HEIGHT)
# all you have to do is add new sprites to the sprite group
self.sprite.add(Sprite(troll, [tmp_x, tmp_y]))
def update(self):
self.rect.x += 1
self.rect.y += 2
if self.rect.y > SCREEN_HEIGHT:
self.rect.y = -1 * sprite_height
if self.rect.x > SCREEN_WIDTH:
self.rect.x = -1 * sprite_width
@classmethod
def setImage(self,Image):
self.image=pygame.image.load(Image)
@classmethod
def gameLoop(self):
screen = pygame.display.set_mode([640, 400])
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(black)
# to update or blitting just call the groups update or draw
# notice there is no for loop
# this will automatically call the individual sprites update method
self.actor.update()
self.actor.draw(screen)
pygame.display.update()
self.clock.tick(20)
Sprite.sprite()
Sprite.setImage("image.png")
Sprite.gameLoop()
感謝這個,說如果我不希望使用類的方法和我代替 '巨魔=雪碧() 巨魔。 init() troll.gameLoop()' init方法只有類第一個,它不執行方法重載,我該如何解決這個問題? –