pythonpygame矩形无法绘制/呈现

2024-10-01 02:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编码比较陌生,在理解了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()

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


Tags: rectimageselfeventinitdisplayallscreen
1条回答
网友
1楼 · 发布于 2024-10-01 02:27:06
puddle1 = Puddles(red, 400, 600)

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

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

相关问题 更多 >