Запустите игру, когда пробел будет нажат (модуль Turtle) - PullRequest
1 голос
/ 12 февраля 2020

Я пытался закодировать мини-игру Pong, используя Turtle Graphics. Кажется, все работает отлично, за исключением начала. Я хочу, чтобы основной l oop начал повторяться ТОЛЬКО после нажатия клавиши пробела. Вот часть, с которой у меня были проблемы:

## Start the game
start = False

def start_game():
    startmessage.clear()   #This is a turtle i created to show the message "Press SPACE to start"
    start = True
    ball_start()    #This is a function i created to get the ball moving

wn.onkeypress(start_game, "space")

## Main loop    
while start == True:  

Я включил .listen () и .mainl oop (), так что это не проблема. Полный код ниже. Если я запускаю программу с этим кодом, это то, что он делает:

  1. Он просит пользователя ввести максимальное количество баллов. Появляется черное окно, также отображается таблица результатов, но по какой-то причине граница отсутствует.
  2. Когда в оболочке вводится значение максимального количества очков, в черном отображается сообщение «Нажмите клавишу ПРОБЕЛ». screen.
  3. Как только нажимается пробел, это сообщение исчезает и больше ничего не происходит.

Вот полный код на случай, если я что-то упустил:

import turtle
import random
import time



## Screen setup
wn = turtle.Screen()
wn.title("Pong!")
wn.bgcolor("black")
wn.setup(width = 900, height = 700)

wn.tracer(0)


# Border
collisions = 0

border = turtle.Turtle()
border.penup()
border.color("white")
border.setposition(-400,-300)
border.pendown()
for side in range(2):
    border.forward(800)
    border.left(90) 
    border.forward(600)
    border.left(90)

border.hideturtle()


# Scores
scoreA = 0
scoreB = 0

score_marker = turtle.Turtle()
score_marker.shape("blank")
score_marker.color("yellow")
score_marker.speed(0)
score_marker.penup()
score_marker.setposition(0, 310)
score_marker.pendown()
score_marker.write("Player A : {}       Player B:  {}".format(scoreA,scoreB), align = "center", font = ("Courier", 20, "bold"))


# Select number of points
maxpoints = int(input("Enter max points: "))
print("The player who first gets to {} points wins the game!".format(maxpoints))


# Start message
startmessage = turtle.Turtle()
startmessage.speed(0)
startmessage.color("white")
startmessage.shape("blank")
startmessage.penup()
startmessage.setposition(0,75)
startmessage.pendown()
startmessage.write("Press SPACE to start", align = "center", font = ("Courier", 20, "bold"))

# End message
endmessage = turtle.Turtle()
endmessage.speed(0)
endmessage.color("green")
endmessage.shape("blank")
endmessage.penup()
endmessage.setposition(0,0)
endmessage.pendown()


## Paddles

paddleB_speed = 25
paddleA_speed = paddleB_speed

wn.listen()

# Paddle A
paddleA = turtle.Turtle()
paddleA.speed(0)
paddleA.shape("square")
paddleA.color("white")
paddleA.shapesize(3.2,0.7)

paddleA.penup()
paddleA.goto(-350,0)


# Paddle A movement
def a_up():
    y = paddleA.ycor() + paddleA_speed
    paddleA.sety(y)

def a_down():
    y = paddleA.ycor() - paddleA_speed
    paddleA.sety(y)

wn.onkeypress(a_up,"w")
wn.onkeypress(a_down,"s")


# Paddle B
paddleB = turtle.Turtle()
paddleB.speed(0)
paddleB.shape("square")
paddleB.color("white")
paddleB.shapesize(3.2,0.7)

paddleB.penup()
paddleB.goto(350,0)


# Paddle B movement
def b_up():
    y = paddleB.ycor() + paddleB_speed
    paddleB.sety(y)

def b_down():
    y = paddleB.ycor() - paddleB_speed
    paddleB.sety(y)

wn.onkeypress(b_up,"Up")
wn.onkeypress(b_down,"Down")

## Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.shapesize(0.5,0.5)

ball.penup()
ball.setposition(0,0)

ballspeed = 0.3
ballspeed_increase = 0.01


# Ball starting movement
def ball_start():
    angle_ranges = list(range(30,60)) + list(range(120,150)) + list(range(210,240)) + list(range(300,330))
    angle = random.choice(angle_ranges)
    ball.setheading(angle)

ball_start()


## Exit the game
def exit_game():
    wn.bye()


## Start the game
start = False

def start_game():
    startmessage.clear()
    start = True
    ball_start()

wn.onkeypress(start_game, "space")



## Main loop

while start == True:    

    wn.update()

    ### MOVEMENT

    ## Ball movement
    ball.forward(ballspeed)


    ### COLLISIONS

    ## Paddles and border

    # Paddle A
    if paddleA.ycor() > 268:
        paddleA.sety(268)

    if paddleA.ycor() < -268:
        paddleA.sety(-268)

    # Paddle B  
    if paddleB.ycor() > 268:
        paddleB.sety(268)

    if paddleB.ycor() < -268:
        paddleB.sety(-268)


    ## Ball and paddles
    if paddleA.distance(ball) <= 10:

        collisions += 1

        #direction = ball.heading()
        #ball.setheading(180 - random.randint(0,30) -  direction)
        ball.setheading(random.randint(110,250) + 180)
        ball.forward(ballspeed + ballspeed_increase)

    if paddleB.distance(ball) <= 10:

        collisions += 1

        #direction = ball.heading()
        #ball.setheading(180 - random.randint(0,30) -  direction)
        ball.setheading(random.randint(110,250))
        ball.forward(ballspeed + ballspeed_increase)


    ## Ball and border

    # Top and bottom borders
    if ball.ycor() < - 296 or ball.ycor() > 296:

        collisions += 1

        direction = ball.heading()
        ball.setheading(360 - direction)
        ball.forward(ballspeed + ballspeed_increase)

    # Left and right borders
    if ball.xcor() < -396:  #--> Player B wins the point
        delay = 0.1
        time.sleep(1)
        collisions += 1

        ball.setposition(0,0)

        scoreB += 1
        score_marker.clear()
        score_marker.write("Player A : {}       Player B:  {}".format(scoreA,scoreB), align = "center", font = ("Courier", 20, "bold"))
        ball_start()


    if ball.xcor() > 396:  #--> Player A wins the poin
        delay = 0.1
        time.sleep(1)
        collisions += 1

        ball.setposition(0,0)

        scoreA += 1
        score_marker.clear()
        score_marker.write("Player A : {}       Player B:  {}".format(scoreA,scoreB), align = "center", font = ("Courier", 20, "bold"))
        ball_start()


    # End of the game
    if scoreA == maxpoints or scoreB == maxpoints:
        if scoreA == maxpoints:
            print("Player A wins the game!")
            endmessage.write("Player A wins the game!\nA scored {} points\nB scored {} points".format(scoreA,scoreB), align = "center", font = ("Courier", 18, "bold"))
            time.sleep(1)
            endmessage.penup()
            endmessage.setposition(0,-100)
            endmessage.pendown()
            endmessage.color("red")
            endmessage.write("Press ESC to exit game", align = "center", font = ("Courier", 14, "normal"))
            wn.onkeypress(exit_game, "Escape")

            break

        if scoreB == maxpoints:
            print("Player B wins the game!")
            endmessage.write("Player B wins the game!\nA scored {} points\nB scored {} points".format(scoreA,scoreB), align = "center", font = ("Courier", 18, "bold"))
            time.sleep(1)
            endmessage.penup()
            endmessage.setposition(0,-100)
            endmessage.pendown()
            endmessage.color("red")
            endmessage.write("Press ESC to exit game", align = "center", font = ("Courier", 14, "normal"))
            wn.onkeypress(exit_game, "Escape")

            break


wn.mainloop()

Ответы [ 2 ]

0 голосов
/ 12 февраля 2020

Этот код выглядит как будто он сбрасывает start, но это не так - он устанавливает локальный start и игнорирует global start:

start = False

def start_game():
    startmessage.clear()
    start = True
    ball_start()

Вам необходим оператор global:

start = False

def start_game():
    global start
    startmessage.clear()
    start = True
    ball_start()

Вышеуказанное необходимо, но недостаточно. Следующая проблема - способ использования верхнего уровня while l oop вместо события таймера. То, как эта игра должна запускаться и работать, будет выглядеть примерно так:

from turtle import Screen, Turtle

running = False

def start_game():
    global running

    start_message.clear()
    running = True
    ball_start()

def ball_start():
    start_message.write("We've started the game!", align="center", font=("Courier", 20, "bold"))
    # what really ball_start() really does replaces the above...

def move_one_step():

    if running:

        screen.update()

        # ...

    screen.ontimer(move_one_step, 100)

screen = Screen()

start_message = Turtle()
start_message.hideturtle()
start_message.penup()
start_message.sety(75)
start_message.write("Press SPACE to start", align="center", font=("Courier", 20, "bold"))

screen.onkeypress(start_game, 'space')
screen.listen()

move_one_step()

screen.mainloop()
0 голосов
/ 12 февраля 2020

Я думаю, это может быть способ, которым вы использовали значение клавиши пробела. Вы ставите в космос вместо возврата.

wn.onkeypress(start_game, "return")
...