"""Flappy, game inspired by Flappy Bird.
Exercises
1. Keep score.
2. Vary the speed.
3. Vary the size of the balls.
4. Allow the bird to move forward and back.
"""
from random import *
from turtle import *
from freegames import vector
bird = vector(0, 0)
balls = []
def tap(x, y):
"""Move bird up in response to screen tap."""
# 鼠标点击 移动鸟
up = vector(-10, 30)
bird.move(up)
def inside(point):
"""Return True if point on screen."""
# 判断 障碍是否在屏幕内 不在删除
return -200 < point.x < 200 and -200 < point.y < 200
def draw(alive):
"""Draw screen objects."""
# 画障碍和小鸟
clear()
goto(bird.x, bird.y)
if alive:
dot(10, 'green') # 改变小球的大小
else:
dot(10, 'red')
for ball in balls:
goto(ball.x, ball.y)
dot(20, 'black') # 改变障碍的大小
update()
def move():
"""Update object positions."""
# 更新 鸟和障碍的坐标 判断是否撞到障碍
bird.y -= 5
bird.x += 1
for ball in balls:
ball.x -= 3
if randrange(10) == 0:
y = randrange(-199, 199)
ball = vector(199, y)
balls.append(ball)
while len(balls) > 0 and not inside(balls[0]):
balls.pop(0)
if not inside(bird):
draw(False)
return
for ball in balls:
if abs(ball - bird) < 15:
draw(False)
return
draw(True)
ontimer(move, 50) # 改变速度
setup(420, 420, 370, 0)
hideturtle()
up()
tracer(False)
onscreenclick(tap)
move()
done()
7 flappy bird
最新推荐文章于 2024-04-16 12:34:02 发布