pygame - 雪碧没有出现在pygame中
问题描述
我目前正在从教程中学习 pygame。所以在下面的代码中,我按照教程中所示的每个步骤进行操作,但结果仍然不同。我的精灵没有出现。一点帮助可能会有用。
import pygame
import random
WIDTH = 720
HEIGHT = 480
FPS = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0 ,0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
class Player(pygame.sprite.Sprite):
#Sprite for the player
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
pygame.init()
pygame.mixer.init() #----------------for sound and music
screen = pygame.display.set_mode((WIDTH, HEIGHT)) #-----------------setting up screen
pygame.display.set_caption("My Game") #----------------game title
clock = pygame.time.Clock() #----------------to check
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
running = True #--------------variable to control start and stop of game
while(running): #--------------Game loop begins
# Keep loop running at right speed/FPS
clock.tick(FPS)
# Process Input
for event in pygame.event.get():
if event.type == pygame.QUIT: #--------------Check for closing the window
running = False
# Update
all_sprites.update()
# Draw/Render
all_sprites.draw(screen)
screen.fill(BLACK)
pygame.display.flip()
pygame.quit()
解决方案
在绘制组中的精灵之前,您必须清除显示:
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
screen.fill(BLACK)
screen
用黑色填充整个表面。该操作在整个表面上绘制,之前绘制的所有内容都丢失了。
pygame.display.flip()
通过关联的表面更新窗口。当您screen.fill(BLACK)
之前这样做时,窗口将变为全黑。
推荐阅读
- c# - Asp .Net Identity 自定义 AspNetUser 主 id 字段值?
- magento - 从 magento 插件更改不生效,但从 magento\vendor 更改显示效果
- excel - 在不同的工作表上查找具有多个结果的值
- android - 以编程方式防止android应用程序的远程屏幕录制
- javascript - javascript中Object(this)的目的是什么?
- java - Reactor Mono,如何同步进程(使用 .map())并返回 Mono
来自 response.setComplete() - javascript - 一个团队的所有目标的数量(来自 JSON)
- clojurescript - Clojurescript 高级编译无法访问的代码
- graphics - 有没有办法自己定义相机参数?
- version-control - PyCharm 中用于 Jupyter 笔记本的 VCS