gpt4 book ai didi

python - 在pygame中点击按钮

转载 作者:行者123 更新时间:2023-11-28 17:04:39 25 4
gpt4 key购买 nike

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

完整代码如下:

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": 玩游戏 如果行动==“退出”: 退出游戏

最佳答案

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

quit_game函数中,可以调用pygame.quitsys.exit来关闭窗口。 start_game 函数需要在 game_intro 函数中定义,因为我们必须在 的帮助下访问 intro 变量nonlocal 关键字,并将其设置为 False,这样 game_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)

此外,不要在主 while 循环中调用 game_intro

game_intro()

run = True
while run:

关于python - 在pygame中点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51803264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com