我是python和turtle模块的新手,正试图为课堂作业制作一个乒乓球游戏,但当我的球拍出现时,屏幕上一直在画一条线。我试过用示踪剂,但似乎没有效果。
很抱歉,如果我的代码有点乱,而且解决方案很简单,那我就被难住了。
#import turtle for the background of the game (where the game is going to be played on)
import turtle
#creating the main screen/background
screen = turtle.Screen()
screen.title("JMR_Hercules_Pong")
turtle.bgcolor("black")
screen.setup(width = 800, height = 600)
screen.tracer(0)
#Requires two paddles one on the left one on the right
#left paddle
left_pad = turtle.Turtle() ##This creates a shape/device over the turtle background
left_pad.speed(0)
left_pad.shape("square")
left_pad.color("green")
left_pad.shapesize(stretch_wid=5,stretch_len=0.5)
left_pad.setx(-350)
turtle.pensize(2)
turtle.penup()
#right paddle
right_pad = turtle.Turtle()
right_pad.speed(0)
right_pad.shape("square")
right_pad.color("green")
right_pad.shapesize(5,0.5)
right_pad.setx(350)#sets the x coordinate so that you paddle goes to that x coord, goto wasn't working
right_pad.sety(0)
turtle.pensize(2)
turtle.penup()
screen.update()
这里可能有两个问题。创建新的Turtle对象以绘制左桨和右桨时,默认情况下会在屏幕中心创建这些对象。首先,在使用.setx((移动Turtle对象之前,您需要调用.pompop((方法。这将停止从屏幕中心到边缘出现的线。另一件需要注意的事情是,在调用.pnpop((和.pensize((时,需要将实际的Turtle对象用于桨板(目前,您只是在引用乌龟模块本身(。
因此,我建议切换以下内容:
left_pad = turtle.Turtle() ##This creates a shape/device over the turtle background
left_pad.speed(0)
left_pad.shape("square")
left_pad.color("green")
left_pad.shapesize(stretch_wid=5,stretch_len=0.5)
left_pad.setx(-350)
turtle.pensize(2)
turtle.penup()
至
left_pad = turtle.Turtle() ##This creates a shape/device over the turtle background
left_pad.speed(0)
left_pad.shape("square")
left_pad.color("green")
left_pad.shapesize(stretch_wid=5,stretch_len=0.5)
left_pad.penup()
left_pad.setx(-350)
left_pad.pensize(2)
那么,理想情况下,你会想为正确的划桨做同样的事情。
关注问题"当我的球拍出现时,我一直在屏幕上画一条线;在放置拨杆之前提起笔。
# import turtle for the background of the game (where the game is going to be played on)
import turtle
# creating the main screen/background
screen = turtle.Screen()
screen.title("JMR_Hercules_Pong")
turtle.bgcolor("black")
screen.setup(width=800, height=600)
screen.tracer(0)
# Requires two paddles one on the left one on the right
# left paddle
left_pad = turtle.Turtle() ##This creates a shape/device over the turtle background
left_pad.penup() # Lift the pen before moving the pad
left_pad.speed(0)
left_pad.shape("square")
left_pad.color("green")
left_pad.shapesize(stretch_wid=5, stretch_len=0.5)
left_pad.setx(-350)
turtle.pensize(2)
# left_pad.penup()
# right paddle
right_pad = turtle.Turtle()
right_pad.penup() # Lift the pen before moving the pad
right_pad.speed(0)
right_pad.shape("square")
right_pad.color("green")
right_pad.shapesize(5, 0.5)
right_pad.setx(
350) # sets the x coordinate so that you paddle goes to that x coord, goto wasn't working
right_pad.sety(0)
turtle.pensize(2)
# right_pad.penup()
screen.update()