0
Follow
0
View

Alien invasion of the Earth game, start the game button can not be displayed, how to solve

deareffen 注册会员
2023-02-26 17:16

The following answers are quoted from GPT-3 model, please use them wisely:

import pygame
 
WIDTH = 800
HEIGHT = 800
FPS = 60
 
# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)    
 
# initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Alien Invasion")
clock = pygame.time.Clock()
 
class Button:
    def __init__(self, ai_game, words, x, y):
        self.screen = ai_game.screen
        self.rect = self.screen.get_rect()
        self.rect = pygame.Rect(0, 0, 100, 100)
        self.rect.x = x
        self.rect.y = y
        pygame.draw.rect(self.screen, RED, self.rect)
        self._prepare_words(words)
 
    def _prepare_words(self, words):
        font = pygame.font.Font(None, 100)
        msg = font.render(words, True, (12, 12, 21), (0, 0, 0))
        msg_rect = msg.get_rect()
        msg_rect.center = self.rect.center
        self.screen.blit(msg, msg_rect)
 
    def check_clicked(self, position):
        if self.rect.collidepoint(position):
            return True
        else:
            return False
 
def game_state():
    intro = True
    while intro:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = event.pos
                if button.check_clicked(mouse_pos):
                    print("Clicked!")
                    intro = False
        screen.fill(WHITE)
        button.show()
        pygame.display.update()
 
# Game loop
running = True
button = Button(ai_game, "Play", WIDTH/2-50, HEIGHT/2-50)
while running:
    game_state()
 
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False
 
 
pygame.quit()

Please accept my answer if it answers your question

QQ39974867 注册会员
2023-02-26 17:16

The Button class in this program implements a rectangular button, but does not show it. Your problem is how to make the buttons appear. There is nothing wrong with the code in the
Button class. The problem is that this class is not used in the main game loop. You need to add the following line of code to the game's main loop to display the button:
less




```Copy code
play_button = Button(screen, "Play")
这行代码会创建一个名为play_button的按钮,并将其显示在屏幕上。您需要将这行代码添加到游戏主循环的while循环中,具体位置应该是在屏幕刷新之前。例如:
蟒复制代码
    # 检查事件
    gf.check_events()

    # 更新游戏状态
    ...

    # 绘制屏幕
    ...

    # 显示按钮
    play_button = Button(screen, "Play")

    # 刷新屏幕
    pygame.display.flip()
注意,这个程序中的按钮没有实现任何功能,因此点击按钮并不会有任何效果。如果您想为按钮添加功能,您需要在Button类中添加一个click方法,并在check_events方法中检查鼠标点击事件是否发生在按钮上。
- 

1. ****


About the Author

Question Info

Publish Time
2023-02-26 17:16
Update Time
2023-02-26 17:16