不确定为什么 If 语句不起作用。 (Pygame)
Not sure why If Statement is not working. (Pygame)
好的,现在当我在 "start.png" 处按 E 时,它会跳转到 "cele.png",而我希望它跳转到 "new.png"。我希望它从 "start.png" 开始然后当您按 E 它应该更改为 "new.png" 如果您在背景为 "new.png" 时再次按 E 那么它应该更改为 "cele.png" 谢谢!
#import library
import pygame
from pygame.locals import *
#initialize the game
pygame.init()
width, height = 1000, 800
screen=pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame Window")
#load images
bg_filename = "start.png"
background = pygame.image.load(bg_filename)
#keep looping through
while 1:
#clear the screen before drawing it again
screen.fill(0)
#draw the screen elements
screen.blit(background, (0,0))
#update the screen
pygame.display.flip()
#loop through the events
for event in pygame.event.get():
#start to new
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
if bg_filename == "start.png":
bg_filename = "new.png"
background = pygame.image.load(bg_filename)
#new to cele
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
if bg_filename == "new.png":
bg_filename = "cele.png"
background = pygame.image.load(bg_filename)
#check if the event is the X button
if event.type==pygame.QUIT:
#if it is quit the game
pygame.quit()
exit(0)
Surfaces 没有用于检索文件名的对象,但也许您可以使用另一个变量来保存文件名?
#load images
bg_filename = "start.png"
background = pygame.image.load(bg_filename)
#keep looping through
while 1:
#clear the screen before drawing it again
screen.fill(0)
#draw the screen elements
screen.blit(background, (0,0))
#update the screen
pygame.display.flip()
#loop through the events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
if bg_filename == "start.png":
bg_filename = "new.png"
background = pygame.image.load(bg_filename)
好的,现在当我在 "start.png" 处按 E 时,它会跳转到 "cele.png",而我希望它跳转到 "new.png"。我希望它从 "start.png" 开始然后当您按 E 它应该更改为 "new.png" 如果您在背景为 "new.png" 时再次按 E 那么它应该更改为 "cele.png" 谢谢!
#import library
import pygame
from pygame.locals import *
#initialize the game
pygame.init()
width, height = 1000, 800
screen=pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame Window")
#load images
bg_filename = "start.png"
background = pygame.image.load(bg_filename)
#keep looping through
while 1:
#clear the screen before drawing it again
screen.fill(0)
#draw the screen elements
screen.blit(background, (0,0))
#update the screen
pygame.display.flip()
#loop through the events
for event in pygame.event.get():
#start to new
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
if bg_filename == "start.png":
bg_filename = "new.png"
background = pygame.image.load(bg_filename)
#new to cele
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
if bg_filename == "new.png":
bg_filename = "cele.png"
background = pygame.image.load(bg_filename)
#check if the event is the X button
if event.type==pygame.QUIT:
#if it is quit the game
pygame.quit()
exit(0)
Surfaces 没有用于检索文件名的对象,但也许您可以使用另一个变量来保存文件名?
#load images
bg_filename = "start.png"
background = pygame.image.load(bg_filename)
#keep looping through
while 1:
#clear the screen before drawing it again
screen.fill(0)
#draw the screen elements
screen.blit(background, (0,0))
#update the screen
pygame.display.flip()
#loop through the events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
if bg_filename == "start.png":
bg_filename = "new.png"
background = pygame.image.load(bg_filename)