2016-01-11 39 views
1

我在一個遊戲編程課,我應該做一個遊戲,從屏幕的頂部掉落下來,你捕捉或避免的精靈。現在我正在努力讓物體掉落,他們一開始工作,但後來我搞砸了一些東西,因爲現在它們在屏幕上掉下時出現斑駁/隨機消失。我試着改變代碼中的各種東西來解決這個問題,但我一直被困住了。Sprites在屏幕上沒有正確顯示 - pygame

我非常確定我搞砸了煤炭/糖果到屏幕上,或者在別人消失後添加新的東西,但我包含了我的代碼的很大一部分,以防萬一有些東西搞亂了它向上。我會強調下面可能出現的混亂部分。

噢,我也總是很欣賞如何使代碼更加簡潔/高效的評論,但老師希望代碼與他教我們的方式類似,所以如果你確實有特定的修補程序,如果它可以保留代碼類似於現在我真的很感激它!

非常感謝!

#Setting up time/timer 
time = 6000 
TICKTOCK = 0 
pygame.time.set_timer (TICKTOCK+1, 10) 

#Class for candy 
class Candy: 
    def __init__(self): 
     self.x = random.randint(0, SCREEN_WIDTH - 150) 
     self.y = random.randint(-1000, 0) 
     self.image_Candy = pygame.image.load('konpeito.png') 
     self.height = self.image_Candy.get_height() 
     self.width = self.image_Candy.get_width() 

    def collide (self, sprite): 
     selfRect = pygame.Rect(self.x, self.y, self.width, self.height) 
     spriteRect = pygame.Rect(sprite.x, sprite.y, sprite.width, sprite.height) 
     if selfRect.colliderect(spriteRect): 
      return True 
     else: 
      return False 

#class for coal 
class Coal: 
    def __init__(self): 
     self.x = random.randint(0, SCREEN_WIDTH - 150) 
     self.y = random.randint(-1000, SCREEN_HEIGHT) 
     self.image_Coal = pygame.image.load('coal.png') 
     self.width = self.image_Coal.get_width() 
     self.height = self.image_Coal.get_height() 

    def collide (self, sprite): 
     selfRect = pygame.Rect(self.x, self.y, self.width, self.height) 
     spriteRect = pygame.Rect(sprite.x, sprite.y, sprite.width, sprite.height) 
     if selfRect.colliderect(spriteRect): 
      return True 
     else: 
      return False 

#class for sootsprite (collects candy and coal) 
class Sootsprite: 
    def __init__(self): 
     self.x = random.randint(0, SCREEN_WIDTH) 
     self.y = random.randint(0, SCREEN_HEIGHT) 
     self.image_bowl = pygame.image.load('sootsprite.png') 
     self.height = self.image_bowl.get_height() 
     self.width = self.image_bowl.get_width() 

clock = pygame.time.Clock() 
fps = 10 

#Creating candies and rocks 
bowl = [] 
for i in range (15): 
    candyInstance = Candy() 
    bowl.append(candyInstance) 

rocks = [] 
for i in range (8): 
    coalInstance = Coal() 
    rocks.append(coalInstance) 

catch = Sootsprite() 

playground.fill(cyan) 

Game_Over = False 

while not Game_Over: 
    font = pygame.font.SysFont(None, 30) 
    endfont = pygame.font.SysFont(None, 100) 
    text = font.render('Points: ' + str(total) + '/15', True, black) 
    playground.blit(text, (0,0)) 
    timer = font.render('Time remaining: ' + str(time), True, black) 
    playground.blit(timer, (0, 40)) 
    end = endfont.render("GAME OVER." + str(total) + " POINTS EARNED", True, black) 

這是我的東西被blit屏幕上,並可能搞砸了:

playground.blit(catch.image_bowl, (catch.x, catch.y)) 
    playground.blit(candyInstance.image_Candy, (candyInstance.x, candyInstance.y)) 
    playground.blit(coalInstance.image_Coal, (coalInstance.x, coalInstance.y)) 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      Game_Over = True 
     if event.type == TICKTOCK+1: 
      time -=1 
     #ends game when time is over 
     if time < 0: 
      playground.blit(end, (300, 400)) 

     #moving sootsprite with mouse 
     if event.type == MOUSEMOTION: 
      (catch.x, catch.y) = pygame.mouse.get_pos() 

    #making candy fall  
    for candyInstance in bowl: 
     if candyInstance.x <= 0: 
      candyInstance.x += 0 
     elif candyInstance.x >= 0 : 
      candyInstance.x -=0 

     if candyInstance.y <= (SCREEN_HEIGHT+150): 
      candyInstance.y += 20 
     elif candyInstance.y > (SCREEN_HEIGHT + 150) : 
      candyInstance.y = -100 

這就是我刪除的東西,添加新的和可能已經搞砸了:

#removing candy when collected   
    for candyInstance in bowl: 
     if candyInstance.collide(catch): 
      bowl.remove(candyInstance) 
      total += 1 
      candyInstance = Candy() 
      bowl.append(candyInstance) 

    #making coal fall 
    for coalInstance in rocks: 
     if coalInstance.x <= 0: 
      coalInstance.x += 0 
     elif coalInstance.x >= 0 : 
      coalInstance.x -=0 

     if coalInstance.y <= (SCREEN_HEIGHT + 200): 
      coalInstance.y += 20 
     elif coalInstance.y > (SCREEN_HEIGHT + 200) : 
      coalInstance.y = -100   

這也是一個地方,我刪除的對象,並添加新的:

#removing coal when collected 
    for coalInstance in rocks: 
     if coalInstance.collide(catch): 
      rocks.remove(coalInstance) 
      total -= 1 
      coalInstance = Coal() 
      rocks.append(coalInstance) 

    pygame.display.flip() 
    playground.fill(cyan) 
    clock.tick (fps) 

pygame.quit() 
+0

你'具有15個元素bowl'列表同樣的問題,但是當你的blit你不使用它。 – furas

+0

我已經嘗試了使用列表blit的方法,但是我得到的錯誤例如「參數必須是表面,不是列表」或「列表沒有屬性」image_xyz,'「有沒有一種方法可以將列表合併到blit聲明? –

回答

0

blit只有一顆糖果。使用for循環至blit所有糖果。

for candyInstance in bowl: 
    playground.blit(candyInstance.image_Candy, (candyInstance.x, candyInstance.y)) 

您有rocks

for coalInstance in rocks: 
    playground.blit(coalInstance.image_Coal, (coalInstance.x, coalInstance.y)) 
+0

非常感謝!你的回答總是很有幫助,我很欣賞你花時間瀏覽我的代碼! –

+1

請參閱示例中的建議如何組織代碼:http://pastebin.com/p2KAfsHH。使用'self.rect'來保留位置和大小(而不是'self.x','self,y','self.width','self。height'),並且你可以創建'def draw(self,surface) :surface.blit(self.image,self.rect)'在類中,並使用它'candyInstance.draw(操場)' – furas