gpt4 book ai didi

python - 空白的 Pygame 屏幕

转载 作者:太空宇宙 更新时间:2023-11-04 01:53:54 24 4
gpt4 key购买 nike

我知道那不是真正的黑屏,但现在我遇到了问题。加载游戏需要很长时间。每次我关闭窗口时,它都会完全加载????如果您需要代码,请询问。

请帮忙

最佳答案

您必须创建一个 PlayerClass 对象,并且您必须添加 pygame.sprite.Spritepygame.sprite.Group :

player = PlayerClass('player_1_0.png', [5, 0])

all_sprites = pygame.sprite.Group()
all_sprites.add(player)

在主循环中,您必须更新玩家的位置 (player.move())。清除显示,绘制 Sprite 并更新显示(例如 pygame.display.update() ):

while running:

# [...]

player.move()

screen.fill([255, 255, 255])
all_sprites.draw(screen)
pygame.display.update()

工作示例:

import pygame, sys

screen = pygame.display.set_mode([640,480])
clock = pygame.time.Clock()

class PlayerClass(pygame.sprite.Sprite):
def __init__(self, image_file, speed, location = [0,0]):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.center = [320, 100]
self.speed = speed
self.angle = 90

def move(self):
global points, score_text
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > screen.get_width():
self.speed[0] = -self.speed[0]

if self.rect.top <= 0 :
self.speed[1] = -self.speed[1]
points = points + 1
score_text = font.render(str(points), 1, (0, 0, 0))

player = PlayerClass('player_1_0.png', [5, 0])

all_sprites = pygame.sprite.Group()
all_sprites.add(player)

running = True
while running:
clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

player.move()

screen.fill([127, 127, 127])
all_sprites.draw(screen)
pygame.display.update()

pygame.quit()

关于python - 空白的 Pygame 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57357812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com