按任意键使用turtle或pygame在python中再次玩游戏

2024-05-21 01:19:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用turtle模块和pygame作为主菜单创建一场比赛 如何执行一个代码,在显示获胜图像后按任意键返回主菜单?我试着插入一个pygame事件键,它就是不起作用,所以我试着使用海龟,但它也不起作用

import turtle
from time import sleep  # Zzz!
from random import randint
import pygame
import os

# colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (51, 102, 51)
BROWN = (122, 49, 2)
GRAY = (94, 92, 148)
DBROWN = (78, 30, 0)
DGRAY = (31, 31, 47)
VIOLET = (99, 60, 181)
DVIOLET = (38, 24, 69)

pygame.init()

winHeight = 625
winWidth = 1000

win = pygame.display.set_mode((winWidth, winHeight))
mainmenu = pygame.image.load("m1.png")
houseterror = os.path.abspath("House_Of_Terror.ttf")


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


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

    if x + w > mouse[0] > x and y + h > mouse[1] > y:  # If mouse 0 (x coordinate) is less than  greater than
        pygame.draw.rect(win, hovercol, (x, y, w, h), border_radius=12)
        if click[0] == 1 and action != None:
            if action == "play":
                main()  
            elif action == "quit":
                pygame.quit()
                quit()
    else:
        pygame.draw.rect(win, nothovercol, (x, y, w, h),
                            border_radius=12)  # rect(name of screen, color, (x position, y position, width, height)

    label_menu = pygame.font.Font(houseterror, 30)
    textSurf, textRect = text_objects(msg, label_menu)
    textRect.center = ((x + (w / 2)), (y + (h / 2)))
    win.blit(textSurf, textRect)


def main_menu():
    menu = True

    while menu:

        for event in pygame.event.get():
            # print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        button("PLAY", 373, 469, 254, 41, VIOLET, DVIOLET, "play")
        button("QUIT", 373, 525, 254, 39, VIOLET, DVIOLET, "quit")


win.blit(mainmenu, (0, 0))

pygame.display.update()
clock = pygame.time.Clock()
clock.tick(60)


def main():
    # Making the screen and setting the background color

    window = turtle.Screen()
    window.setup(width=1000, height=625, startx=175, starty=40)
    window.bgpic("Main.png")
    window.register_shape("zom1.gif", shape=None)

    # Making the turtle to write the text
    text = turtle.Turtle()
    # Makes the text color white
    text.color('white')
    # So we don't see the movement
    text.speed(0)
    # So it doesnt draw a line
    text.penup()
    # So we don't see the turtle, just the text
    text.hideturtle()
    # Sets the position
    text.setpos(-95, 180)
    # Makes the text appear
    # We write the text here so the rules apply to the text
    text.write('Turtle Race', font=('Comic Sans Ms', 30, 'bold'))

    # Making the Finish Line
    square_size = 20
    stamp = 20
    finish_line = 150

    # Makes the square turtle that will draw the Finish Line
    square = turtle.Turtle()
    square.shape('square')
    square.penup()
    square.speed(0)

    # Makes the black squares
    square.color()

    # Draws the first 'column' of the Finish Line
    for i in range(9):
        # weird math
        # (finish_line) sets the x pos
        # (150 - (i * square_size * 3)) makes 'dashed' model of the finish line
        square.setpos(finish_line, (135 - (i * square_size * 2)))
        square.stamp()

    # Makes the white squares
    square.color('white')

    for i in range(9):
        # weird math
        # (finish_line) sets the x pos
        # (150 - (i * square_size * 3)) makes 'dashed' model of the finish line
        square.setpos(finish_line, (115 - (i * square_size * 2)))
        square.stamp()

    # Making the contestants
    # Contestant No.1

    turtle1 = turtle.Turtle()
    turtle1.speed(0)
    # Nice color
    turtle1.color('deepskyblue')
    # There's no turtle race whitout turtles ;)
    turtle1.shape("zom1.gif")
    turtle1.penup()
    turtle1.goto(-270, 165)

    # Contestant No.2

    turtle2 = turtle.Turtle()
    turtle2.speed(0)
    # Nice color
    turtle2.color('fuchsia')
    # There's no turtle race whitout turtles ;)
    turtle2.shape("zom1.gif")
    turtle2.penup()
    turtle2.goto(-270, 95)

    # Contestant No.3

    turtle3 = turtle.Turtle()
    turtle3.speed(0)
    # Nice color
    turtle3.color('gold')
    # There's no turtle race whitout turtles ;)
    turtle3.shape("zom1.gif")
    turtle3.penup()
    turtle3.goto(-270, 25)

    # Contestant No.4

    turtle4 = turtle.Turtle()
    turtle4.speed(0)
    # Nice color
    turtle4.color('darkorange')
    # There's no turtle race whitout turtles 
    turtle4.shape("zom1.gif")
    turtle4.penup()
    turtle4.goto(-270, -45)

    # Contestant No.5

    turtle5 = turtle.Turtle()
    turtle5.speed(0)
    # Nice color
    turtle5.color('darkorange')
    # There's no turtle race whitout turtles ;)
    turtle5.shape("zom1.gif")
    turtle5.penup()
    turtle5.goto(-270, -115)

    # The turtle have 2 secs to prepare before the race
    sleep(2)

    # START THE GAME ALREADY!
    # Ready! Seatdy! GO!
    for i in range(150):  # (a lot)
        turtle1.forward(randint(1, 5))  # move between 1 and 6 pixels at a time
        turtle2.forward(randint(1, 5))  # move between 1 and 6 pixels at a time
        turtle3.forward(randint(1, 5))  # move between 1 and 6 pixels at a time
        turtle4.forward(randint(1, 5))  # move between 1 and 6 pixels at a time
        turtle5.forward(randint(1, 5))  # move between 1 and 6 pixels at a time
        if turtle1.xcor() == 210 or turtle2.xcor() == 210 or turtle3.xcor() == 210 or turtle4.xcor() == 210 or turtle5.xcor() == 210:
            break

    # CELEBRATE THE WINNER
    if turtle1.xcor() > turtle2.xcor() and turtle1.xcor() > turtle3.xcor() and turtle1.xcor() > turtle4.xcor() and turtle1.xcor() > turtle5.xcor():
        window.bye()
        wonn = pygame.image.load("won.png")
        win.blit(wonn, (0, 0))
        pygame.display.update()

        window.onkey(main_menu, "space")
        window.listen()
    elif turtle2.xcor() > turtle1.xcor() and turtle2.xcor() > turtle3.xcor() and turtle2.xcor() > turtle4.xcor() and turtle2.xcor() > turtle5.xcor():
        window.bye()
        wonn = pygame.image.load("won.png")
        win.blit(wonn, (0, 0))
        pygame.display.update()

        window.onkey(main_menu, "space")
        window.listen()
    elif turtle3.xcor() > turtle1.xcor() and turtle3.xcor() > turtle2.xcor() and turtle3.xcor() > turtle4.xcor() and turtle3.xcor() > turtle5.xcor():
        window.bye()
        wonn = pygame.image.load("won.png")
        win.blit(wonn, (0, 0))
        pygame.display.update()

        window.onkey(main_menu, "space")
        window.listen()
    elif turtle4.xcor() > turtle1.xcor() and turtle3.xcor() > turtle2.xcor() and turtle3.xcor() > turtle4.xcor() and turtle4.xcor() > turtle5.xcor():
        window.bye()
        wonn = pygame.image.load("won.png")
        win.blit(wonn, (0, 0))
        pygame.display.update()

        window.onkey(main_menu, "space")
        window.listen()
    else:
        window.bye()
        wonn = pygame.image.load("won.png")
        win.blit(wonn, (0, 0))
        pygame.display.update()

        window.onkey(main_menu, "space")
        window.listen()


main_menu()
main()

Tags: andthetextwindowpygamecolormenuturtle