【得捷电子Follow me第1期】第九帖 - 贪吃蛇1
[复制链接]
这次的总结准备做一个贪吃蛇的小游戏。需要用到屏幕和操作杆。现在已经实现了苹果的刷新、蛇的移动、吃果和死亡判断。还准备加上音乐和开始界面。
用Python写程序还是不熟练,经常出错误,尤其是设计面向对象的地方,这次也算是通过项目提高一下编程能力了。
from ssd1306 import SSD1306_I2C
from machine import Pin, I2C
from utime import sleep
import framebuf
import random
import joystick
i2c = I2C(1)
oled = SSD1306_I2C(128,64,i2c)
oled.fill(0)
oled.show()
DOT_SIZE = 8
class Dot:
'作为苹果和贪吃蛇的基类,绘制8*8的方格'
def __init__(self,x=0,y=0):
self.coordinate = [x,y]
def get_x(self):
return self.coordinate[0]
def get_y(self):
return self.coordinate[1]
def equal(self,dot):
if (self.coordinate[0] == dot.coordinate[0]) and (self.coordinate[1] == dot.coordinate[1]):
return True
else:
return False
def copy(self,dot):
self.coordinate[0] = dot.coordinate[0]
self.coordinate[1] = dot.coordinate[1]
def show(self):
oled.rect(self.coordinate[0]*DOT_SIZE,self.coordinate[1]*DOT_SIZE,DOT_SIZE,DOT_SIZE,framebuf.MONO_HLSB)
oled.show()
def remove(self):
oled.rect(self.coordinate[0]*DOT_SIZE,self.coordinate[1]*DOT_SIZE,DOT_SIZE,DOT_SIZE,0)
oled.show()
def move_dot(self,direct):
if direct == 'up':
self.coordinate[1] += 1
elif direct == 'down':
self.coordinate[1] -= 1
elif direct == 'left':
self.coordinate[0] += 1
elif direct == 'right':
self.coordinate[0] -= 1
else:
pass
class Apple(Dot):
def __init__(self):
x = int(16*random.random())
y = int(8*random.random())
Dot.__init__(self,x,y)
def get_location(self):
return self.coordinate
def fresh(self,snk):
print('draw apple')
for i in range(len(snk.body)):
if self.coordinate == snk.body[i]:
x = int(16*random.random())
y = int(8*random.random())
self.coordinate = [x,y]
self.show()
print('show apple',self.coordinate,' ',snk.body)
class Snake:
def __init__(self):
self.direct = 'up'
b1 = Dot(0,1)
b2 = Dot(0,0)
self.body = list((b1,b2))
def draw(self):
for i in range(len(self.body)):
self.body[i].show()
def remove_snake(self):
#for i in range(len(self.body)):
self.body[-1].remove()
def move_snake(self):
length = len(self.body)
for i in range(length-1):
self.body[length-i-1].copy(self.body[length-i-2])
#print(self.zuobiao[0]," ",self.zuobiao[1])
self.body[0].move_dot(self.direct)
def check_snack_dead(self):
x = self.body[0].get_x()
y = self.body[0].get_y()
if self.body[0].get_x() < 0 or self.body[0].get_y() < 0 or self.body[0].get_x() >= 16 or self.body[0].get_y() >= 8:
raise Exception('snake dead')
else:
for i in range(len(self.body)-1):
if self.body[0] == self.body[i+1]:
raise Exception('snake dead')
def set_move_direct(self,direct):
self.direct = direct
def fresh_snake_location(self):
self.remove_snake()
self.move_snake()
self.draw()
def append_body(self):
#new_body = list((self.body[-1][0],self.body[-1][1]))
new_body = Dot()
new_body.copy(self.body[-1])
self.body.append(new_body)
def eat_dot(self,dot):
if dot.equal(self.body[0]):
self.append_body()
return True
else:
return False
apple = Apple()
snk = Snake()
snk.draw()
apple.fresh(snk)
while True:
snk.fresh_snake_location()
snk.check_snack_dead()
if snk.eat_dot(apple):
apple = Apple()
apple.fresh(snk)
for i in range(8):
btn = joystick.read_btn()
if btn != '':
snk.set_move_direct(btn)
sleep(0.05)
演示视频
|