【发布时间】:2021-07-24 19:14:14
【问题描述】:
我是创建视频游戏的初学者,但我被一些我不理解的东西所困扰。我正在创建的游戏包括移动桨和击球,如下所示:
我正在使用海龟,我正在尝试编写桨和球之间的碰撞,但我不明白它是如何工作的。这是脚本:
import turtle
width,height = 800, 600
score = 0
wn = turtle.Screen()
wn.title('Breakout')
wn.bgcolor('black')
wn.setup(width,height)
wn.tracer()
# Paddle
paddle = turtle.Turtle()
paddle.speed(0)
paddle.shape('square')
paddle.shapesize(stretch_wid=5, stretch_len=1)
paddle.color('white')
paddle.penup()
paddle.left(90)
paddle.goto(0, -290)
# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape('square')
ball.color('white')
ball.penup()
ball.goto(0,0)
ballx = 3
bally = -3
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color('white')
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write('Score: 0', align='center', font=('Courier', 24, 'normal'))
# Paddle movement
def paddle_right():
x = paddle.xcor()
x -= 20
paddle.setx(x)
def paddle_left():
x = paddle.xcor()
x += 20
paddle.setx(x)
wn.listen()
wn.onkeypress(paddle_right, 'a')
wn.onkeypress(paddle_left, 'd')
while True:
wn.update()
ball.setx(ball.xcor() + ballx)
ball.sety(ball.ycor() + bally)
# Borders
if ball.xcor() > 390:
ball.setx(390)
ballx *= -1
if ball.xcor() < -390:
ball.setx(-390)
ballx *= -1
if ball.ycor() > 290:
ball.sety(290)
bally *= -1
if ball.ycor() < -290:
ball.goto(0, 0)
bally *= -1
score -= 1
pen.clear()
pen.write('Score: {}'.format(score), align='center', font=('Courier', 24, 'normal'))
# Paddle and ball collision
谁能帮我写下最后几行代码并向我解释一下——它是如何工作的?谢谢
【问题讨论】:
-
请说明您是否尝试过任何解决方案以及哪些解决方案(带有源代码)。
-
我已经尝试解决问题几天并尝试了很多代码。我试图从名为“通过构建五个游戏学习 Python - 完整课程”的教程中更改代码。这是 28 分钟的第一场比赛。
标签: python collision-detection python-3.7 python-turtle