我对编码很陌生。我(可怕的)编程老师直接把它扔进了它。这就是我所拥有的:
import pygame, sys, time
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Caldun")
bg = pygame.image.load("Background.png")
bg2 = pygame.image.load("BG2 clone.png")
bg3 = pygame.image.load("BG2 clone clone.png")
white = (255, 255, 255)
black = (0, 0, 0)
myfont = pygame.font.SysFont("Press Start 2P", 23)
label = myfont.render("Welcome to the land of Caldun.", 1, (255,255,255))
label2 = myfont.render("Click to continue", 1, (255,255,255))
running = 1
while running:
screen.fill((black))
screen.blit(bg,(0,0))
screen.blit(bg3,(37,30))
screen.blit(label, (65,420))
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == pygame.KEYDOWN and
(event.key == K_ESCAPE)):
pygame.quit()
sys.exit()
if(event.type == pygame.KEYDOWN and
(event.key == K_SPACE)):
screen.blit(bg2,(37,30))
pygame.display.update()
我正在尝试做的是,当你按空格键时,它会在bg3上blit bg2,并且基本上从屏幕上移除bg3。出于某种原因,这对我不起作用。发生的事情是屏幕类型只是闪烁并且仅显示bg2瞬间。我确实知道我有两个pygame.display.update()s,这可能会导致它,但我很丢失。如果我能得到一些关于如何做的指示,甚至如何清理我的代码以及在哪里使用它,我将不胜感激。
答案 0 :(得分:0)
好的,我已经按照以下方式重写了你的主循环以解决问题:
running = True
stage = 1
while running:
screen.fill(black)
screen.blit(bg, (0, 0))
screen.blit(label, (65, 420))
if stage == 1:
screen.blit(bg2, (37, 30))
else:
screen.blit(bg3, (37, 30))
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == pygame.KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == K_SPACE:
stage += 1
现有代码的问题在于它假定KEYDOWN
事件是粘性的,即在按住键时报告每次循环迭代,但事实并非如此。要解决此问题,我们使用stage
变量来跟踪您要显示的当前状态,并在按下空格键时更新它。
根据您的评论我已经更新了代码,因此您可以使用它来呈现简单的序列。要在序列中添加其他阶段,只需更新stage
if test以支持其他值,例如:
if state == 1:
...render graphics for stage 1
elif state == 2:
...render graphics for stage 2
elif state == n:
...render graphics for stage `n`
else:
...render the final stage graphics