2016-04-26 62 views
1

即時通訊相對較新的編碼和理解了python的一些基本知識後,我試圖在pygame應用oop 我有這個代碼,我想不通爲什麼矩形贏了不會出現蟒蛇pygame矩形不會繪製/渲染

import pygame 
import time 

pygame.init() 

screen = pygame.display.set_mode((800,600)) 
pygame.display.set_caption("EXAMPLE") 

clock = pygame.time.Clock() 

white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
FPS = 30 

class Puddles(pygame.sprite.Sprite): 
    puddle_width = 100 
    puddle_height = 20 
    def __init__(self,color,x,y): 
     pygame.sprite.Sprite.__init__(self) 
     self.x = x 
     self.y = y 
     self.color = color 
     self.image = pygame.Surface((Puddles.puddle_width,Puddles.puddle_height)) 
     self.image.fill(self.color) 
     self.rect = self.image.get_rect() 
     self.rect.x = self.x # i think problem's here because if I type specific integers for x and y the tile will appear 
     self.rect.y = self.y 

all_sprites = pygame.sprite.Group() 
puddle1 = Puddles(red, 400, 600) 
all_sprites.add(puddle1) 
gameExit = False 

while not gameExit: 
    clock.tick(FPS) 
    for event in pygame.event.get(): 
     print event 

     if event.type == pygame.QUIT: 
      gameExit = True 

all_sprites.update() 
screen.fill(white) 
all_sprites.draw(screen) 
pygame.display.flip() 


pygame.quit() 
quit() 

有什麼想法嗎? 在此先感謝:)

回答

0
puddle1 = Puddles(red, 400, 600) 

水坑的y位置低於屏幕高度,所以它的屏幕之外。 ;)嘗試將其更改爲例如puddle1 = Puddles(red, 400, 200)

另外,第43-46行應該縮進,以便它們在while循環中。

+0

哦謝謝老兄!我犯了多麼愚蠢的錯誤!因爲(400,600)(600是邊緣)我把這些數字隨機,我想檢查它是否工作 – Aris

+0

和第43-46行意外地存在,因爲這是我第一次問一個問題。感謝你的回答! – Aris

+0

@Aris我很高興我能幫助你! :)請註冊並接受。 – skrx