当game_state是pygame中的“游戏”时,您如何编写想要发生的事情?

因此,我问您如何制作主菜单窗口,游戏窗口,按钮等。好的,完成了。

import pygame
from pygame.locals import *

pygame.init()

win = pygame.display.set_mode((800, 600))
win.fill((0, 180, 210))

pygame.display.set_caption("Baloon War!")
icon = pygame.image.load("Baloon war icon.png")
pygame.display.set_icon(icon)

class button():
    def __init__(self, color, x, y, width, height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self, win, outline=None):
        # Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

        pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

        if self.text != '':
            font = pygame.font.SysFont('comicsans', 60)
            text = font.render(self.text, 1, (0, 0, 0))
            win.blit(text, (self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))

    def isOver(self, pos):
        # Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False

def redrawMenuWindow():
    win.fill((0, 255, 110))
    greenButton.draw(win, (0, 0, 0))
    redButton.draw(win, (0, 0, 0))

def redrawGameWindow():
    win.fill((0, 180, 210))

greenButton = button((0, 255, 0), 280, 255, 250, 100, "Start")
redButton = button ((255, 0, 0), 280, 380, 250, 100, "Quit")

game_state = "menu"
run = True
while run:
    if game_state == "menu":
        redrawMenuWindow()
    elif game_state == "game":
        redrawGameWindow()
    pygame.display.update()

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if greenButton.isOver(pos):
                print("clicked the button")
                game_state = "game"
            if redButton.isOver(pos):
                print("clicked the 2button")
                run = False
                pygame.quit()
                quit()


        if event.type == pygame.MOUSEMOTION:
            if greenButton.isOver(pos):
                greenButton.color = (105, 105, 105)
            else:
                greenButton.color = (0, 255, 0)
            if redButton.isOver(pos):
                redButton.color = (105, 105, 105)
            else:
                redButton.color = (255, 0, 0)

So then I wanted to make my main game part. After pressing the Start/greenButton the game_state is "game". But how do I write what the game should be like, where???? I didn't know how so I wrote this:

game_state = "game"
while game_state is "game":

    redrawGameWindow():

但这是行不通的。在座的任何人都可以解释我该怎么做?

评论
  • nnam
    nnam 回复

    You have to put the game implementation in redrawGameWindow. You don't need any extra loop.

    You have to draw the scene dependent on the state of the game redrawMenuWindowin redrawGameWindow. Note you can have more than 2 game states, then you'll need more functions, which draw the scene dependent on the state.

    def redrawMenuWindow():
        win.fill((0, 255, 110))
    
        # draw menu
        # [...]
    
    def redrawGameWindow():
        win.fill((0, 180, 210))
    
        # draw main game part
        # [...]
    
    game_state = "menu"
    run = True
    while run:
    
        if game_state == "menu":
            redrawMenuWindow()
        elif game_state == "game":
            redrawGameWindow()
        # elif game_state == "gameover":
        #    redrawGameoverWindow()  
    
        pygame.display.update()
    
        # [...]
    

    并且您必须根据游戏状态来处理事件:

    while run:
    
        # [...]
    
        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
    
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                quit()
    
            if game_state == "menu":
    
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if greenButton.isOver(pos):
                        print("clicked the button")
                        game_state = "game"
                    if redButton.isOver(pos):
                        print("clicked the 2button")
                        run = False
                # [...]
    
    
            elif game_state == "game":
    
               # handel main game events
               # [...]