Python小游戏,弹力球

《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"))
   

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值