Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当游戏状态为“时,你如何写下你想要发生的事情?”;游戏“;在游戏中?_Python_Pygame - Fatal编程技术网

Python 当游戏状态为“时,你如何写下你想要发生的事情?”;游戏“;在游戏中?

Python 当游戏状态为“时,你如何写下你想要发生的事情?”;游戏“;在游戏中?,python,pygame,Python,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("

我问过你如何制作主菜单窗口、游戏窗口、按钮等等。好的,完成了。这是迄今为止的代码:

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)

但这根本不起作用。这里有人能告诉我怎么做吗?

你必须把游戏实现放在
重画游戏窗口中。你不需要任何额外的循环

您必须根据游戏状态绘制场景
redrawMenuWindowin
redrawGameWindow
。请注意,您可以有两个以上的游戏状态,然后您将需要更多的函数,这些函数根据状态绘制场景

def redrawMenuWindow():
胜利填充((0,255,110))
#绘图菜单
# [...]
def重画游戏窗口():
胜利填充((0,180,210))
#平局主要比赛部分
# [...]
游戏状态=“菜单”
运行=真
运行时:
如果游戏状态==“菜单”:
重画Menuwindow()
elif game_state==“game”:
重画游戏窗口()
#elif game_state==“gameover”:
#重画
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
           # [...]
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
           # [...]