Python pygame渲染的问题
我遇到了一个问题,pygame只渲染一个带有奇怪的闪烁绿线的背景屏幕 我正在关注RealPython.com上的教程,第一次尝试时一切都很顺利。一旦我做了第二次尝试,这就是呈现的内容,这显然是错误的Python pygame渲染的问题,python,pygame,Python,Pygame,我遇到了一个问题,pygame只渲染一个带有奇怪的闪烁绿线的背景屏幕 我正在关注RealPython.com上的教程,第一次尝试时一切都很顺利。一旦我做了第二次尝试,这就是呈现的内容,这显然是错误的 #sidescrolling air-shooter import pygame #import pygame locals from pygame.locals import( K_UP, K_DOWN, K_LEFT, K_RIGHT, K_ESCAPE
#sidescrolling air-shooter
import pygame
#import pygame locals
from pygame.locals import(
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
#constants for screen width/height
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((75,25))
self.surf.fill((255, 255,255))
self.rect = self.surf.get_rect()
pygame.init()
#create screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Instantiate player
player = Player()
#keep the game running!
running = True
#loop time!
while running:
#look at all the events
for event in pygame.event.get():
#did the user hit a key?
if event.type == KEYDOWN:
#Was it escape? uh oh we gotta stop
if event.key == K_ESCAPE:
running = False
elif event.type ==QUIT:
running = False
#fill screen with white
screen.fill((255, 255, 255))
# Create a surface and pass in a tuple containing legth and width
surf = pygame.Surface((50, 50))
# Give the surface a color to separate it from the Background
surf.fill((0, 0, 0))
rect = surf.get_rect()
#This line says "Draw surf onto the screen at the center"
screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
#Draw the player on the screen
screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
pygame.display.flip()
pygame.quit()
我甚至还检查了他们项目的源代码,它似乎是匹配的。这是个问题。您需要在应用程序循环中绘制场景并更新显示,而不是在应用程序循环后更新显示:
循环时间!
运行时:
#看看所有的事件
对于pygame.event.get()中的事件:
#用户是否按了键?
如果event.type==KEYDOWN:
#是逃跑吗?哦,我们得停下来
如果event.key==K_转义:
运行=错误
elif event.type==退出:
运行=错误
#-->|压痕
#用白色填充屏幕
屏幕填充((255、255、255))
#创建曲面并传入包含长度和宽度的元组
surf=pygame.Surface((50,50))
#为曲面指定颜色以将其与背景分开
表面填充((0,0,0))
rect=surf.get_rect()
#这条线上写着“在屏幕中央绘制冲浪”
屏幕。blit(冲浪,(屏幕宽度/2,屏幕高度/2))
#在屏幕上画出玩家
screen.blit(player.surf,(屏幕宽度/2,屏幕高度/2))
pygame.display.flip()
pygame.quit()