【问题标题】:Pygame - How to change and hold background screenPygame - 如何更改和保持背景屏幕
【发布时间】:2021-01-30 17:33:27
【问题描述】:

我使用的是 Python 3.6。

当我左键单击“开始游戏”矩形对象时,我试图让 Pygame 执行以下操作:

  1. 通过变量“background”将新的背景图像从“bgHome”图像更改为“bgIntro”图像。
  2. 按住新的背景图片,松开鼠标左键时移除矩形对象。
  3. 显示文件“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, 0, 255)
DARK_BLUE = (0, 0, 102)
LIGHT_RED = (255, 65, 65)
RED = (255, 0, 0)
DARK_RED = (153, 0, 0)
LIGHT_GREEN = (102, 255, 0)
GREEN = (51, 153, 0)
DARK_GREEN = (0, 51, 0)
WHITE = (255, 255, 255)
GREY = (102, 102, 102)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 102, 0)
PURPLE = (153, 0, 204)


#Other variables
res = (500,500)
screen = pygame.display.set_mode(res)
width = screen.get_width()
height = screen.get_height()
leftclick = (1, 0, 0)
middleclick = (0, 1, 0)
rightclick = (0, 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, True, BLACK)


#Set texts for buttons
start_butt_text = smallfont.render('Start Game' , True , WHITE)
load_butt_text = smallfont.render('Load Game' , True , WHITE)
quit_butt_text = smallfont.render('Quit Game' , True , 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,GREY,[10,60,140,40])
        pygame.draw.rect(screen,BLACK,[15,65,130,30])
        pygame.draw.rect(screen,GREY,[10,110,140,40])
        pygame.draw.rect(screen,BLACK,[15,115,130,30])


def ButtonLG():
        pygame.draw.rect(screen,GREY,[10,10,140,40])
        pygame.draw.rect(screen,BLACK,[15,15,130,30]) 
        pygame.draw.rect(screen,GREY,[10,60,140,40])
        pygame.draw.rect(screen,RED,[15,65,130,30])
        pygame.draw.rect(screen,GREY,[10,110,140,40])
        pygame.draw.rect(screen,BLACK,[15,115,130,30])
        
        
def ButtonQG():
        pygame.draw.rect(screen,GREY,[10,10,140,40])
        pygame.draw.rect(screen,BLACK,[15,15,130,30])
        pygame.draw.rect(screen,GREY,[10,60,140,40])
        pygame.draw.rect(screen,BLACK,[15,65,130,30])
        pygame.draw.rect(screen,GREY,[10,110,140,40])
        pygame.draw.rect(screen,RED,[15,115,130,30])
        
def ButtonNil():
        pygame.draw.rect(screen,GREY,[10,10,140,40])
        pygame.draw.rect(screen,BLACK,[15,15,130,30])
        pygame.draw.rect(screen,GREY,[10,60,140,40])
        pygame.draw.rect(screen,BLACK,[15,65,130,30])
        pygame.draw.rect(screen,GREY,[10,110,140,40])
        pygame.draw.rect(screen,BLACK,[15,115,130,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,0))


def mainloop():
    while True: 
                  
        background = pygame.image.load("bg1.png")
            
        # fills the screen with a color 
        
        DISPLAYSURF.blit(background, (0,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, (35,72)) 
        screen.blit(quit_butt_text, (35,122))
        screen.blit(copyright_text, (10,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, (10,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()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    背景变量在主应用程序循环中连续设置。因此,如果您更改变量,它将在循环开始时重置。去掉这行代码:

    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
        
        # [...]
    

    点击检测未按预期工作。关于这个主题有很多问题和答案。例如:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2021-10-06
      • 2011-07-24
      • 2011-05-17
      • 2017-05-02
      • 1970-01-01
      相关资源
      最近更新 更多