[英]python-pygame rectangle won't draw/render
im relatively new to coding and after understanding some basics in python i'm trying to apply oop in pygame I have this code and I can't figure out why the rectangle won't appear 我是相对较新的编码和理解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()
any ideas? 有任何想法吗? thanks in advance:)
提前致谢:)
puddle1 = Puddles(red, 400, 600)
The y position of the puddle is below the screen height, so it's outside of the screen. 水坑的y位置低于屏幕高度,因此它位于屏幕之外。 ;) Try to change it for example to
puddle1 = Puddles(red, 400, 200)
. ;)尝试将其更改为例如
puddle1 = Puddles(red, 400, 200)
。
Also, lines 43-46 should be indented, so that they're in the while loop. 另外,第43-46行应该缩进,以便它们在while循环中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.