我正在和乌龟一起打乒乓球。但是,分配是代码必须具有多线程。我无法正确应用多线程。
该代码在编辑器中未显示任何错误,但不起作用。我们如何解决此运行时错误?当编辑器中没有错误时,很难找到错误。
import turtle
from threading import *
from time import sleep
wn = turtle.Screen()
wn.title("Ping pong by Cagatay em")
wn.bgcolor("blue")
wn.setup(width=900, height=600)
wn.tracer(0) #oyunu hizlandirir silersen cok yavaslar
class PaddleFirst(Thread):
def __init__(self):
self.pen = turtle.Turtle()
self.pen.penup()
self.pen.speed(0)
self.pen.shape("square")
self.pen.shapesize(stretch_wid=5, stretch_len=1)
self.pen.penup()
self.pen.goto(-350, 0)
def run(self):
y = self.pen.ycor()
y += 20
self.pen.sety(y)
k = self.pen.ycor()
k -= 20
self.pen.sety(k)
class PaddleSecond(Thread):
def __init__(self):
self.pen = turtle.Turtle()
self.pen.penup()
self.pen.speed(0)
self.pen.shape("square")
self.pen.shapesize(stretch_wid=5, stretch_len=1)
self.pen.penup()
self.pen.goto(350, 0)
def run(self):
y = self.pen.ycor()
y += 20
self.pen.sety(y)
k = self.pen.ycor()
k -= 20
self.pen.sety(k)
class Ball(Thread):
def __init__(self):
self.pen = turtle.Turtle()
self.pen.penup()
self.pen.speed(0)
self.pen.shape("circle")
self.pen.color("red")
self.pen.penup()
self.pen.goto(0, 0)
self.pen.dx = 00.1
self.pen.dy = 00.1
def run(self):
self.pen.setx(self.pen.xcor() + self.pen.dx)
self.pen.sety(self.pen.ycor() + self.pen.dy)
if self.pen.ycor() > 290:
self.pen.sety(290)
self.pen.dy *= -1
if self.pen.ycor() < -290:
self.pen.sety(-290)
self.pen.dy *= -1
if self.pen.xcor() > 390:
self.pen.goto(0, 0)
self.pen.dx *= -1
if self.pen.xcor() < -390:
self.pen.goto(0, 0)
self.pen.dx *= -1
class Wall(Thread):
def run(self):
if ball.pen.xcor() > 340 and (ball.pen.ycor() < paddle2.pen.ycor() + 40 and ball.pen.ycor() > paddle2.pen.ycor() - 40):
ball.pen.dx *= -1
if ball.pen.xcor() < -340 and (ball.pen.ycor() < paddle1.pen.ycor() + 40 and ball.pen.ycor() > paddle1.pen.ycor() - 40):
ball.pen.dx *= -1
paddle1 = PaddleFirst()
paddle2 = PaddleSecond()
ball = Ball()
wall = Wall()
wn.listen()
wn.onkeypress(paddle1.run, "w")
wn.onkeypress(paddle1.run(), "s")
wn.onkeypress(paddle2.run(), "Up")
wn.onkeypress(paddle2.run, "Down")
while True:
wn.update() # everytime uptades the screen
ball.start()
sleep(0.2)
wall.start()
sleep(0.2)
paddle1.start()
sleep(0.2)
paddle2.start()
答案 0 :(得分:0)
我的猜测是,您收到的错误消息是由于您没有在__init__()
的子类中调用超类的Thread
方法。
当编辑器中没有错误时,很难找到错误。
您需要学习调试的技巧。
我无法正确应用多线程。
我认为这里有两个问题。首先,要对在tkinter之上构建的turtle使用线程,您需要使所有图形命令都通过主线程进行路由-辅助线程不应尝试直接修改屏幕。
第二,您的代码一团糟。除了线程之外,尚不清楚您是否了解对象编程,因为您拥有两个 identical 类,除了X坐标。这应该是一个带有初始化程序参数的类。目前尚不清楚您是否了解乌龟,因为wn.onkeypress(paddle1.run(), "s")
将永远无法工作。在代码正常工作之前,您可能不应该弄混tracer()
和update()
。而且主乌龟线程上不应该有while True:
。
我将您的代码拆开,并在下面将其重新组合在一起。只有球是单独的线程,(单个)桨类现在从Turtle
继承。主线程处理来自球形线程的图形命令以及标准的乌龟事件:
from turtle import Screen, Turtle
from threading import Thread, active_count
from queue import Queue
QUEUE_SIZE = 1
class Paddle(Turtle):
def __init__(self, xcor):
super().__init__(shape='square')
self.shapesize(stretch_wid=5, stretch_len=1)
self.speed('fastest')
self.penup()
self.setx(xcor)
self.dy = 10
def down(self):
y = self.ycor() - self.dy
if y > -250:
self.sety(y)
def up(self):
y = self.ycor() + self.dy
if y < 250:
self.sety(y)
class Ball(Thread):
def __init__(self):
super().__init__(daemon=True)
self.pen = Turtle('circle')
self.pen.speed('fastest')
self.pen.color('red')
self.pen.penup()
self.dx = 1
self.dy = 1
def run(self):
x, y = self.pen.position()
while True:
x += self.dx
y += self.dy
if x > 330 and (paddle2.ycor() - 60 < y < paddle2.ycor() + 60):
self.dx *= -1
elif x < -330 and (paddle1.ycor() - 60 < y < paddle1.ycor() + 60):
self.dx *= -1
if y > 290:
y = 290
self.dy *= -1
elif y < -290:
y = -290
self.dy *= -1
elif not -440 < x < 440:
x, y = 0, 0
self.dx *= -1
actions.put((self.pen.setposition, x, y))
def process_queue():
while not actions.empty():
action, *arguments = actions.get()
action(*arguments)
if active_count() > 1:
screen.ontimer(process_queue, 100)
screen = Screen()
screen.title("Ping pong rework by cdlane")
screen.bgcolor('blue')
screen.setup(width=900, height=600)
actions = Queue(QUEUE_SIZE)
paddle1 = Paddle(-350)
paddle2 = Paddle(350)
ball = Ball()
screen.onkeypress(paddle1.up, 'w')
screen.onkeypress(paddle1.down, 's')
screen.onkeypress(paddle2.up, 'Up')
screen.onkeypress(paddle2.down, 'Down')
screen.listen()
ball.start()
process_queue()
screen.mainloop()
代码仍然不完整,并且存在一些错误(例如无法彻底关闭),您需要进行一些工作。但它基本上以合理的方式玩游戏。