Skip to main content
 首页 » 编程设计

python之单击 "play"时如何清除游戏屏幕并移动到新场景

2024年11月24日852php

我不知道如何使用我的按钮功能将 background.jpg 覆盖在按钮上或删除当前屏幕并在清除场景后将背景放回原位。

import pygame 
 
 
pygame.init() 
 
 
screen = pygame.display.set_mode((800, 600)) 
clock = pygame.time.Clock() 
 
 
 
BLACK = (0, 0, 0) 
BACKGROUND = (200, 230, 234) 
WHITE = (255, 255, 255) 
HOVER_COLOUR = (50, 70, 90) 
# Text Variables  
FONT = pygame.font.SysFont ("Times New Norman", 60) 
TEXT = FONT.render ("", True, WHITE) 
background_images = pygame.image.load("background.jpg").convert() 
screen.blit(background_images, [0,0]) 
screen.blit(TEXT, (150, 50)) 
# Text & Rectangles construction 
text1 = FONT.render("PlAY", True, WHITE) 
text2 = FONT.render("CONTROLS", True, WHITE) 
text3 = FONT.render("DIFFICULTY", True, WHITE) 
text4 = FONT.render("SCOREBOARD", True, WHITE) 
 
rect1 = pygame.Rect(250,200,300,80) 
rect2 = pygame.Rect(250,300,300,80) 
rect3 = pygame.Rect(250,400,300,80) 
rect4 = pygame.Rect(250,500,300,80) 
# The button construction arry. Text and Rectangle  
buttons = [ 
        [text1, rect1, BACKGROUND, 1], 
        [text2, rect2, BACKGROUND, 2], 
        [text3, rect3, BACKGROUND, 3], 
        [text4, rect4, BACKGROUND, 4], 
        ] 
 
# Function for button printing (testing) 
def on_button(buttons): 
        print(buttons[3]) 
 
 
def game_intro(): 
        while True: 
                for event in pygame.event.get(): 
                        if event.type == pygame.QUIT: 
                                return 
                        elif event.type == pygame.MOUSEMOTION: 
                                for button in buttons: 
                                        # Uses collisionpoint to detect mouse position collisions 
                                        if button[1].collidepoint(event.pos): 
                                                # Set the button's colour to the hover colour. 
                                                button[2] = HOVER_COLOUR 
                                        else: 
                                                # resets the colour to normal. 
                                                button[2] = BACKGROUND 
                        # Button Controls  
                        elif event.type == pygame.MOUSEBUTTONDOWN: 
                                for button in buttons: 
                                        # Uses collisionpoint to detect mouse position collisions 
                                        if button[1].collidepoint(event.pos): 
                                                on_button(button) 
                                        if button == buttons[0]: 
                                                screen.fill(0,0,0) 
 
 
                # Draws the buttons with their current colours (normal & collisions) 
                for text, rect, colour, button_id in buttons: 
                        pygame.draw.rect(screen, colour, rect) 
                        screen.blit(text, rect) 
 
                pygame.display.flip() 
                clock.tick(15) 
 
 
#Run Game 
game_intro() 
pygame.quit() 

正如你所看到的操作:

if button == buttons[0]: 
screen.fill(0,0,0) 

我目前正在使用什么。 if 语句工作正常,iv 测试了它的打印操作反馈,但我无法使用 Pygame 函数。

请您参考如下方法:

问题是由

引起的
screen.fill(0,0,0) 

因为 pygame.Surface.fill() 的第二个参数假定为矩形(例如 pygame.Rect ),它将填充限制在特定区域。

pygame.Surface.fill() 的第一个参数必须是 RGB 序列、RGBA 序列或颜色索引。

所以应该是

screen.fill( (0,0,0) ) 

或

screen.fill(0) 

按钮仍然是它们,因为它们在每一帧中都是连续绘制的:

for text, rect, colour, button_id in buttons: 
    pygame.draw.rect(screen, colour, rect) 
    screen.blit(text, rect) 

添加一个全局状态变量 (play),它在按下播放按钮时设置。在函数 on_button 中改变状态,使用 global statement更改全局变量 play 的值。根据状态绘制场景:

play = False 
def on_button(buttons): 
    global play  
    play = buttons[3] == 1 
    print(buttons[3], play) 
def game_intro(): 
 
    # [...] 
 
    if play: 
        screen.fill(0) 
 
        # [...]             
 
    else:  
        for text, rect, colour, button_id in buttons: 
            pygame.draw.rect(screen, colour, rect) 
            screen.blit(text, rect) 
 
    pygame.display.flip() 
    clock.tick(15)