我一直试图解决这个问题一段时间了,但我似乎无法让它正常工作。我正在尝试用python进行乒乓球游戏并试图在三场比赛中发挥最佳效果。但是,当我在每轮结束后重启游戏时遇到麻烦,我无法找到保持wins1和wins2变量值的方法。我在开始时将它们设置为0并且我知道这不会保留该值,因为每次调用pongyay()函数时它都会重置为0,但我保留它以便不得到任何其他错误。如果有人能给出关于如何保留这些内容的良好建议,但仍然将它们保持在我运行游戏的循环中,那将非常感激。这是代码:
from tkinter import *
import random
import time
import subprocess
import sys
tk = Tk()
tk.title("Epic Game of Destiny") # Title of the window
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
wins1 = 0
wins2 = 0
def pongyay():
canvas = Canvas(tk, width=700, height=400, bd=0, highlightthickness=0) #canvas attributes
canvas.configure(background='black')
canvas.pack()
tk.update()
class Rectangle:
def __init__(self, canvas, color):
self.canvas = canvas
self.paddle = paddle
self.paddle2 = paddle2
self.id = self.canvas.create_rectangle(6, 20, 0, 0, fill=color)
class Brik:
def __init__(self, canvas, color):
self.canvas = canvas
self.paddle = paddle
self.paddle2 = paddle2
self.id = self.canvas.create_rectangle(6, 30, 0, 0, fill=color)
class PowerUp: #class that creates the ball and assigns its attributes
def __init__(self, canvas, color):
self.canvas = canvas
self.paddle = paddle
self.paddle2 = paddle2
self.id = canvas.create_rectangle(10, 10, 10, 10, fill=color) #attributes, using self.id, which allows the detection of the current object self.id is
self.canvas.move(self.id, 400, 400) #starting position
def draw(self): #how the powerup bounces and reacts to its surroundings
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 2
if self.hit_paddle(pos) == True:
canvas.move(self.id, 1000, 1000)
if self.hit_paddle2(pos) == True:
canvas.move(self.id, 1000, 1000)
if pos[3] >= self.canvas_height: #places the powerup on the canvas and detects where the edges are, meaning that when the powerup hits the edge it will bounce off
self.y = -2
if pos[0] <= 0:
self.x = 2
class Ball: #class that creates the ball and assigns its attributes
def __init__(self, canvas, color):
self.canvas = canvas
self.paddle = paddle
self.paddle2 = paddle2
self.id = canvas.create_rectangle(10, 10, 25, 25, fill=color) #ball attributes, using self.id, which allows the detection of the current object self.id is
self.canvas.move(self.id, 330, 200)
starts = [-2, 2]
start2 = [-1, 1]
random.shuffle(starts)
random.shuffle(start2)#ball starting position
self.x = starts[0]
self.y = start2[0]
self.canvas_height = self.canvas.winfo_height() #gets size of canvas in order to allow ball to bounce off the walls
self.canvas_width = self.canvas.winfo_width()
self.side1 = False
self.side2 = False
def hit_paddle(self, pos):
paddle_pos = self.canvas.coords(self.paddle.id)
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]: # returns true or false when the ball does or does not hit the paddle, which then allows the ball to bounce off it
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
return True
return False
def hit_paddle2(self, pos):
paddle2_pos = self.canvas.coords(self.paddle2.id)
if pos[2] >= paddle2_pos[0] and pos[0] <= paddle2_pos[2]: # returns true or false when the ball does or does not hit the paddle, which then allows the ball to bounce off it
if pos[3] >= paddle2_pos[1] and pos[3] <= paddle2_pos[3]:
return True
return False
def draw(self): #how the ball bounces and reacts to its surroundings
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 8
if self.hit_paddle(pos) == True:
self.x = -8
if self.hit_paddle2(pos) == True:
self.x = 8
if pos[3] >= self.canvas_height: #places the ball on the canvas and detects where the edges are, meaning that when the ball hits the edge it will bounce off
self.y = -8
if pos[0] <= 0:
self.side1 = True
self.x = 8
if pos[2] >= 700:
self.side2 = True
self.x = -8
if pos[0] >= 1:
self.side1 = False
if pos[2] <= 700:
self.side2 = False
class Paddle: #class to create the paddle and its attributes
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 15, 80, fill = color) #the paddles attributes, it is long and thin, and will be filled with the color specified
self.canvas.move(self.id, 50, 200)
self.y = 0
self.canvas_height = self.canvas.winfo_height()
self.canvas.bind_all('<KeyPress-Up>', self.turn_left) # keybindings that moves the paddle along the canvas
self.canvas.bind_all('<KeyPress-Down>', self.turn_right)
self.canvas.bind_all('<KeyRelease-Up>', self.stop_left) # keybindings that moves the paddle along the canvas
self.canvas.bind_all('<KeyRelease-Down>', self.stop_right)
def turn_left(self, evt):# when the left key is pressed, the paddle moves along at this speed
self.y = -9
def turn_right(self, evt): # when the right key is pressed, the paddle moves along at this speed
self.y = 9
def stop_left(self, evt):# when the left key is released, the paddle will stop moving
self.y = 0
def stop_right(self, evt): # when the right key is released, the paddle will stop moving
self.y = 0
def draw(self):
pos = self.canvas.coords(self.id) #this detects the side of the canvas and stops the paddle from moving past it
if pos[1] <= 0:
self.y = 1
elif pos[3] >= self.canvas_height:
self.y = -1
self.canvas.move(self.id, 0, self.y)
class Paddle2: #class to create the paddle and its attributes
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 15, 80, fill = color) #the paddles attributes, it is long and thin, and will be filled with the color specified
self.canvas.move(self.id, 50, 200)
self.y = 0
self.canvas_height = self.canvas.winfo_height()
self.canvas.bind_all('<KeyPress-a>', self.turn_left2) # keybindings that moves the paddle along the canvas
self.canvas.bind_all('<KeyPress-z>', self.turn_right2)
self.canvas.bind_all('<KeyRelease-a>', self.stop_left2) # keybindings that moves the paddle along the canvas
self.canvas.bind_all('<KeyRelease-z>', self.stop_right2)
def turn_left2(self, evt):# when the left key is pressed, the paddle moves along at this speed
self.y = -9
def turn_right2(self, evt): # when the right key is pressed, the paddle moves along at this speed
self.y = 9
def stop_left2(self, evt):# when the left key is released, the paddle will stop moving
self.y = 0
def stop_right2(self, evt): # when the right key is released, the paddle will stop moving
self.y = 0
def draw(self):
pos = self.canvas.coords(self.id) #this detects the side of the canvas and stops the paddle from moving past it
if pos[1] <= 0:
self.y = 1
elif pos[3] >= self.canvas_height:
self.y = -1
self.canvas.move(self.id, 0, self.y)
paddle = Paddle(canvas, 'white') #the object of the paddle that will be instantiated
paddle2 = Paddle2(canvas, 'white')
ball = Ball(canvas, 'white') #the object of the ball that will be instantiated
powerup = PowerUp(canvas, 'white')
paddle.draw()
paddle2.draw()
brick1 = Brik(canvas, 'white')
brick5 = Brik(canvas, 'white')
brick1.canvas.move(brick1.id, 450, 200)
brick5.canvas.move(brick5.id, 250, 200)
rectangle1 = Rectangle(canvas, 'white')
rectangle2 = Rectangle(canvas, 'white')
rectangle3 = Rectangle(canvas, 'white')
rectangle4 = Rectangle(canvas, 'white')
rectangle5 = Rectangle(canvas, 'white')
rectangle6 = Rectangle(canvas, 'white')
rectangle7 = Rectangle(canvas, 'white')
rectangle8 = Rectangle(canvas, 'white')
rectangle9 = Rectangle(canvas, 'white')
rectangle10 = Rectangle(canvas, 'white')
rectangle11 = Rectangle(canvas, 'white')
rectangle1.canvas.move(rectangle1.id, 350, 380)
rectangle2.canvas.move(rectangle2.id, 350, 342)
rectangle3.canvas.move(rectangle3.id, 350, 304)
rectangle4.canvas.move(rectangle4.id, 350, 266)
rectangle5.canvas.move(rectangle5.id, 350, 228)
rectangle6.canvas.move(rectangle6.id, 350, 190)
rectangle7.canvas.move(rectangle7.id, 350, 152)
rectangle8.canvas.move(rectangle8.id, 350, 114)
rectangle9.canvas.move(rectangle9.id, 350, 76)
rectangle10.canvas.move(rectangle10.id, 350, 38)
rectangle11.canvas.move(rectangle11.id, 350, 0)
paddle2.canvas.move(paddle.id, 583, 0)
tk.update_idletasks
tk.update()
global score1
global score2
score1 = 0
score2 = 0
def restart():
result = messagebox.askyesno("Revenge?","Would you like to restart?")
if result == True:
score1 = 0
score2 = 0
canvas.destroy()
pongyay()
else:
tk.destroy()
while 1:
tk.update_idletasks() #loop that and allows it to move, by updating tkinter every 0.01 second
tk.update()
time.sleep(0.0000001)
ball.draw()
paddle.draw()
paddle2.draw()
if ball.side1 == True:
score2 = score2 + 1
print(score2)
if score2 == 1:
rc1 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc1, 365, 20)
if score2 == 2:
rc2 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc2, 385, 20)
if score2 == 3:
rc3 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc3, 405, 20)
if score2 == 4:
rc4 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc4, 425, 20)
if score2 == 5:
rc5 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc5, 445, 20)
if score2 == 6:
rc6 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc6, 465, 20)
if score2 == 7:
rc7 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc7, 485, 20)
if score2 == 8:
rc8 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc8, 505, 20)
if score2 == 9:
rc9 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc9, 525, 20)
if score2 == 10:
rc10 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc10, 545, 20)
elif ball.side2 == True:
score1 = score1 + 1
print(score1)
if score1 == 1:
rc1 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc1, 320, 20)
if score1 == 2:
rc2 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc2, 300, 20)
if score1 == 3:
rc3 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc3, 280, 20)
if score1 == 4:
rc4 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc4, 260, 20)
if score1 == 5:
rc5 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc5, 240, 20)
if score1 == 6:
rc6 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc6, 220, 20)
if score1 == 7:
rc7 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc7, 200, 20)
if score1 == 8:
rc8 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc8, 180, 20)
if score1 == 9:
rc9 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc9, 160, 20)
if score1 == 10:
rc10 = canvas.create_rectangle(5, 5, 15, 15, fill='white')
canvas.move(rc10, 140, 20)
if score1 == 10:
wins1 = 0
wins1 = wins1 + 1
print(wins1)
time.sleep(2)
score1 = 0
score2 = 0
canvas.destroy()
pongyay()
if wins1 == 1:
finalpoint = Label(tk, text='This Match Decides The Winner', fg="white", bg="black", font=("OCR A Std", 50))
finalpoint.pack()
finalpoint.place(x = 350, y = 200)
time.sleep(1.5)
finalpoint.destroy()
if wins1 == 2:
finalpoint1 = Label(tk, text='Player 1 Wins!', fg="white", bg="black", font=("OCR A Std", 50))
finalpoint1.pack()
finalpoint1.place(x = 350, y = 200)
restart()
if score2 == 10:
wins2 = 0
wins2 = wins2 + 1
print(wins2)
time.sleep(2)
score1 = 0
score2 = 0
canvas.destroy()
pongyay()
if wins2 == 1:
finalpoint = Label(tk, text='This Match Decides The Winner', fg="white", bg="black", font=("OCR A Std", 50))
finalpoint.pack()
finalpoint.place(x = 350, y = 200)
time.sleep(1.5)
finalpoint.destroy()
if wins2 == 2:
finalpoint2 = Label(tk, text='Player 2 Wins!', fg="white", bg="black", font=("OCR A Std", 50))
finalpoint2.pack()
finalpoint2.place(x = 350, y = 200)
restart()
pongyay()
问题出现的主要区域是评分底部。
再次感谢任何回答
的人答案 0 :(得分:1)
看看这两位:
wins1 = 0
wins1 = wins1 + 1
和
wins2 = 0
wins2 = wins2 + 1
在增加胜利计数之前,你需要擦除胜利计数。如果您想初始化胜利计数,请在循环外进行。
在代码顶部附近,您可以创建一对全局wins1
和wins2
变量;也许你打算在pongyay
里面使用那些?
tk.wm_attributes("-topmost", 1)
wins1 = 0
wins2 = 0
如果有,请从wins1 = 0
内删除wins2 = 0
和pongyay
行,并在global wins1
内声明global wins2
和pongyay
,以便功能正常使用全局变量而不是创建局部变量。