我相对较新的编码和了解python中的一些基础知识后我试图在pygame中应用oop 我有这个代码,我无法弄清楚矩形不会出现的原因
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 :(得分:0)
puddle1 = Puddles(red, 400, 600)
水坑的y位置低于屏幕高度,因此它位于屏幕之外。 ;)尝试将其更改为puddle1 = Puddles(red, 400, 200)
。
此外,第43-46行应缩进,以便它们处于while循环中。