gpt4 book ai didi

python - 我怎样才能重新开始我的 Pong 游戏并保持我的分数有效?

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:29 25 4
gpt4 key购买 nike

我一直在为我的第一个完整的 Python 项目开发 Pong 游戏,主要使用 tkinter 进行大部分操作。但是,我无法弄清楚如何使用我添加的重新启动按钮重新启动程序而不禁用左上角的分数计数器。以下文字是我的代码:

from tkinter import *
import random
import time
import pygame
import os, sys
import time

class Ball:

def __init__(self, canvas, paddle, color):
self.canvas = canvas
self.paddle = paddle
self.id = canvas.create_oval(10, 10, 25, 25, fill='red')
self.canvas.move(self.id, 245, 100)
starts = [-3, -2, -1, 1, 2, 3]
random.shuffle(starts)
self.x = starts[0]
self.y = -3
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
self.hit_bottom = False
self.score = 0

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]:
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
return True
return False

def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 3
if pos[3] >= self.canvas_height:
self.hit_bottom = True
if self.hit_paddle(pos) == True:
self.y = -3
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
if self.hit_paddle(pos) == True:
self.score += 1

class Paddle:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 150, 10, fill='blue')
self.canvas.move(self.id, 450, 750)
self.x = 0
self.y = 0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
self.canvas.bind_all('<KeyRelease-Left>', self.stop_left)
self.canvas.bind_all('<KeyRelease-Right>', self.stop_right)
self.canvas.bind_all('<KeyPress-Up>', self.turn_up)
self.canvas.bind_all('<KeyPress-Down>', self.turn_down)
self.canvas.bind_all('<KeyRelease-Up>', self.stop_up)
self.canvas.bind_all('<KeyRelease-Down>', self.stop_down)

def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[0] <= 0:
self.x = 0
elif pos[2] >= self.canvas_width:
self.x = 0
def turn_left(self, evt):
self.x = -3
def turn_right(self, evt):
self.x = 3
def stop_left(self, evt):
self.x = 0
def stop_right(self, evt):
self.x = 0
def turn_up(self, evt):
self.y = -0
def turn_down(self, evt):
self.y = 0
def stop_up(self, evt):
self.y = 0
def stop_down(self, evt):
self.y = 0
def restart ():
global paddle, ball
canvas.delete (ALL)
paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, 'red')
label = canvas.create_text(5, 5, anchor=NW, text="Score: 0", font=('Courier, 40'), fill='white')
canvas.itemconfig(label, text="Score: "+str(ball.score))

def main ():
global paddle, ball
tk.update()

paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, 'red')

while 1:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
if ball.hit_bottom == True:
canvas.create_text(525, 300, text='Game Over', fill='red', font=('Courier', 60))
canvas.itemconfig(label, text="Score: "+str(ball.score))

tk = Tk()
tk.title("User's Pong Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=1000, height=800, bd=0, highlightthickness=0)
canvas.configure(background='black')
canvas.grid (row = 0)
label = canvas.create_text(5, 5, anchor=NW, text="Score: 0", font=('Courier, 40'), fill='white')
Button = Button(tk, anchor=NE, text = "Start Over", command = restart)
Button.grid(row=1, column=1)

tk.configure(background='white')

main ()

最佳答案

替换:

canvas.itemconfig(label, text="Score: "+str(ball.score))

与:

while 1:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
if ball.hit_bottom == True:
canvas.create_text(525, 300, text='Game Over', fill='red', font=('Courier', 60))
canvas.itemconfig(label, text="Score: "+str(ball.score))

restart方法中。

<小时/>

我是如何调试它的:

  1. 复制并运行整个代码,尝试按钮,看到分数静态为 0
  2. 在脚本中搜索“Score”,查看脚本中的修改位置
  3. 注意到缺少 while,因此解决了它。

关于python - 我怎样才能重新开始我的 Pong 游戏并保持我的分数有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49216249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com