0

我正在尝试在我的乒乓球游戏上添加一个菜单,但是当我尝试加载程序时,该窗口没有显示,也没有看到任何错误,所以我不知道问题出在哪里。如果缺少某些内容,这是我的完整代码,因此可以看到。游戏运行正常,但是当添加菜单时,它通常会停止运行。

import sys
import pygame
pygame.init()
pygame.font.get_fonts()
mainClock = pygame.time.Clock()
# menu
def draw_text(text, font, color, surface, x, y):
    textobj = font.render(text, 1, color)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)
click = False
def main_menu():
    while True:
        win.fill((0, 0, 0))
        draw_text('main menu', ('arial'), (255, 255, 255), win, 20, 20)
        mx, my = pygame.mouse.get_pos()
        button_1 = pygame.Rect(50, 100, 200, 50)
        button_2 = pygame.Rect(50, 200, 200, 50)
        if button_1.collidepoint((mx, my)):
            if click:
                game()
        if button_2.collidepoint((mx, my)):
            if click:
                options()
        pygame.draw.rect(win, (255, 0, 0), button_1)
        pygame.draw.rect(win, (255, 0, 0), button_2)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    click = True
        pygame.display.update()
def options():
    running = True
    while running:
        win.fill((0, 0, 0))

        draw_text('options', "arial", (255, 255, 255), win, 20, 20)
# color
white = (255, 255, 255)
black = (0, 0, 0)
# screen
win = pygame.display.set_mode((750, 500))
pygame.display.set_caption('PONG')
icon = pygame.image.load("lig.png")
pygame.display.set_icon(icon)
# players
class Paddle1(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([10, 75])
        self.image.fill(white)
        self.rect = self.image.get_rect()
        self.points = 0
class Paddle2(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([10, 75])
        self.image.fill(white)
        self.rect = self.image.get_rect()
        self.points = 0
paddle1 = Paddle1()
paddle1.rect.x = 25
paddle1.rect.y = 225
paddle2 = Paddle2()
paddle2.rect.x = 715
paddle2.rect.y = 225
running = True
paddle_speed = 7
# ball
class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([10, 10])
        self.image.fill(white)
        self.rect = self.image.get_rect()
        self.speed = 6
        self.dx = 2
        self.dy = 2
pong = Ball()
pong.rect.x = 375
pong.rect.y = 250
all_sprites = pygame.sprite.Group()
all_sprites.add(paddle1, paddle2, pong)
def redraw():
    win.fill(black)
    font = pygame.font.SysFont('Arial', 30)
    text = font.render('PONG', False, white)
    textRect = text.get_rect()
    textRect.center = (750 // 2, 25)
    win.blit(text, textRect)
    p1_score = font.render(str(paddle1.points), False, white)
    p1Rect = p1_score.get_rect()
    p1Rect.center = (50, 50)
    win.blit(p1_score, p1Rect)
    p2_score = font.render(str(paddle2.points), False, white)
    p2Rect = p2_score.get_rect()
    p2Rect.center = (700, 50)
    win.blit(p2_score, p2Rect)
        all_sprites.draw(win)
    pygame.display.update()
def game():
    while running:
        pygame.time.delay(30)
        win.fill((0, 0, 0))
        for event in pygame.event.get():
            key = pygame.key.get_pressed()
        if key[pygame.K_w]:
            paddle1.rect.y += -paddle_speed
        if key[pygame.K_s]:
            paddle1.rect.y += paddle_speed
        if key[pygame.K_i]:
            paddle2.rect.y += -paddle_speed
        if key[pygame.K_k]:
            paddle2.rect.y += paddle_speed
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            pong.rect.x += pong.speed * pong.dx
        pong.rect.y += pong.speed * pong.dy
        if pong.rect.y > 490:
            pong.dy = -1
        if pong.rect.y < 1:
            pong.dy = 1
        if pong.rect.x > 740:
            pong.rect.x, pong.rect.y = 375, 250
            pong.dx = -1
            paddle1.points += 1
        if pong.rect.x < 1:
            pong.rect.x, pong.rect.y = 375, 250
            pong.dx = 1
            paddle2.points += 1
            if paddle1.rect.colliderect(pong.rect):
            pong.dx = 1
        if paddle2.rect.colliderect(pong.rect):
            pong.dx = -1
            if paddle1.rect.y <= 0:
            paddle1.rect.y = 0
            if paddle1.rect.y >= 425:
            paddle1.rect.y = 425
        if paddle2.rect.y <= 0:
            paddle2.rect.y = 0
        if paddle2.rect.y >= 425:
            paddle2.rect.y = 425
        redraw()
        pygame.display.update()
4

0 回答 0