micropython 01stuido canmv 实现2048游戏

之前一直没有尝试写过游戏,来试试简单的,找了比较好弄的硬件,01的canmv,k210芯片,主要是跑视觉的,不务正业了。。。主要是有大屏,有触摸,好搞。
没打算自己重头写,移植一个pygame的吧,选代码的时候很看程序员功底,写的清楚的,读起来也快,移植也快,基本框架不变,主要就是改掉不兼容的地方,改写屏幕的输出方式。
还挺爱搞这种移植工作的,移植的同时也是重新学习的过程。参考的是这位博主的文章,他的代码比较清晰,好移植。
csdn上另一位博主用pygame写的2048
界面肯定重写了,这里原博主写的代码就比较科学,坐标都是算出来的,如果都是给定的自己写方便但是移植到其他分辨率就很痛苦了。
我有界面强迫症,自己重做了UI,看起来稍微好点,以单片机的水平来说,我感觉还可以了,做UI的时间占了一半儿。。。
加了个BGM,在github发现了个比较好的蜂鸣器库,写的音乐比较有层次感,听了就知道了。
写着写着偷懒了,没有做结束游戏、通关、最高分、重新开始等功能,加这写不难,有兴趣的自己加一下,上截图:
在这里插入图片描述
上主要代码:

#make by jd3096 2022.10.28 wx:jd3096
import lcd, image
import sys, random, time, os
import math
from machine import I2C
from fpioa_manager import fm
from maix import GPIO
import touchscreen as ts
from time import sleep
from machine import Timer,PWM
from music import music
import gc
song = '0 F4 1 0;0 G#4 1 0;0 C4 1 0;0 F3 1 0;0 F2 1 0;12 A#4 1 0;18 G4 1 0;12 G4 1 0;18 D#4 1 0;24 A#4 1 0;30 C5 1 0;24 F4 1 0;24 C#4 1 0;12 F3 1 0;18 F3 1 0;12 F2 1 0;18 F2 1 0;24 F2 1 0;24 F3 1 0;36 F3 1 0;36 F2 1 0;36 D#5 1 0;36 D#4 1 0;36 G4 1 0;36 A#4 1 0;48 G#4 1 0;48 C5 1 0;48 F4 1 0;48 C4 1 0;48 F3 1 0;48 F2 1 0;60 F2 1 0;66 F2 1 0;60 F3 1 0;66 F3 1 0;60 G#4 1 0;60 F4 1 0;66 D#4 1 0;66 G4 1 0;72 A#4 1 0;72 F3 1 0;72 F2 1 0;72 C#4 1 0;72 F4 1 0;84 F2 1 0;84 F3 1 0;84 E4 1 0;84 E5 1 0;90 G5 1 0;90 G4 1 0;84 A#4 1 0;90 A#4 1 0;96 F5 1 0;96 F6 1 0;96 G#5 1 0;96 C6 1 0;96 F2 1 0;96 F3 1 0;98 C4 1 0;98 G#4 1 0;99 F4 1 0;100 G#4 1 0;102 G#4 1 0;101 F4 1 0;103 F4 1 0;100 C4 1 0;102 C4 1 0;104 G#4 1 0;106 G#4 1 0;105 F4 1 0;107 F4 1 0;104 C4 1 0;106 C4 1 0;108 A#4 1 0;110 A#4 1 0;112 A#4 1 0;114 A#4 1 0;116 A#4 1 0;118 A#4 1 0;109 F4 1 0;111 F4 1 0;113 F4 1 0;115 F4 1 0;117 F4 1 0;119 F4 1 0;108 C#4 1 0;110 C#4 1 0;112 C#4 1 0;114 C#4 1 0;116 C#4 1 0;118 C#4 1 0;114 E5 1 0;114 E6 1 0;114 E6 1 0;120 G6 1 0;120 G5 1 0;120 A#5 1 0;120 C#6 1 0;120 C#5 1 0;122 C#5 1 0;124 C#5 1 0;126 C#5 1 0;128 C#5 1 0;130 C#5 1 0;120 F4 1 0;122 F4 1 0;124 F4 1 0;126 F4 1 0;128 F4 1 0;130 F4 1 0;121 A#4 1 0;123 A#4 1 0;125 A#4 1 0;127 A#4 1 0;129 A#4 1 0;131 A#4 1 0;132 C5 1 0;134 C5 1 0;136 C5 1 0;138 C5 1 0;140 C5 1 0;142 C5 1 0;132 F4 1 0;134 F4 1 0;136 F4 1 0;138 F4 1 0;140 F4 1 0;142 F4 1 0;133 G#4 1 0;135 G#4 1 0;137 G#4 1 0;139 G#4 1 0;141 G#4 1 0;143 G#4 1 0;138 F5 1 0;138 F6 1 0;144 G#6 1 0;144 F3 1 0;145 C4 1 0;146 G#4 1 0;146 C4 1 0;147 F4 1 0;148 G#4 1 0;149 F4 1 0;148 C4 1 0;150 G#4 1 0;152 G#4 1 0;154 G#4 1 0;151 F4 1 0;153 F4 1 0;155 F4 1 0;150 C4 1 0;152 C4 1 0;154 C4 1 0;144 G#5 1 0;144 C6 1 0;144 F6 1 0;156 C#4 1 0;156 A#4 1 0;158 A#4 1 0;160 A#4 1 0;162 A#4 1 0;164 A#4 1 0;166 A#4 1 0;157 F4 1 0;159 F4 1 0;161 F4 1 0;163 F4 1 0;165 F4 1 0;167 F4 1 0;158 C#4 1 0;160 C#4 1 0;162 C#4 1 0;164 C#4 1 0;166 C#4 1 0;156 G6 1 0;156 G5 1 0;162 G#6 1 0;162 G#5 1 0;168 A#6 1 0;168 A#5 1 0;168 C#5 1 0;170 C#5 1 0;172 C#5 1 0;174 C#5 1 0;176 C#5 1 0;178 C#5 1 0;168 F4 1 0;170 F4 1 0;172 F4 1 0;174 F4 1 0;176 F4 1 0;178 F4 1 0;169 A#4 1 0;171 A#4 1 0;173 A#4 1 0;175 A#4 1 0;177 A#4 1 0;179 A#4 1 0;180 C5 1 0;182 C5 1 0;184 C5 1 0;186 C5 1 0;188 C5 1 0;190 C5 1 0;180 F4 1 0;182 F4 1 0;184 F4 1 0;186 F4 1 0;188 F4 1 0;190 F4 1 0;181 G#4 1 0;183 G#4 1 0;185 G#4 1 0;187 G#4 1 0;189 G#4 1 0;191 G#4 1 0;168 C#6 1 0;168 F6 1 0;180 G5 1 0;180 G6 1 0;178 G#6 1 0;178 G#5 1 0;192 F3 1 0;192 F2 1 0;192 F2 1 0;198 C4 1 0;200 C4 1 0;202 C4 1 0;204 C#4 1 0;206 C#4 1 0;208 C#4 1 0;210 C#4 1 0;212 C#4 1 0;214 C#4 1 0;198 F4 1 0;200 F4 1 0;202 F4 1 0;204 F4 1 0;206 F4 1 0;208 F4 1 0;210 F4 1 0;212 F4 1 0;214 F4 1 0;198 G#4 1 0;200 G#4 1 0;202 G#4 1 0;204 A#4 1 0;206 A#4 1 0;208 A#4 1 0;210 A#4 1 0;212 A#4 1 0;214 A#4 1 0;192 F6 1 0;192 F5 1 0;192 G#5 1 0;192 C6 1 0;216 F3 1 0;216 F2 1 0;216 F2 1 0;198 F5 1 0;204 E5 1 0;210 G5 1 0;198 C5 1 0;204 C#5 1 0;210 C#5 1 0;210 E5 1 0;222 G#4 1 0;224 G#4 1 0;226 G#4 1 0;228 A#4 1 0;230 A#4 1 0;232 A#4 1 0;234 A#4 1 0;236 A#4 1 0;238 A#4 1 0;222 F4 1 0;224 F4 1 0;226 F4 1 0;228 F4 1 0;230 F4 1 0;232 F4 1 0;234 F4 1 0;236 F4 1 0;238 F4 1 0;222 C4 1 0;224 C4 1 0;226 C4 1 0;228 C#4 1 0;230 C#4 1 0;232 C#4 1 0;234 C#4 1 0;236 C#4 1 0;238 C#4 1 0;222 F5 1 0;222 C5 1 0;228 C#5 1 0;228 E5 1 0;228 G5 1 0;234 A#5 1 0;234 G5 1 0;234 E5 1 0;234 C#5 1 0;240 F3 1 0;240 F2 1 0;240 F2 1 0;246 C4 1 0;248 C4 1 0;250 C4 1 0;252 C#4 1 0;254 C#4 1 0;256 C#4 1 0;258 C#4 1 0;260 C#4 1 0;262 C#4 1 0;246 F4 1 0;248 F4 1 0;250 F4 1 0;252 F4 1 0;254 F4 1 0;256 F4 1 0;258 F4 1 0;260 F4 1 0;262 F4 1 0;246 G#4 1 0;248 G#4 1 0;250 G#4 1 0;252 A#4 1 0;254 A#4 1 0;256 A#4 1 0;258 A#4 1 0;260 A#4 1 0;262 A#4 1 0;246 F6 1 0;252 E6 1 0;258 G6 1 0;246 F5 1 0;252 E5 1 0;258 G5 1 0;246 G#5 1 0;246 C6 1 0;252 G5 1 0;252 A#5 1 0;258 A#5 1 0;258 C#6 1 0;264 F3 1 0;264 F2 1 0;264 F2 1 0;270 G#4 1 0;272 G#4 1 0;274 G#4 1 0;276 A#4 1 0;278 A#4 1 0;280 A#4 1 0;282 A#4 1 0;284 A#4 1 0;286 A#4 1 0;270 F4 1 0;272 F4 1 0;274 F4 1 0;276 F4 1 0;278 F4 1 0;280 F4 1 0;282 F4 1 0;284 F4 1 0;286 F4 1 0;270 C4 1 0;272 C4 1 0;274 C4 1 0;276 C#4 1 0;278 C#4 1 0;280 C#4 1 0;282 C#4 1 0;284 C#4 1 0;286 C#4 1 0;270 C7 1 0;270 C6 1 0;270 F6 1 0;270 G#6 1 0;276 A#6 1 0;282 G6 1 0;276 A#5 1 0;282 G5 1 0;276 C#6 1 0;276 F6 1 0;282 A#5 1 0;282 C#6 1 0;288 F5 1 0;288 F6 1 0;288 G#5 1 0;288 C6 1 0;288 F3 1 0;288 F2 1 0;290 G#4 1 0;291 F4 1 0;290 C4 1 0;292 C4 1 0;294 C4 1 0;296 C4 1 0;298 C4 1 0;293 F4 1 0;295 F4 1 0;297 F4 1 0;299 F4 1 0;292 G#4 1 0;294 G#4 1 0;296 G#4 1 0;298 G#4 1 0;300 G#4 1 0;302 G#4 1 0;304 G#4 1 0;306 G#4 1 0;308 G#4 1 0;310 G#4 1 0;301 F4 1 0;303 F4 1 0;305 F4 1 0;307 F4 1 0;309 F4 1 0;311 F4 1 0;300 C4 1 0;302 C4 1 0;304 C4 1 0;306 C4 1 0;308 C4 1 0;310 C4 1 0;312 C#4 1 0;312 A#4 1 0;314 A#4 1 0;316 A#4 1 0;318 A#4 1 0;320 A#4 1 0;322 A#4 1 0;324 A#4 1 0;326 A#4 1 0;328 A#4 1 0;330 A#4 1 0;332 A#4 1 0;334 A#4 1 0;313 F4 1 0;315 F4 1 0;317 F4 1 0;319 F4 1 0;321 F4 1 0;323 F4 1 0;325 F4 1 0;327 F4 1 0;329 F4 1 0;331 F4 1 0;333 F4 1 0;335 F4 1 0;314 C#4 1 0;316 C#4 1 0;318 C#4 1 0;320 C#4 1 0;322 C#4 1 0;324 C#4 1 0;326 C#4 1 0;328 C#4 1 0;330 C#4 1 0;332 C#4 1 0;334 C#4 1 0;312 G6 1 0;312 G5 1 0;312 A#5 1 0;312 C#6 1 0;324 A#6 1 0;324 A#5 1 0;324 C#6 1 0;324 F6 1 0;336 G#6 1 0;336 G#5 1 0;336 F3 1 0;337 C4 1 0;338 G#4 1 0;338 C4 1 0;339 F4 1 0;340 G#4 1 0;341 F4 1 0;340 C4 1 0;342 G#4 1 0;344 G#4 1 0;346 G#4 1 0;343 F4 1 0;345 F4 1 0;347 F4 1 0;342 C4 1 0;344 C4 1 0;346 C4 1 0;348 G#4 1 0;350 G#4 1 0;352 G#4 1 0;354 G#4 1 0;356 G#4 1 0;358 G#4 1 0;348 C4 1 0;350 C4 1 0;352 C4 1 0;354 C4 1 0;356 C4 1 0;358 C4 1 0;349 F4 1 0;351 F4 1 0;353 F4 1 0;355 F4 1 0;357 F4 1 0;359 F4 1 0;336 C6 1 0;336 F6 1 0;348 G6 1 0;354 G#6 1 0;348 G5 1 0;354 G#5 1 0;360 A#5 1 0;360 A#6 1 0;360 A#4 1 0;362 A#4 1 0;364 A#4 1 0;366 A#4 1 0;368 A#4 1 0;370 A#4 1 0;372 A#4 1 0;374 A#4 1 0;376 A#4 1 0;378 A#4 1 0;380 A#4 1 0;382 A#4 1 0;361 F4 1 0;363 F4 1 0;365 F4 1 0;367 F4 1 0;369 F4 1 0;371 F4 1 0;373 F4 1 0;375 F4 1 0;377 F4 1 0;379 F4 1 0;381 F4 1 0;383 F4 1 0;360 C#4 1 0;362 C#4 1 0;364 C#4 1 0;366 C#4 1 0;368 C#4 1 0;370 C#4 1 0;372 C#4 1 0;374 C#4 1 0;376 C#4 1 0;378 C#4 1 0;380 C#4 1 0;382 C#4 1 0;360 C#6 1 0;360 F6 1 0;372 G5 1 0;372 G6 1 0;378 E6 1 0;378 E5 1 0;384 F6 1 0;384 F5 1 0;384 G#5 1 0;384 C6 1 0;384 F2 1 0;384 F3 1 0;386 A#3 1 0;387 C4 1 0;388 A#3 1 0;390 F4 1 0;392 A#3 1 0;394 C4 1 0;396 F3 1 0;396 F2 1 0;398 A#3 1 0;399 C4 1 0;400 A#3 1 0;401 C4 1 0;402 F4 1 0;403 A#3 1 0;404 C4 1 0;405 F4 1 0;406 A#3 1 0;407 C4 1 0;408 G5 1 0;408 G6 1 0;408 A#5 1 0;408 C#6 1 0;408 F3 1 0;408 F2 1 0;410 A#3 1 0;411 C#4 1 0;412 A#3 1 0;413 C#4 1 0;414 G4 1 0;415 A#3 1 0;416 C#4 1 0;417 G4 1 0;418 A#3 1 0;419 C#4 1 0;420 G4 1 0;423 G4 1 0;426 G4 1 0;429 G4 1 0;421 A#3 1 0;422 C#4 1 0;424 A#3 1 0;425 C#4 1 0;427 A#3 1 0;428 C#4 1 0;430 A#3 1 0;431 C#4 1 0;420 A#5 1 0;420 A#6 1 0;426 C#6 1 0;426 C#7 1 0;420 C#6 1 0;420 F6 1 0;426 F6 1 0;426 A#6 1 0;384 F3 1 0;396 F3 1 0;408 F3 1 0;432 C7 1 0;432 C6 1 0;432 F6 1 0;432 G#6 1 0;432 F3 1 0;432 F2 1 0;434 A#3 1 0;435 C4 1 0;436 A#3 1 0;438 F4 1 0;440 A#3 1 0;442 C4 1 0;432 F3 1 0;444 F3 1 0;444 F3 1 0;444 F2 1 0;446 A#3 1 0;447 C4 1 0;448 A#3 1 0;449 C4 1 0;450 F4 1 0;451 A#3 1 0;452 C4 1 0;453 F4 1 0;454 A#3 1 0;455 C4 1 0;444 G6 1 0;444 G5 1 0;450 A#5 1 0;450 A#6 1 0;456 C6 1 0;456 C7 1 0;456 A#6 1 0;456 F6 1 0;456 F3 1 0;456 F2 1 0;456 F3 1 0;458 A#3 1 0;459 C#4 1 0;460 A#3 1 0;461 C#4 1 0;462 G4 1 0;463 A#3 1 0;465 G4 1 0;466 A#3 1 0;464 C#4 1 0;467 C#4 1 0;468 G4 1 0;471 G4 1 0;474 G4 1 0;477 G4 1 0;469 A#3 1 0;470 C#4 1 0;472 A#3 1 0;473 C#4 1 0;475 A#3 1 0;476 C#4 1 0;478 A#3 1 0;479 C#4 1 0;468 C#7 1 0;468 C#6 1 0;468 F6 1 0;468 A#6 1 0;474 C7 1 0;474 A#6 1 0;474 F6 1 0;474 C6 1 0;480 F3 1 0;480 F2 1 0;480 F5 1 0;480 F4 1 0;480 C5 1 0;488 G5 1 0;490 G#5 1 0;492 F5 1 0;492 F4 1 0;492 C#5 1 0;492 C#3 1 0;492 C#4 1 0;488 G4 1 0;490 G#4 1 0;500 G5 1 0;502 G#5 1 0;504 F5 1 0;504 F4 1 0;500 G4 1 0;502 G#4 1 0;504 B4 1 0;504 B3 1 0;504 B2 1 0;512 G5 1 0;514 G#5 1 0;516 F5 1 0;522 E5 1 0;512 G4 1 0;514 G#4 1 0;516 F4 1 0;522 E4 1 0;516 C5 1 0;516 C4 1 0;516 C3 1 0;522 C5 1 0;528 A#3 1 0;528 A#2 1 0;528 F4 1 0;528 F5 1 0;528 A#4 1 0;536 G5 1 0;538 G#5 1 0;540 F5 1 0;536 G4 1 0;538 G#4 1 0;540 F4 1 0;540 G#4 1 0;540 G#3 1 0;540 G#2 1 0;548 G4 1 0;550 G#4 1 0;552 F4 1 0;548 G5 1 0;550 G#5 1 0;552 F5 1 0;552 B4 1 0;552 B3 1 0;552 B2 1 0;560 G5 1 0;562 G#5 1 0;564 F5 1 0;570 E5 1 0;560 G4 1 0;562 G#4 1 0;564 F4 1 0;570 E4 1 0;564 C5 1 0;564 C4 1 0;564 C3 1 0;570 C5 1 0;576 F2 1 0;576 F3 1 0;578 F3 1 0;580 F3 1 0;582 F3 1 0;584 F3 1 0;586 F3 1 0;588 F3 1 0;590 F3 1 0;592 F3 1 0;594 F3 1 0;596 F3 1 0;598 F3 1 0;578 C4 1 0;580 C4 1 0;582 C4 1 0;584 C4 1 0;586 C4 1 0;588 C4 1 0;590 C4 1 0;592 C4 1 0;594 C4 1 0;596 C4 1 0;598 C4 1 0;576 C5 1 0;582 C#5 1 0;588 C5 1 0;576 F4 1 0;582 F4 1 0;588 F4 1 0;594 B4 1 0;594 F4 1 0;600 C5 1 0;606 C#5 1 0;612 C5 1 0;618 B4 1 0;600 F4 1 0;606 F4 1 0;612 F4 1 0;618 F4 1 0;602 C4 1 0;604 C4 1 0;606 C4 1 0;608 C4 1 0;610 C4 1 0;612 C4 1 0;614 C4 1 0;616 C4 1 0;618 C4 1 0;620 C4 1 0;622 C4 1 0;600 F3 1 0;602 F3 1 0;604 F3 1 0;606 F3 1 0;608 F3 1 0;610 F3 1 0;612 F3 1 0;614 F3 1 0;616 F3 1 0;618 F3 1 0;620 F3 1 0;622 F3 1 0;600 F2 1 0;624 F3 1 0;624 F2 1 0;626 C5 1 0;627 C#5 1 0;628 C5 1 0;626 F4 1 0;632 C5 1 0;633 C#5 1 0;634 C5 1 0;632 F4 1 0;630 F3 1 0;630 F2 1 0;648 F3 1 0;654 F3 1 0;648 F2 1 0;654 F2 1 0;650 C5 1 0;651 C#5 1 0;652 C5 1 0;650 F4 1 0;656 C5 1 0;657 C#5 1 0;658 C5 1 0;656 F4 1 0;672 C2 1 17'

tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
beep = PWM(tim, freq=1, duty=50, pin=9)
mySong = music(song, pins=[beep])
gamemusic=True

def on_timer(timer):
    mySong.tick()


def check_direction():
    global gamemusic,beep
    #电阻屏的滑动检测,略痛苦,0无 1上 2下 3左 4右
    x_last = 0
    y_last = 0
    abs_range=60
    move_range=30
    (status,x,y) = ts.read()
    if status==1 and x>220 and x<270 and y>167 and y<217:
        gamemusic=not gamemusic
        if gamemusic:
            beep.duty(50)
            tim2.start()
            img.draw_image(psoundon,220,167)
            lcd.display(img)
        else:
            beep.duty(0)
            tim2.stop()
            img.draw_image(psoundoff,220,167)
            lcd.display(img)
        while status==1:
            (status,x,y) = ts.read()
        return 0
    if status==3:
        x_last = x
        y_last = y
        while status==3:
            (status,x,y) = ts.read()
        if abs(x_last-x)<=abs_range and y_last-y>=move_range:
            return [-1, 0]
        elif abs(x_last-x)<=abs_range and y_last-y<=0-move_range:
            return [1, 0]
        elif x_last-x>=move_range and abs(y_last-y)<=abs_range:
            return [0, -1]
        elif x_last-x<=0-move_range and abs(y_last-y)<=abs_range:
            return [0, 1]
        else:
            return 0
    else:
        return 0


def np_sign(data):   #自行写np.sign()函数
    result=0
    if data<0:
        result=-1
    elif data>0:
        result=1
    else:
        result=0
    return result

def pre_load_image():#done
    imageList = {}
    imagePath = '/sd/resources/score'
    image_filenames = [i for i in os.listdir(imagePath)]
    for name in image_filenames:
        if name.replace('.jpg', '').isdigit():
            images=image.Image(imagePath + '/'+name)
            imageList[name.replace('.jpg', '')] = images
    return imageList


# 加载score图像
def draw_image(score_list, image_list, pos_list):
    for pos_num in score_list:
        score = score_list[pos_num]
        if score <= 4096:
            images = image_list['{}'.format(score)]
        else:
            images = image_list['4096']
        image_rect = pos_list['{}'.format(pos_num)]
        img.draw_image(images,image_rect[0],image_rect[1])
        #if score > 0:
            #image.font_load(image.UTF8, 16, 16, '/sd/resources/2048.Dzk')
            #img.draw_string(image_rect[0],image_rect[1]+18, str(score), x_spacing=-5, mono_space=1,color=(0,0,0))
        lcd.display(img)


def image_pos_list():   #done
    internalWidth=0.1  #unsure!!!!!!!!!!!!!!!!!!
    width=200
    pre_x = 10
    pre_y = 20
    internalLong = math.ceil(internalWidth / 5 * width)
    imageLong = math.floor((1 - internalWidth) / 4 * width)
    posList = dict(zip(list(range(1, 17)), [''] * 16))
    for num in range(1, 17):
        row1, row2 = divmod(num, 4)
        row = row1 + np_sign(row2)
        column = [row2 if row2 != 0 else 4][0]
        image_x = pre_x + internalLong * column + imageLong * (column - 1)
        image_y = pre_y + internalLong * row + imageLong * (row - 1)
        posList['{}'.format(num)] = (image_x, image_y)
    return posList


def pre_move():
    numberPos = {}
    for num in range(1, 17):
        row1, row2 = divmod(num, 4)
        row = row1 + np_sign(row2)
        column = [row2 if row2 != 0 else 4][0]
        numberPos['{}'.format([row, column])] = num
    return numberPos

def number_move(number_pos, move_input, score_list):
    values = list(number_pos.values())
    keys = list(number_pos.keys())
    numberPosReverse = dict(zip(values, keys))
    newScoreList = score_list.copy()
    oldScoreList = {}
    while newScoreList != oldScoreList:
        oldScoreList = newScoreList.copy()
        for num in range(1, 17):
            pos = eval(numberPosReverse[num])
            x, y = pos[0] + move_input[0], pos[1] + move_input[1]
            pos[0] = [x if 1 <= x <= 4 else pos[0]][0]
            pos[1] = [y if 1 <= y <= 4 else pos[1]][0]
            number = number_pos['{}'.format(pos)]
            oldNumberScore = newScoreList[num]
            nextNumberScore = newScoreList[number]
            syn = list(map(lambda x, y: abs(x) * abs(y), move_input, pos))
            # 0值移动
            if nextNumberScore == 0:
                newScoreList[number] = oldNumberScore
                newScoreList[num] = 0
            # 无法移动
            elif num == number:
                pass
            # 合并移动
            elif oldNumberScore == nextNumberScore and num != number:
                newScoreList[number] = 2 * oldNumberScore
                newScoreList[num] = 0
            # 边界移动
            elif oldNumberScore != nextNumberScore and 1 in syn or 4 not in syn:
                pass
            # 非边界移动
            elif oldNumberScore != nextNumberScore and 1 not in syn and 4 not in syn:
                x, y = pos[0] + move_input[0], pos[1] + move_input[1]
                next2NumberScore = newScoreList[number_pos['{}'.format([x, y])]]
                if next2NumberScore != nextNumberScore:
                    pass
                elif next2NumberScore == nextNumberScore:
                    newScoreList[number_pos['{}'.format([x, y])]] = 2 * next2NumberScore
                    newScoreList[number] = oldNumberScore
                    newScoreList[num] = 0

    return newScoreList

def random_score(score_list):
    values = list(score_list.values())
    pro = [2] * (2 + values.count(2)) + [4] * (1 + values.count(4))  # 以当前score图中2或4出现的频率为概率
    blank = [[i if score_list[i] == 0 else 0][0] for i in range(1, 17)]
    blank = list(set(blank))
    blank.remove(0)
    if not blank:
        return 'GameOver'  # 游戏结束
    else:
        score_list[random.choice(blank)] = random.choice(pro)
        return score_list

def record_score(score_list, background):
    totalScore = 0
    values = list(score_list.values())
    for i in values: totalScore += i
    return totalScore

def game_over(score,bg):
    pass

def game_start():
    global gamemusic
    BackGround = (248, 216, 0)  # 灰色
    img.draw_rectangle(0, 0, 320, 240, BackGround, thickness=1, fill=True)
    lcd.display(img)
    gc.collect()
    posList = image_pos_list()
    imageList = pre_load_image()
    scoreList = dict(zip(list(range(1, 17)), [0] * 15 + [2]))  # score表
    numberPos = pre_move()
    scoreList = random_score(scoreList)
    totalScore=0
    # 主循环
    draw_image(scoreList, imageList, posList)  # 绘制得分
    totalScore = record_score(scoreList, BackGround)
    img.draw_image(picon,220,25)
    img.draw_image(pscore,220,117)
    img.draw_string(235, 137, str(totalScore), x_spacing=0, mono_space=1,scale=1,color=(255,255,255))
    while True:
        if scoreList == 'GameOver':
            game_over()
        move_input = check_direction()
        if move_input:
            scoreList = number_move(numberPos, move_input, scoreList)  # 移动数字
            scoreList = random_score(scoreList)  # 在按下按键后生成新的数字
            draw_image(scoreList, imageList, posList)  # 绘制得分
            totalScore = record_score(scoreList, BackGround)
            img.draw_image(pscore,220,117)
            img.draw_string(235, 137, str(totalScore), x_spacing=0, mono_space=1,scale=1,color=(255,255,255))
            move_input = check_direction()
        if gamemusic:
            img.draw_image(psoundon,220,167)
        else:
            img.draw_image(psoundoff,220,167)
        lcd.display(img)
        time.sleep_ms(1)

if __name__ == '__main__':
    fm.register(16, fm.fpioa.GPIO1, force=True)
    btn_clear = GPIO(GPIO.GPIO1, GPIO.IN)
    i2c = I2C(I2C.I2C0, freq=400000, scl=30, sda=31)
    ts.init(i2c)
    lcd.init()
    imagePath = '/sd/resources/score'
    psoundon=image.Image(imagePath + '/'+'soundon.jpg')
    psoundoff=image.Image(imagePath + '/'+'soundoff.jpg')
    picon=image.Image(imagePath + '/'+'icon.jpg')
    pscore=image.Image(imagePath + '/'+'score.jpg')
    img = image.Image()
    tim2 = Timer(Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PERIODIC, period=33, callback=on_timer, arg=on_timer)
    gc.collect()
    game_start()


代码基本都是mpy通用的,想移植到esp32等也不难的。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值