python-pygame 矩形不会绘制/渲染

标签 python pygame

我对编码相对较新,在了解了 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()

有什么想法吗? 提前致谢:)

最佳答案

puddle1 = Puddles(red, 400, 600)

水坑的 y 位置低于屏幕高度,因此它位于屏幕之外。 ;) 尝试将其更改为例如 puddle1 = Puddles(red, 400, 200)

此外,第 43-46 行应该缩进,以便它们位于 while 循环中。

关于python-pygame 矩形不会绘制/渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36867623/

相关文章:

python - 如何使用 Pandas 从excel文件中读取特定行

python - 如何检查按钮是否按下两次?

python - 使用 Python 流式传输音频(没有 GStreamer)

python - 我如何在 Pygame 中使用 Sprite Sheets?

python - 我应该使用 'not x' 还是 'x == 0' 来检查 python 中模运算的结果是否为零

python - 尝试通过 Twitter 预测用户的个性时,文本分类的准确性较低

python - 在特定深度的字典上添加项目

python - os.walk 返回一个生成器对象。我究竟做错了什么?

python - 恒定的键输入可以不断地移动 Sprite 吗?

audio - 在 pygame 中使用声音会使我的游戏崩溃