pygame中的再次播放按钮

时间:2018-08-16 13:55:09

标签: python pygame

我正在尝试创建一个按钮,当我撞墙时重新启动游戏。我设法做了一些事情,但是没有完全做到。当我当机时,会有一个屏幕向我显示“再次播放”按钮,当我单击它时它可以工作,但前提是我同时从墙上移动了Sprite。当我不再撞墙时,它会“重新启动”,但这不是我想要的。我想在崩溃时按下“再次播放”按钮,该按钮可以重置Sprite的位置,这样我通常就可以再次播放。

这是整个代码

import pygame
import sys

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):
        # Calling the __init__ method of the parent class
        super().__init__(x, y, w, h)
        self.vel = vel

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

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()

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 is not None:
            action()
    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 quit_game():
    pygame.quit()
    sys.exit()

def restart():
    restart = True

    def start_game2():
        nonlocal restart
        restart = False

    while restart:
        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, start_game2)
        button("Quit", 525, 350, 150, 60, YELLOW, BRIGHT_YELLOW, quit_game)

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


def game_intro():
    intro = True

    def start_game():
        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)


        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)

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)
    ]


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

    keys = pygame.key.get_pressed()

    # 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 rectangle collides with a wall rectangle
        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")

    # TO DO #
    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()

这是我的重启功能:

def restart():
restart = True

def start_game2():
    nonlocal restart
    restart = False

while restart:
    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, start_game2)
    button("Quit", 525, 350, 150, 60, YELLOW, BRIGHT_YELLOW, quit_game)

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

0 个答案:

没有答案