如何解决Pygame - 如何更改和保持背景屏幕
我使用的是 Python 3.6。
当我左键单击“开始游戏”矩形对象时,我试图让 Pygame 执行以下操作:
- 通过变量“background”将新背景图像从“bgHome”图像更改为“bgIntro”图像。
- 按住新的背景图像,松开鼠标左键时移除矩形对象。
- 显示文件“intro.txt”中的文本并保留位于变量 text1 中的页脚文本。
代码按预期工作,直到我松开鼠标左键,它恢复到前一个屏幕,实际上是 mainloop() 开头的标题屏幕。
我想我需要在左键单击操作后添加一些行来告诉 Pygame 鼠标按钮仍然按下?
完整代码如下:
# Imports
import sys
import os
import random
import time
import pygame
from pygame.locals import *
# Must add to initialize pygame
pygame.init()
#Colour chart
LIGHT_BLUE = (51,102,255)
BLUE = (0,255)
DARK_BLUE = (0,102)
LIGHT_RED = (255,65,65)
RED = (255,0)
DARK_RED = (153,0)
LIGHT_GREEN = (102,255,0)
GREEN = (51,153,0)
DARK_GREEN = (0,51,0)
WHITE = (255,255)
GREY = (102,102)
BLACK = (0,0)
YELLOW = (255,0)
ORANGE = (255,0)
PURPLE = (153,204)
#Other variables
res = (500,500)
screen = pygame.display.set_mode(res)
width = screen.get_width()
height = screen.get_height()
leftclick = (1,0)
middleclick = (0,1,0)
rightclick = (0,1)
#Backgrounds for game
background = pygame.image.load("bg1.png")
bgHome = pygame.image.load("bg1.png")
bgOptions = 0
bgIntro = pygame.image.load("bg2.png")
bgSimulate = 0
bgAssets = 0
bgEmpl = 0
#Setting up Fonts
smallfont = pygame.font.SysFont('Corbel',20)
mediumfont = pygame.font.SysFont('Corbel',32)
largefont = pygame.font.SysFont('Corbel',46)
#Common text featured throughout game
text1 = "Footer text goes here."
copyright_text = smallfont.render(text1,True,WHITE)
#Load text file
f = open('Intro.txt','r')
content = f.read()
intromsg = smallfont.render(content,BLACK)
#Set texts for buttons
start_butt_text = smallfont.render('Start Game',WHITE)
load_butt_text = smallfont.render('Load Game',WHITE)
quit_butt_text = smallfont.render('Quit Game',WHITE)
#Player class
class Player():
def __init__(self,name):
self.name = name
self.cash = 10000
self.assets = 0
self.execs = 0
self.mgmt = 0
self.directlab = 0
self.indirectlab = 0
self.liab = 0
self.equity = self.cash + self.assets - self.liab
#Create a white screen
disPLAYSURF = pygame.display.set_mode((500,500))
disPLAYSURF.fill(WHITE)
pygame.display.set_caption("Capitalism: The Game")
def ButtonSG():
pygame.draw.rect(screen,GREY,[10,10,140,40])
pygame.draw.rect(screen,RED,[15,15,130,30])
pygame.draw.rect(screen,60,BLACK,30])
pygame.draw.rect(screen,110,115,30])
def ButtonLG():
pygame.draw.rect(screen,30])
def ButtonQG():
pygame.draw.rect(screen,30])
def ButtonNil():
pygame.draw.rect(screen,30])
def IntroScreen():
disPLAYSURF.fill(WHITE)
background = bgIntro
disPLAYSURF.blit(background,(0,0))
disPLAYSURF.blit(intromsg,(10,10))
#Game loop
def resetscreen():
disPLAYSURF.blit(bgHome,0))
def mainloop():
while True:
background = pygame.image.load("bg1.png")
# fills the screen with a color
disPLAYSURF.blit(background,0))
# stores the (x,y) coordinates into
# the variable as a tuple
mouse = pygame.mouse.get_pos()
# if mouse is hovered on a button it changes colour
if width-width+10 <= mouse[0] <= width-width+150 and height-height+10 <= mouse[1] <= height-height+50:
ButtonSG()
elif width-width+10 <= mouse[0] <= width-width+150 and height-height+60 <= mouse[1] <= height-height+100:
ButtonLG()
elif width-width+10 <= mouse[0] <= width-width+150 and height-height+110 <= mouse[1] <= height-height+150:
ButtonQG()
else:
ButtonNil()
# superimposing the text onto our button
screen.blit(start_butt_text,(35,22))
screen.blit(load_butt_text,72))
screen.blit(quit_butt_text,122))
screen.blit(copyright_text,height-25))
#checks if a mouse is clicked
if pygame.mouse.get_pressed() == (leftclick):
if width-width+10 <= mouse[0] <= width-width+150 and height-height+10 <= mouse[1] <= height-height+50:
IntroScreen()
elif width-width+10 <= mouse[0] <= width-width+150 and height-height+60 <= mouse[1] <= height-height+100:
pass
elif width-width+10 <= mouse[0] <= width-width+150 and height-height+110 <= mouse[1] <= height-height+150:
pygame.quit()
sys.exit()
else:
pass
screen.blit(copyright_text,height-25))
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
# updates the frames of the game
pygame.display.update()
mainloop()
解决方法
背景变量在主应用程序循环中连续设置。因此,如果您更改变量,它将在循环开始时重置。删除这行代码:
def mainloop():
while True:
# background = pygame.image.load("bg1.png") <--- DELETE
如果你想改变背景,你所要做的就是改变Background
变量:
background = bgIntro
或
background = bgHome
请注意,如果您想在函数内部更改它,则必须使用 global
statement,因为 background
是全局命名空间中的变量。见Using global variables in a function:
def clicked_start():
global background
background = bgIntro
# [...]
点击检测未按预期工作。关于这个主题有很多问题和答案。例如:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。