【名家作品】flappy_bird,超难过关的掌控小游戏
有一首歌我要飞得更高
小朋友却接着唱
撞死拉倒
真是一群熊孩子
今天就有一个掌控小游戏
flappy_bird
分享给大家
http://v.youku.com/v_show/id_XMzkwMjU2NzYzMg==.html?spm=a2h3j.8428770.3416059.1
作者的这个视频
被吴俊杰老师转发
并配文
世间有高人
它的作者
是
很厉害的创客大伽
李时念老师
掌声
{:5_148:}
游戏中的小鸟
让我家孩子用了十分钟
才得了3分
哎
比吃鸡难多了
如果想要得高分
想要好过关
告诉你吧
有秘决
那就是
改代码
至于怎么改
小伙伴们
{:5_151:}
python
学起来
#flappy_bird 游戏代码作者 李时念老师
from mpython import *
import random
import framebuf
#屏幕大小
WIDTH = 128
HEIGHT= 64
#通过urandom字符串免去生产随机种子
def random(a,b):
result = uos.urandom(1) / 256
result *= b-a
result += a
return int(result)
#字符串Byte转为十六进制节约文本空间
def to_bytearray(s):
return bytearray() for i in range(0,len(s),2)])
#写最高分
def write_high_score(n):
f = open('fb_high_score', 'w')
f.write(str(n))
f.close()
#读最高分
def read_high_score():
if 'fb_high_score' in uos.listdir():
f = open('fb_high_score', 'r')
high_score = f.read()
f.close()
return int(high_score)
else:
#没有文件初始为0
write_high_score(0)
return 0
#三个精灵可以理解为游戏可以活动的部分 一个鸟上下两个管子
BIRD = '07e018f021f871ecf9ecfcfcbe7e4c81717e4082307c0f80'
COL1 = '201c'*26+'ffff'+'800f'*4+'ffff'
COL2 = 'ffff'+'800f'*4+'ffff'+'201c'*26
#精灵大小
bird_size = (16,12)
colu_size= (16,32)
#生成点阵
bird = framebuf.FrameBuffer(to_bytearray(BIRD),bird_size,bird_size,framebuf.MONO_HLSB)
col1 = framebuf.FrameBuffer(to_bytearray(COL1),colu_size,colu_size,framebuf.MONO_HLSB)
col2 = framebuf.FrameBuffer(to_bytearray(COL2),colu_size,colu_size,framebuf.MONO_HLSB)
#定义一个鸟的类
class FlappyBird:
def __init__(self):
self.height = bird_size
self.y = HEIGHT // 2 - self.height // 2
self.vel = -wing_power
def move(self):
self.vel += gravity
self.y = int(self.y + self.vel)
def flap(self):
self.vel = -wing_power
def crashed(self):
y_limit = HEIGHT - self.height
return self.y > y_limit
#定义一个障碍类
class Obstacle:
def __init__(self, x):
self.gap = random(6+gap_size, HEIGHT-6-gap_size)
self.x = x
self.score = 0
def scroll(self):
self.x -= velocity
if self.x < -colu_size:
self.score += 1
self.x = WIDTH
self.gap = random(6+gap_size, HEIGHT-6-gap_size)
def collided(self, y):
if self.x < bird_size and \
self.x > -colu_size and \
(self.gap - y > gap_size or y + bird_size - self.gap > gap_size):
return True
else:
return False
#很简单啦 读按键
def clicked():
global pressed
if touchPad_P.read() < 50 and not pressed:
pressed = True
return True
elif touchPad_P.read() > 50 and pressed:
pressed = False
return False
#绘制基本游戏场景
def draw():
display.fill(0)
display.blit(bird, 0, flappy_bird.y)
display.blit(col1,obstacle_1.x,obstacle_1.gap-gap_size-colu_size)
display.blit(col2,obstacle_1.x,obstacle_1.gap+gap_size)
display.blit(col1,obstacle_2.x,obstacle_2.gap-gap_size-colu_size)
display.blit(col2,obstacle_2.x,obstacle_2.gap+gap_size)
display.fill_rect(WIDTH//2 - 13, 0, 26, 9, 0)
display.DispChar('%03d' % (obstacle_1.score + obstacle_2.score), WIDTH//2 - 12, 0)
display.show()
#游戏的参数
#最高分
high_score= read_high_score()
#缺口大小
gap_size = 18
#飞行速度
velocity = 3
#下坠速度
gravity = .8
#点击飞行高度
wing_power= 4
#场景状态
state = 0
#按键
pressed = False
#开始画面
def splash_screen():
global state
display.fill(0)
display.blit(col2, (WIDTH-colu_size)//2, HEIGHT-12)
display.blit(bird, (WIDTH-bird_size)//2, HEIGHT-12-bird_size)
display.rect(0, 0, WIDTH, HEIGHT, 1)
display.DispChar('FLAPPY', WIDTH//2-36, 1)
display.DispChar('BIRD', WIDTH//2-20, 12)
display.DispChar('Record: ' + '%03d' % high_score, WIDTH//2-36, HEIGHT//2-8)
display.show()
state = 1
#判断帧
def game_waiting():
global state,score,flappy_bird,obstacle_1,obstacle_2, pressed
if clicked():
flappy_bird = FlappyBird()
obstacle_1 = Obstacle(WIDTH)
obstacle_2 = Obstacle(WIDTH + (WIDTH + colu_size) // 2)
display.fill(1)
display.show()
state = 2
#游戏更新状态
def game_running():
global state
if clicked(): flappy_bird.flap()
flappy_bird.move()
if flappy_bird.crashed():
flappy_bird.y = HEIGHT - flappy_bird.height
state = 3
obstacle_1.scroll()
obstacle_2.scroll()
if obstacle_1.collided(flappy_bird.y) or obstacle_2.collided(flappy_bird.y):
state = 3
draw()
#游戏结束状态
def game_over():
global state, high_score
display.fill_rect(WIDTH//2-32, 4, 64, 23, 0)
display.rect(WIDTH//2-32, 4, 64, 34, 1)
display.DispChar('G A M E', WIDTH//2-20, 8)
display.DispChar('O V E R', WIDTH//2-20, 21)
score = obstacle_1.score + obstacle_2.score
if score > high_score:
high_score = score
display.fill_rect(WIDTH//2-44,39, 96, 18, 0)
display.rect(WIDTH//2-44, 39, 96, 18, 1)
display.DispChar('New record!',WIDTH//2-30, 40)
write_high_score(high_score)
display.show()
state = 1
#循环状态
def loop():
while True:
if state == 0: splash_screen()
elif state == 1: game_waiting()
elif state == 2: game_running()
elif state == 3: game_over()
loop()
创意不错,空了也来做个游戏 历害,真是高手 李老师确实厉害,里面还定义了一个类,厉害,向高手致敬。回头学习一下,准备把它翻译成图形语言{:5_190:} 空军一号 发表于 2019-4-3 21:09
李老师确实厉害,里面还定义了一个类,厉害,向高手致敬。回头学习一下,准备把它翻译成图形语言 ...
桑老师也很厉害啊。 rzyzzxw 发表于 2019-4-4 07:35
桑老师也很厉害啊。
李老师的代码都没读懂,回头还得把python好好学学。 rzyzzxw 发表于 2019-1-30 09:33
有一首歌
我要飞得更高
小朋友却接着唱
真的好厉害。。。 厉害厉害
页:
[1]