首页 > 解决方案 > 在pygame中单击按钮

问题描述

所以我创建了一个包含“开始”按钮和“退出”按钮的小菜单。显然我想要的是当我点击开始时,它开始游戏循环,当我点击退出时,它退出游戏。我已经编码了一些东西,但它不起作用。我认为问题在于我没有游戏循环和退出的功能。

这是整个代码:

import pygame


pygame.init()
win = pygame.display.set_mode((1200, 600))
pygame.display.set_caption("WALTHER")
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
WHITE = (255,255,255)
RED = (255, 0, 0)
GREEN = (13, 255, 0)
YELLOW = (0, 255, 20)
BRIGHT_YELLOW = (255, 255, 20)

player = pygame.Rect(40, 45, 30, 30)
vel = 4
vel_left = 5
vel_right = -5



class MovingRect(pygame.Rect):

def __init__(self, x, y, w, h, vel):
    # Call the __init__ method of the parent class.
    super().__init__(x, y, w, h)
    self.vel = vel

def update(self):
    self.x += self.vel  # Move.
    if self.right > 600 or self.left < 320:  # If it's not in this area.
        self.vel = -self.vel  # Invert the direction.

def text_objects(text, font):
textSurface = font.render(text, True, BLACK)
return textSurface, textSurface.get_rect()

def button(msg, x, y, w, h, ic, ac, action = None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

if x + w > mouse[0] > x and y + h  > mouse[1] > y:
    pygame.draw.rect(win, ac, (x, y, w, h))
    if click[0] == 1 and action != None:
        if action == "Quit":
            pygame.quit()
else:
    pygame.draw.rect(win, ic, (x, y, w, h))

smallText = pygame.font.Font("freesansbold.ttf",50)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)), (y+(h/2)))
win.blit(textSurf, textRect)


def game_intro():
intro = True

while intro:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    win.fill(WHITE)
    largeText2 = pygame.font.Font('freesansbold.ttf', 115)
    TextSurf, TextRect = text_objects("Red Square", largeText2)
    TextRect.center = ((600), (100))
    win.blit(TextSurf, TextRect)


    button("Start", 525, 250, 150, 60, YELLOW, BRIGHT_YELLOW)
    button("Quit", 525, 350, 150, 60, YELLOW, BRIGHT_YELLOW)


    pygame.display.update()
    clock.tick(15)

def message_display(text):
largeText = pygame.font.Font(None , 115)
win.blit(largeText.render(text, True, (RED)), (370,250))
pygame.display.update()
pygame.time.wait(2000)
pygame.quit()

rotatingrects = [
pygame.Rect(630, 300, 250, 20),
pygame.Rect(920, 300, 250, 20)
]

movingrects = [
MovingRect(320, 120, 30, 30, vel_left),
MovingRect(320, 240, 30, 30, vel_left),
MovingRect(320, 360, 30, 30, vel_left),
MovingRect(570, 180, 30, 30, vel_right),
MovingRect(570, 300, 30, 30, vel_right),
MovingRect(570, 420, 30, 30, vel_right)
]

walls = [
pygame.Rect(0, 0, 1200, 20), pygame.Rect(0, 0, 20, 600),
pygame.Rect(0, 580, 1200, 20), pygame.Rect(1180, 0, 20, 600),
pygame.Rect(300, 0, 20, 530), pygame.Rect(20, 100, 230, 20),
pygame.Rect(70, 200, 230, 20), pygame.Rect(20, 300, 230, 20),
pygame.Rect(70, 400, 230, 20), pygame.Rect(600, 100, 20, 500)
]

run = True
while run:
# Handle the events.
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False
game_intro()

keys = pygame.key.get_pressed()

# Update the player coordinates.
if keys[pygame.K_LEFT] and player.x > 0:
    player.x -= vel
if keys[pygame.K_RIGHT] and player.x < 1200 - player.width:
    player.x += vel
if keys[pygame.K_UP] and player.y > 0:
    player.y -= vel
if keys[pygame.K_DOWN] and player.y < 600 - player.height:
    player.y += vel

# Game logic for walls and moving objects
for wall in walls:
    # Check if the player rect collides with a wall rect.
    if player.colliderect(wall):
        print("Game over")
        message_display("Game Over")

for movingrect in movingrects:
    movingrect.update()  # Movement and bounds checking.
    if player.colliderect(movingrect):
        print("Game over")
        message_display("Game Over")

for rotatingrect in rotatingrects:
    if player.colliderect(rotatingrect):
        print("Game over")


# Drawing everything
win.fill(WHITE)
pygame.draw.rect(win, RED, player)
# Drawing walls and moving objects
for rotatingrect in rotatingrects:
    pygame.draw.rect(win, BLACK, rotatingrect)

for wall in walls:
    pygame.draw.rect(win, BLACK, wall)

for movingrect in movingrects:
    pygame.draw.rect(win, GREEN, movingrect)


pygame.display.update()
clock.tick(60)

pygame.quit()

我认为问题出在这里:

    if x + w > mouse[0] > x and y + h  > mouse[1] > y:
    pygame.draw.rect(win, ac, (x, y, w, h))
    if click[0] == 1 and action != None:
        if action == "Quit":
            pygame.quit()
    else:
    pygame.draw.rect(win, ic, (x, y, w, h))

我只想做 if action == "Start": 玩游戏 if action == "Quit": 退出游戏

标签: pythonpygame

解决方案


定义两个回调函数,您需要将它们作为action参数传递给button函数。

在该quit_game函数中,您可以调用pygame.quitsys.exit关闭窗口。start_game函数需要在函数中定义,因为我们必须在关键字的帮助下game_intro访问变量,并将其设置为,这样函数将被终止并开始主循环。intrononlocalFalsegame_intro

def button(msg, x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + w > mouse[0] > x and y + h  > mouse[1] > y:
        pygame.draw.rect(win, ac, (x, y, w, h))
        if click[0] == 1 and action is not None:
            action()  # Call the callback function.
    # etc. ...


def quit_game():
    pygame.quit()
    sys.exit()  # `import sys` at the top of the file.


def game_intro():
    intro = True

    def start_game():
        # Set the intro variable in the enclosing scope to False.
        nonlocal intro
        intro = False

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

        win.fill(WHITE)
        largeText2 = pygame.font.Font('freesansbold.ttf', 115)
        TextSurf, TextRect = text_objects("Red Square", largeText2)
        TextRect.center = ((600), (100))
        win.blit(TextSurf, TextRect)

        # Pass the two callback functions to `button`.
        button("Start", 525, 250, 150, 60, YELLOW, BRIGHT_YELLOW, start_game)
        button("Quit", 525, 350, 150, 60, YELLOW, BRIGHT_YELLOW, quit_game)

        pygame.display.update()
        clock.tick(15)

另外,不要game_intro在主 while 循环内调用。

game_intro()

run = True
while run:

推荐阅读