MicroPython动手做(35)——体验小游戏
2020-06-252.5万
AI 快速预览详细
本文介绍了小游戏的概念及其特点,包括体积小、玩法简单、安装简便等。小游戏通常以休闲益智类为主,适合家长和孩子一起探索。通过MicroPython动手制作小游戏,可以激发孩子的兴趣和创造力。
![]() 小游戏 (体积较小、玩法简单的游戏) 小游戏是一个较模糊的概念,它是相对于体积庞大的单机游戏及网络游戏而言的,泛指所有体积较小、玩法简单的游戏,通常这类游戏以休闲益智类为主,有单机版有网页版,在网页上嵌入的多为FLASH格式。当下小游戏主要是指在线玩的flash版本游戏,统称小游戏,其实小游戏还包含单机游戏,小型游戏机等。一般游戏大小小于10m的游戏都统称为小游戏,一些街机类小游戏。因其游戏安装简便,耐玩性强,无依赖性而广受白领及小朋友的喜爱。 “小游戏”这个词的型含义其实很简单,它不是一些大的游戏,不必花费更多的时间和精力。小游戏是原始的游戏娱乐方式,小游戏本身是为了叫人们在工作,学习后的一种娱乐、休闲的一种方式,不是为了叫玩家为之花费金钱、花费精力,更不是叫玩家为他痴迷。小游戏也可以理解为“Flash游戏”,是以SWF为后缀的游戏的总称.这些游戏是通过Flash软件和 Flash 编程语言 Flash ActionScript 制作而成。由于Flash是矢量软件,所以小游戏放大后几乎不影响画面效果。Flash小游戏是一种新兴起的游戏形式,以游戏简单,操作方便,绿色,无需安装,文件体积小等优点渐渐被广大网友喜爱。 |






[backcolor=rgb(255, 255, 255)][font="]
[/font][/backcolor]
[backcolor=rgb(255, 255, 255)][font="]
[/font][/backcolor]
[mw_shl_code=python,false]#MicroPython动手做(35)——小游戏
#石头剪刀布
from mpython import *
import time
import random
from machine import Timer
def on_button_a_down(_):
global y, x, k, j
time.sleep_ms(10)
if button_a.value() == 1: return
x = random.randint(1, 3)
xianshi(x, 10)
def on_button_b_down(_):
global y, x, k, j
time.sleep_ms(10)
if button_b.value() == 1: return
y = random.randint(1, 3)
xianshi(y, 90)
_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass
tim12 = Timer(12)
def timer12_tick(_):
global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
try:
touchPad_P.read();pass
except:
return
if touchPad_P.read() < 400:
if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
if touchPad_Y.read() < 400:
if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
if touchPad_T.read() < 400:
if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
if touchPad_H.read() < 400:
if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
if touchPad_O.read() < 400:
if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
if touchPad_N.read() < 400:
if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()
tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)
def on_touchpad_P_pressed():
global y, x, k, j
x = 0
y = 0
oled.fill(0)
oled.DispChar("玩家1按A键玩家2按B键", 0, 0, 1)
oled.show()
def xianshi(j, k):
global y, x
if j == 1:
oled.blit(image_picture.load('face/rock_s.pbm', 0), k, 17)
if j == 2:
oled.blit(image_picture.load('face/scissors_s.pbm', 0), k, 17)
if j == 3:
oled.blit(image_picture.load('face/paper_s.pbm', 0), k, 17)
oled.show()
def panduan():
global y, x, k, j
if x == y:
oled.DispChar(" 打平", 50, 50, 1)
else:
if x - y == -1 or x - y == 2:
oled.DispChar("玩家1赢", 50, 50, 1)
else:
oled.DispChar("玩家2赢", 50, 50, 1)
oled.show()
random.seed(time.ticks_cpu())
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
image_picture = Image()
oled.fill(0)
oled.DispChar(" 石头剪刀布小游戏", 0, 0, 1)
oled.DispChar("玩家1按A键玩家2按B键", 0, 16, 1)
oled.DispChar(" 系统自动判断谁输谁赢", 0, 32, 1)
oled.DispChar(" 按P键开始玩", 0, 48, 1)
oled.show()
x = 0
y = 0
while True:
if x != 0 and y != 0:
panduan()[/mw_shl_code]
[mw_shl_code=python,false]#MicroPython动手做(35)——小游戏
#滚雪球
from mpython import *
import time
import random
import math
def on_button_a_down(_):
global i, x, y, r, yn, xn, my_listy, my_listx, j
time.sleep_ms(10)
if button_a.value() == 1: return
x = 4
y = 32
xn = 0
yn = 0
r = 4
i = 0
j = 10
my_listx = []
my_listy = []
for count in range(10):
my_listx.append((random.randint(20, 120)))
my_listy.append((random.randint(1, 60)))
oled.fill(0)
for count in range(10):
oled.pixel(my_listx[xn], my_listy[yn], 1)
xn = xn + 1
yn = yn + 1
oled.show()
while True:
oled.fill_circle(x, y, r, 1)
oled.DispChar((str(int(r))), 0, 0, 1)
oled.show()
if get_tilt_angle('Y') < -15:
x = x + 1
if get_tilt_angle('X') < -10:
y = y + -1
if get_tilt_angle('X') > 10:
y = y + 1
if x > 130:
break
i = 0
my_func()
def my_func():
global i, x, y, r, yn, xn, my_listy, my_listx, j
for count in range(int(j)):
if math.sqrt((x - my_listx[i]) ** 2 + (y - my_listy[i]) ** 2) <= r:
r = r + 2
my_listx[i] = (-1)
my_listy[i] = (-1)
i = i + 1
random.seed(time.ticks_cpu())
def get_tilt_angle(_axis):
_Ax = accelerometer.get_x()
_Ay = accelerometer.get_y()
_Az = accelerometer.get_z()
if 'X' == _axis:
_T = math.sqrt(_Ay ** 2 + _Az ** 2)
if _Az < 0: return math.degrees(math.atan2(_Ax , _T))
else: return 180 - math.degrees(math.atan2(_Ax , _T))
elif 'Y' == _axis:
_T = math.sqrt(_Ax ** 2 + _Az ** 2)
if _Az < 0: return math.degrees(math.atan2(_Ay , _T))
else: return 180 - math.degrees(math.atan2(_Ay , _T))
elif 'Z' == _axis:
_T = math.sqrt(_Ax ** 2 + _Ay ** 2)
if (_Ax + _Ay) < 0: return 180 - math.degrees(math.atan2(_T , _Az))
else: return math.degrees(math.atan2(_T , _Az)) - 180
return 0
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)[/mw_shl_code]
[mw_shl_code=python,false]#MicroPython动手做(35)——小游戏
#打砖块
import time
import random
from mpython import *
random.seed(time.ticks_cpu())
my_listy = []
my_listx = []
m = 0
n = 0
score = 0
x = random.randint(35, 90)
y = 60
cx = 1
cy = -1
Lx = 50
for count in range(3):
m = 0
for count in range(16):
my_listx.append(m)
m = m + 8
n = 0
for count in range(3):
for count in range(16):
my_listy.append(n)
n = n + 8
while True:
m = 0
n = 0
oled.fill(0)
for count in range(48):
oled.rect(my_listx[m], my_listy[n], 8, 8, 1)
if y <= my_listy[n] + 10:
if x >= my_listx[m] and x <= my_listx[m] + 8:
my_listx[m] = (-40)
my_listy[n] = (-40)
cy = 1
score = score + 1
m = m + 1
n = n + 1
oled.fill_rect(Lx, 62, 30, 2, 1)
oled.circle(x, y, 3, 1)
oled.show()
if button_a.value() == 0:
Lx = Lx + -3
if Lx <= 0:
Lx = 0
if button_b.value() == 0:
Lx = Lx + 3
if Lx >= 107:
Lx = 107
x = x + cx
y = y + cy
if x <= 2:
cx = 1
if x >= 125:
cx = -1
if y <= 2:
cy = 1
if y > 60:
if x >= Lx - 2 and x <= Lx + 32:
cy = -1
else:
oled.fill(0)
oled.DispChar(str("游戏结束,得分为:") + str(score), 0, 16, 1)
oled.show()
break[/mw_shl_code]
13、飞行小鸟
[mw_shl_code=python,false]#MicroPython动手做(35)——小游戏
#飞行小鸟
from mpython import *
from framebuf import FrameBuffer
import framebuf
import time, uos,urandom
# 16 x 12
BIRD = bytearray([
0x7, 0xe0, 0x18, 0xf0, 0x21, 0xf8, 0x71, 0xec, 0xf9, 0xec, 0xfc, 0xfc, 0xbe, 0x7e, 0x4c, 0x81, 0x71, 0x7e, 0x40,
0x82, 0x30, 0x7c, 0xf, 0x80
])
# 16 x 32
PIPE_TOP = bytearray([
0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c,
0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0xff, 0xff, 0x80, 0xf, 0x80,
0xf, 0x80, 0xf, 0x80, 0xf, 0xff, 0xff
])
PIPE_DOWN = bytearray([
0xff, 0xff, 0x80, 0xf, 0x80, 0xf, 0x80, 0xf, 0x80, 0xf, 0xff, 0xff, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c,
0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c,
0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c
])
# Bitmap images
bird_size = (16, 12)
pipe_size = (16, 32)
WIDTH = 128
HEIGHT = 64
"""飞行小鸟类"""
class Bird:
def __init__(self):
self.height = bird_size[1]
self.y = HEIGHT // 2 - self.height // 2
self.wing_power = 4
self.gravity = 0.8
self.vel = -self.wing_power
# 下落
def drop(self):
self.vel += self.gravity
self.y = int(self.y + self.vel)
# 飞行
def flap(self):
self.vel = -self.wing_power
# 是否坠落
def crashed(self):
y_limit = HEIGHT - self.height
return self.y > y_limit
"""障碍类"""
class Obstacle:
def __init__(self, x,size ):
self.size =size
self.gap = urandom.randint(6 + self.size, HEIGHT - 6 - self.size) # 随机生成间隙大小
self.x = x # 距离鸟大小
self.score = 0 # 分数
self.rate = 3 # 速率
# 移动
def scroll(self):
self.x -= self.rate
if self.x < -pipe_size[0]:
self.score += 1
self.x = WIDTH
self.gap = urandom.randint(6 + self.size, HEIGHT - 6 - self.size)
# 是否碰撞
def collided(self, y):
if self.x < bird_size[0] and self.x > -pipe_size[0] and \
(self.gap - y > self.size or y + bird_size[1] - self.gap > self.size):
return True
else:
return False
class Game():
def __init__(self,gap_size):
# 创建鸟和管道的framebuffer
self.bird_fb = FrameBuffer(BIRD, bird_size[0], bird_size[1], framebuf.MONO_HLSB)
self.pipe_top_fb = FrameBuffer(PIPE_TOP, pipe_size[0], pipe_size[1], framebuf.MONO_HLSB)
self.pipe_down_fb = FrameBuffer(PIPE_DOWN, pipe_size[0], pipe_size[1], framebuf.MONO_HLSB)
self.gap_size = gap_size
self.high_score = 0
self.pressed = False
self.game_state = 0
self.flappy_bird = None
self.obstacle_1 = None
self.obstacle_2 = None
# 保存最高分
def write_high_score(self,n):
f = open('fb_high_score.txt', 'w')
f.write(str(n))
f.close()
# 读取最高分
def read_high_score(self):
if 'fb_high_score' in uos.listdir():
f = open('fb_high_score.txt', 'r')
high_score = f.read()
f.close()
return int(high_score)
else:
self.write_high_score(0)
return 0
# 绘制
def draw(self):
oled.fill(0)
oled.blit(self.bird_fb, 0, self.flappy_bird.y)
oled.blit(self.pipe_top_fb, self.obstacle_1.x, self.obstacle_1.gap - self.gap_size - pipe_size[1])
oled.blit(self.pipe_down_fb, self.obstacle_1.x, self.obstacle_1.gap + self.gap_size)
oled.blit(self.pipe_top_fb, self.obstacle_2.x, self.obstacle_2.gap - self.gap_size - pipe_size[1])
oled.blit(self.pipe_down_fb, self.obstacle_2.x, self.obstacle_2.gap + self.gap_size)
oled.fill_rect(WIDTH // 2 - 13, 0, 26, 9, 0)
oled.text('%03d' % (self.obstacle_1.score + self.obstacle_2.score), WIDTH // 2 - 12, 0)
oled.show()
def _clicked(self):
if button_a.value() == 0 and not self.pressed:
self.pressed = True
return True
elif button_a.value() == 1 and self.pressed:
self.pressed = False
return False
# 开机画面
def game_start(self):
oled.fill(0)
oled.blit(self.pipe_down_fb, (WIDTH - pipe_size[0]) // 2, HEIGHT - 12)
oled.blit(self.bird_fb, (WIDTH - bird_size[0]) // 2, HEIGHT - 12 - bird_size[1])
oled.rect(0, 0, WIDTH, HEIGHT, 1)
oled.text('F L A P P Y', WIDTH // 2 - 44, 3)
oled.text('B I R D', WIDTH // 2 - 28, 13)
oled.text('Record: ' + '%03d' % self.high_score, WIDTH // 2 - 44, HEIGHT // 2 - 6)
oled.show()
self.game_state = 1
def game_waiting(self):
if self._clicked():
self.flappy_bird = Bird() # 实例小鸟对象
self.obstacle_1 = Obstacle(WIDTH,self.gap_size) # 实例第一个障碍对象
self.obstacle_2 = Obstacle(WIDTH + (WIDTH + pipe_size[0]) // 2,self.gap_size) # 实例第二个障碍对象
self.game_state = 2
def game_running(self):
if self._clicked():
self.flappy_bird.flap()
self.flappy_bird.drop()
if self.flappy_bird.crashed():
self.flappy_bird.y = HEIGHT - self.flappy_bird.height # 边界限制
self.game_state = 3
self.obstacle_1.scroll()
self.obstacle_2.scroll()
if self.obstacle_1.collided(self.flappy_bird.y) or self.obstacle_2.collided(self.flappy_bird.y):
self.game_state = 3
self.draw()
def game_over(self):
oled.fill_rect(WIDTH // 2 - 32, 10, 64, 23, 0)
oled.rect(WIDTH // 2 - 32, 10, 64, 23, 1)
oled.text('G A M E', WIDTH // 2 - 28, 13)
oled.text('O V E R', WIDTH // 2 - 28, 23)
self.score = self.obstacle_1.score + self.obstacle_2.score
if self.score > self.high_score:
self.high_score = self.score
oled.fill_rect(WIDTH // 2 - 48, 37, 96, 14, 0)
oled.rect(WIDTH // 2 - 48, 37, 96, 14, 1)
oled.text('New record!', WIDTH // 2 - 44, 40)
self.write_high_score(self.high_score)
oled.show()
try:
self.send_score(self.score)
except:
pass
self.game_state = 1
def run(self):
while True:
if self.game_state == 0: self.game_start()
elif self.game_state == 1: self.game_waiting()
elif self.game_state == 2: self.game_running()
elif self.game_state == 3: self.game_over()
if __name__ == '__main__':
game=Game(gap_size = 16)
game.run()[/mw_shl_code]
# 乒乓球(实验视频)
https://v.youku.com/v_show/id_XN ... oneSokuUgc_1.dtitle
[media=x,500,375]https://v.youku.com/v_show/id_XNDcyODQ0MzYyNA==.html?spm=a2h0c.8166622.PhoneSokuUgc_1.dtitle[/media]
[mw_shl_code=python,false]#MicroPython动手做(35)——小游戏
# 乒乓球
from mpython import *
import music
class Pong():
def __init__(self):
self.running = True
self.start = False
self.ball_rad = 5
self.bats_position = 0
self.bats_width = 15
self.bats_height = 4
self.ball_x = self.bats_width // 2
self.ball_y = 64 - (self.ball_rad + self.bats_height + 1)
self.inc_x, self.inc_y = 1, 1
self.score = 0
def collision(self):
if self.ball_x >= 128 - self.ball_rad or self.ball_x < self.ball_rad:
self.inc_x = -self.inc_x
if self.ball_y >= 64 - (self.ball_rad + self.bats_height) or self.ball_y <= self.ball_rad:
self.inc_y = -self.inc_y
def update(self):
self.ball_x = self.ball_x + self.inc_x
self.ball_y = self.ball_y + self.inc_y
self.bats_position = min(max(self.bats_position, 0), 128 - self.bats_width)
def is_hit(self):
# print('ball:', self.ball_x, self.ball_y, 'bats:', self.bats_position)
if self.ball_y >= 64 - (self.ball_rad + self.bats_height):
if self.ball_x >= self.bats_position + self.bats_width + self.ball_rad or self.ball_x <= self.bats_position - self.ball_rad:
return False
self.score += 1
return True
def run(self):
while self.running:
if button_a.value() == 0 and button_b.value() == 1:
self.bats_position -= 2
self.start = True
if button_a.value() == 1 and button_b.value() == 0:
self.bats_position += 2
self.start = True
if self.start:
self.update()
self.collision()
if self.is_hit() == False:
self.running = False
continue
oled.fill(0)
oled.fill_circle(self.ball_x, self.ball_y, self.ball_rad, 1)
oled.fill_rect(self.bats_position, 64 - self.bats_height, self.bats_width, self.bats_height, 1)
oled.show()
oled.text('Game over!', 20, 20)
oled.text('Score %d' % self.score, 20, 32)
oled.show()
if __name__ == '__main__':
pong = Pong()
pong.run()[/mw_shl_code]
[mw_shl_code=python,false]#MicroPython动手做(35)——小游戏
#俄罗斯方块
from mpython import *
import math
import random, time
class Brick():
def __init__(self, p_position):
self.position = p_position
def draw(self):
x = self.position[1] * brick_size
y = self.position[0] * brick_size
oled.fill_rect(brick_size * (field_height - 1) - x, y, brick_size, brick_size, 1)
class Block():
def __init__(self, p_bricks_layout, p_direction):
self.bricks_layout = p_bricks_layout
self.direction = p_direction
self.init_position = (field_width // 2 - 2, 0)
self.cur_layout = self.bricks_layout[self.direction]
self.position = self.init_position
self.stopped = False
self.move_interval = 500
self.last_move = 0
self.bricks = []
for (x, y) in self.cur_layout:
self.bricks.append(Brick((self.position[0] + x, self.position[1] + y)))
def draw(self):
for brick in self.bricks:
brick.draw()
def isLegal(self, layout, position):
(x0, y0) = position
for (x, y) in layout:
if x + x0 < 0 or y + y0 < 0 or x + x0 >= field_width or y + y0 >= field_height:
return False
if field_map[y + y0][x + x0] != 0:
return False
return True
def left(self):
new_position = (self.position[0] - 1, self.position[1])
if self.isLegal(self.cur_layout, new_position):
self.position = new_position
self.refresh_bircks()
def right(self):
new_position = (self.position[0] + 1, self.position[1])
if self.isLegal(self.cur_layout, new_position):
self.position = new_position
self.refresh_bircks()
def down(self):
(x, y) = (self.position[0], self.position[1] + 1)
while self.isLegal(self.cur_layout, (x, y)):
self.position = (x, y)
self.refresh_bircks()
y += 1
def refresh_bircks(self):
for (brick, (x, y)) in zip(self.bricks, self.cur_layout):
brick.position = (self.position[0] + x, self.position[1] + y)
def stop(self):
global field_bricks
global score
self.stopped = True
ys = []
for brick in self.bricks:
field_bricks.append(brick)
(x, y) = brick.position
if y not in ys:
ys.append(y)
field_map[y][x] = 1
eliminate_count = 0
ys.sort()
for y in ys:
if 0 in field_map[y]:
continue
eliminate_count += 1
for fy in range(y, 0, -1):
field_map[fy] = field_map[fy - 1][:]
field_map[0] = [0 for i in range(field_width)]
tmp_field_bricks = []
for fb in field_bricks:
(fx, fy) = fb.position
if fy < y:
fb.position = (fx, fy + 1)
tmp_field_bricks.append(fb)
elif fy > y:
tmp_field_bricks.append(fb)
field_bricks = tmp_field_bricks
if eliminate_count == 1:
score += 1
elif eliminate_count == 2:
score += 2
elif eliminate_count == 3:
score += 4
elif eliminate_count == 4:
score += 6
def update(self, time):
self.draw()
if time - self.last_move >= self.move_interval:
new_position = (self.position[0], self.position[1] + 1)
if self.isLegal(self.cur_layout, new_position):
self.position = new_position
self.refresh_bircks()
self.last_move = time
else:
self.stop()
def rotate(self):
new_direction = (self.direction + 1) % len(self.bricks_layout)
new_layout = self.bricks_layout[new_direction]
if not self.isLegal(new_layout, self.position):
return
self.direction = new_direction
self.cur_layout = new_layout
for (brick, (x, y)) in zip(self.bricks, self.cur_layout):
brick.position = (self.position[0] + x, self.position[1] + y)
self.refresh_bircks()
self.draw()
# 0: oooo
# 1: oo
# oo
# 2: o
# ooo
# 3: o
# oo
# o
# 4: o
# oo
# o
# 5: ooo
# o
# 6: ooo
# o
bricks_layout_0 = (((0, 0), (0, 1), (0, 2), (0, 3)), ((0, 1), (1, 1), (2, 1), (3, 1)))
bricks_layout_1 = (((1, 0), (2, 0), (1, 1), (2, 1)), )
bricks_layout_2 = (
((1, 0), (0, 1), (1, 1), (2, 1)),
((0, 1), (1, 0), (1, 1), (1, 2)),
((1, 2), (0, 1), (1, 1), (2, 1)),
((2, 1), (1, 0), (1, 1), (1, 2)),
)
bricks_layout_3 = (
((0, 1), (1, 1), (1, 0), (2, 0)),
((0, 0), (0, 1), (1, 1), (1, 2)),
)
bricks_layout_4 = (
((0, 0), (1, 0), (1, 1), (2, 1)),
((1, 0), (1, 1), (0, 1), (0, 2)),
)
bricks_layout_5 = (
((0, 0), (1, 0), (1, 1), (1, 2)),
((0, 2), (0, 1), (1, 1), (2, 1)),
((1, 0), (1, 1), (1, 2), (2, 2)),
((2, 0), (2, 1), (1, 1), (0, 1)),
)
bricks_layout_6 = (
((2, 0), (1, 0), (1, 1), (1, 2)),
((0, 0), (0, 1), (1, 1), (2, 1)),
((0, 2), (1, 2), (1, 1), (1, 0)),
((2, 2), (2, 1), (1, 1), (0, 1)),
)
field_width, field_height = 16, 30
brick_size = 4
field_map = [[0 for i in range(field_width)] for i in range(field_height)]
field_bricks = []
score = 0
running = True
threshhold = 400
def drawField():
for brick in field_bricks:
brick.draw()
def getBlock():
block_type = random.randint(0, 6)
if block_type == 0:
return Block(bricks_layout_0, random.randint(0, len(bricks_layout_0) - 1))
elif block_type == 1:
return Block(bricks_layout_1, random.randint(0, len(bricks_layout_1) - 1))
elif block_type == 2:
return Block(bricks_layout_2, random.randint(0, len(bricks_layout_2) - 1))
elif block_type == 3:
return Block(bricks_layout_3, random.randint(0, len(bricks_layout_3) - 1))
elif block_type == 4:
return Block(bricks_layout_4, random.randint(0, len(bricks_layout_4) - 1))
elif block_type == 5:
return Block(bricks_layout_5, random.randint(0, len(bricks_layout_5) - 1))
elif block_type == 6:
return Block(bricks_layout_6, random.randint(0, len(bricks_layout_6) - 1))
def run():
global running
btn_n_stat, btn_o_stat, btn_t_stat, btn_p_stat = [0] * 4
while running:
cur_block = getBlock()
if not cur_block.isLegal(cur_block.cur_layout, cur_block.position):
cur_block.draw()
running = False
continue
while not cur_block.stopped:
oled.fill(0)
ticks = time.ticks_ms()
cur_block.update(ticks)
drawField()
oled.show()
if touchPad_T.read() < threshhold and btn_t_stat == 0:
cur_block.rotate()
btn_t_stat = 1
elif touchPad_T.read() >= threshhold:
btn_t_stat = 0
if touchPad_P.read() < threshhold and btn_p_stat == 0:
cur_block.down()
btn_p_stat = 1
elif touchPad_P.read() >= threshhold:
btn_p_stat = 0
if touchPad_N.read() < threshhold and btn_n_stat == 0:
cur_block.left()
btn_n_stat = 1
elif touchPad_N.read() >= threshhold:
btn_n_stat = 0
if touchPad_O.read() < threshhold and btn_o_stat == 0:
cur_block.right()
btn_o_stat = 1
elif touchPad_O.read() >= threshhold:
btn_o_stat = 0
oled.fill(0)
oled.text('Game over!', 25, 20)
oled.text('Score:%d' % score, 25, 32)
oled.show()
if __name__ == '__main__':
run()[/mw_shl_code]