在pygame中"game" game_state时,你如何写你想发生的事情?



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

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)

所以后来我想让我的主要游戏部分。按下开始/绿色按钮后,game_state是"游戏"。但是我该如何写出游戏应该是什么样子,在哪里????我不知道怎么写的,所以我写了这个:

game_state = "game"
while game_state is "game":
redrawGameWindow():

但这行不通。这里有人可以解释我怎么做吗?

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

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

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

相关内容

最新更新