《Python学习笔记》
近几日学习了Python大神的教学课程,自己动手实践,巩固学习的内容,决定对弹力球小游戏下手(文末源码奉上)。
进入正题:
避免自己忘记下一步干啥,将小游戏分8步完成:
#Part1 Game UI
#Part2 Paddle
#Part3 Ball
#Part4 Move the paddle
#Part5 Move the ball
#Part6 Border check
#Part7 Paddle and Ball collisions
#Part8 Score
先看下这个简陋的游戏吧,没错就是这个界面

Part1: Game UI (游戏界面简陋无匹啊)
import turtle
#UI
wn = turtle.Screen()
wn.title("Rebound Ball Game @TimeOld")
wn.bgcolor("black")
wn.setup(width=500,height=600)
wn.tracer(0)
#Main
while True:
wn.udpate()
Part2 Paddle(接球板,名字好听)
#Paddle
paddle = turtle.Turtle()
paddle.speed(0)
paddle.shape("square")
paddle.color("white")
paddle.shapesize(1,5)
paddle.penup()
paddle.goto(0,-260)
Part3 Ball(小球,不是伽利略的那个摆球啊喂)
#Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0,0)
Part4 Move the paddle (球板动起来喽)
#Function
def paddle_left():
x = paddle.xcor()
x -= 20
paddle.setx(x)
def paddle_right():
x = paddle.xcor()
x += 20
paddle.setx(x)
#Keyboard binding
wn.listen()
wn.onkeypress(paddle_left,"Left")
wn.onkeypress(paddle_right,"Right")
Part5 Move the ball(看我如何摇动此球)
#在Part3 Ball后面加两个变量,后续坐标+++,ball动起来
#Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0,0)
ball.dx = 0.2
ball.dy = 0.2
#Ball Moving
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
#Part6 Border check(防止球动的力度较大,一去不返,需要个框框留住球)
#Border Checking
if ball.xcor() > 240:#right
ball.setx(240)
ball.dx *= -1
if ball.xcor() < -240:#left
ball.setx(-240)
ball.dx *= -1
if ball.ycor() > 290:#top
ball.sety(290)
ball.dy *= -1
if ball.ycor() < -290:#under 下边界自己防守,守不住,球越狱到指定位置
ball.goto(100,100)
#Part7 Paddle and Ball collisions (猛烈的碰撞得分开始)
#Paddle and Ball conllisions
if ball.ycor()< -240 and (ball.xcor()>paddle.xcor()-25 and ball.xcor()<paddle.xcor()+25):
ball.sety(-210)
ball.dy *= -1 #蚂蚁撞大象,反弹回去
#Part8 Score (可能需要记录得分)
#Score 裁判可以统计分数了
score = 0
#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", 20 , "normal"))
#在Part7 Paddle and Ball conllisions 后面记录碰撞得分,得分显示
pen.clear()
pen.write("Score: {} ".format(score), align = "Center" ,font = ("Courier", 20 , "normal"))
Last Part : 源码奉上,记录本次学习
#Rebound Ball Game
#Part1 Game UI
#Part2 Paddle
#Part3 Ball
#Part4 Move the paddle
#Part5 Move the ball
#Part6 Border check
#Part7 Paddle and Ball collisions
#Part8 Score
import turtle
#UI
wn = turtle.Screen()
wn.title("Rebound Ball Game @TimeOld")
wn.bgcolor("black")
wn.setup(width=500,height=600)
wn.tracer(0)
#Score
score = 0
#Paddle
paddle = turtle.Turtle()
paddle.speed(0)
paddle.shape("square")
paddle.color("white")
paddle.shapesize(1,5)
paddle.penup()
paddle.goto(0,-260)
#Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0,0)
ball.dx = 0.2
ball.dy = -0.2
#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", 20 , "normal"))
#Function
def paddle_left():
x = paddle.xcor()
x -= 20
paddle.setx(x)
def paddle_right():
x = paddle.xcor()
x += 20
paddle.setx(x)
#Keyboard binding
wn.listen()
wn.onkeypress(paddle_left,"Left")
wn.onkeypress(paddle_right,"Right")
#Main game loop
while True:
wn.update()
#Ball Moving
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
#Border Checking
if ball.xcor() > 240:#right
ball.setx(240)
ball.dx *= -1
if ball.xcor() < -240:#left
ball.setx(-240)
ball.dx *= -1
if ball.ycor() > 290:#top
ball.sety(290)
ball.dy *= -1
if ball.ycor() < -290:#under
ball.goto(100,100)
#Paddle and Ball conllisions
if ball.ycor()< -240 and (ball.xcor()>paddle.xcor()-25 and ball.xcor()<paddle.xcor()+25):
ball.sety(-210)
ball.dy *= -1
score += 1
pen.clear()
pen.write("Score: {} ".format(score), align = "Center" ,font = ("Courier", 20 , "normal"))
本文分享了Python大神教学课程的学习心得,通过动手实践,详细解析了使用Turtle模块开发弹力球游戏的全过程,从游戏界面设置到球与拍的碰撞检测,再到得分系统实现。
1178

被折叠的 条评论
为什么被折叠?



