我对编程很陌生,并用python创建了一个简单的Pong游戏。
游戏当前正在运行,当玩家获得设定分数时,弹出屏幕会宣布获胜者。但是,如果用户选择再次玩游戏,我将无法重新启动游戏。
目前,该游戏仅继续进行,而上一场比赛结束后剩下的得分仍在继续。我试图在游戏逻辑运行之前实现一个用于初始化游戏的功能,但这没有帮助。
代码: https://pastebin.com/4SCxqqeY
# Pong Game
import turtle
import winsound
from tkinter import messagebox
from Projects.Pong import ball, paddle_b, paddle_a, pen, window
def init():
#Window Details
window = turtle.Screen()
window.title("Pong!")
window.bgcolor("black")
window.setup(width=800, height=600)
window.tracer(0)
#Score
player1_score = 0
player2_score = 0
#Paddles A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
# shape
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_len=1, stretch_wid=5)
# Dont draw line
paddle_a.penup()
# set start position
paddle_a.goto(-350, 0)
# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
# shape
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_len=1, stretch_wid=5)
# Dont draw line
paddle_b.penup()
# set start position
paddle_b.goto(350, 0)
# Ball
ball = turtle.Turtle()
ball.speed(0)
# shape
ball.shape("square")
ball.color("white")
# Dont draw line
ball.penup()
# set start position
ball.goto(0, 0)
ball.dx = 0.31
ball.dy = 0.31
# pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Player A: {} Player B: {}".format(player1_score, player2_score), align="center", font=("Courier", 24, "normal"))
#Function for movement of paddle a
def paddle_a_up():
#assgins the co-oridnate of paddle a to y
y = paddle_a.ycor()
y += 20
#sets paddle a new positon
paddle_a.sety(y)
def paddle_a_down():
#assgins the co-oridnate of paddle a to y
y = paddle_a.ycor()
y -= 20
#sets paddle a new positon
paddle_a.sety(y)
#Function for movement of paddle b
def paddle_b_up():
#assgins the co-oridnate of paddle a to y
y = paddle_b.ycor()
y += 20
#sets paddle a new positon
paddle_b.sety(y)
def paddle_b_down():
#assgins the co-oridnate of paddle a to y
y = paddle_b.ycor()
y -= 20
#sets paddle a new positon
paddle_b.sety(y)
# key bindings
window.listen()
window.onkeypress(paddle_a_up, "w")
window.onkeypress(paddle_a_down, "s")
window.onkeypress(paddle_b_up, "Up")
window.onkeypress(paddle_b_down, "Down")
play_game = True
def check_for_win_player_1():
if player1_score >= 2:
play_game = False
pop_up_msg()
def check_for_win_player_2():
global play_game
if player2_score >= 2:
play_game = False
pop_up_msg()
def pop_up_msg():
global play_game
if player1_score >= 2:
play_again = messagebox.askquestion(title="Result", message="Player 1 Wins! Play Again?")
if play_again == "no":
exit(0)
else:
play_game = True
elif player2_score >= 2:
play_again = messagebox.askquestion(title="Result", message="Player 2 Wins! Play Again?")
if play_again == "no":
exit(0)
else:
play_game = True
#Main Game Loop
def main():
while play_game == True:
init()
window.update()
#move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
#ball checking
#top
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
#bottom
if ball.ycor() <- 290:
ball.sety(-290)
ball.dy *= -1
winsound.PlaySound("bounce.wav", winsound.SND_ASYNC)
#left
if ball.xcor() < -390:
ball.goto(0,0)
ball.dx *= -1
player2_score += 1
pen.clear()
pen.write("Player A: {} Player B: {}".format(player1_score, player2_score), align="center", font=("Courier", 24, "normal"))
check_for_win_player_2()
#right
if ball.xcor() > 390:
ball.goto(0,0)
ball.dx *= -1
player1_score += 1
pen.clear()
pen.write("Player A: {} Player B: {}".format(player1_score, player2_score), align="center", font=("Courier", 24, "normal"))
check_for_win_player_1()
#paddle and ball collision
# left
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() - 50):
ball.setx(-340)
ball.dx *= -1
# right
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50):
ball.setx(340)
ball.dx *= -1
else:
break
main()