MicroPython动手做(35)——体验小游戏-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

MicroPython动手做(35)——体验小游戏

[复制链接]
 楼主| 发表于 2020-6-28 12:34 | 显示全部楼层
mPython X 实验图形编程

02.jpg
 楼主| 发表于 2020-6-28 13:56 | 显示全部楼层
05.gif
 楼主| 发表于 2020-6-28 14:16 | 显示全部楼层
15、滚雪球(大于)


[mw_shl_code=arduino,true]#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) ** 2 + (y - my_listy) ** 2) <= r:
            r = r + 2
            my_listx = (-1)
            my_listy = (-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]
 楼主| 发表于 2020-6-28 14:34 | 显示全部楼层
11.gif
 楼主| 发表于 2020-6-28 14:40 | 显示全部楼层
mPython X 实验图形编程

12.jpg
 楼主| 发表于 2020-6-28 14:42 | 显示全部楼层
mPython X 实验图形编程2

13.jpg
 楼主| 发表于 2020-7-6 16:28 | 显示全部楼层
17、石头剪刀布(阿勇)


  1. #MicroPython动手做(35)——小游戏
  2. #石头剪刀布

  3. from mpython import *
  4. import time
  5. import random
  6. from machine import Timer

  7. def on_button_a_down(_):
  8.     global y, x, k, j
  9.     time.sleep_ms(10)
  10.     if button_a.value() == 1: return
  11.     x = random.randint(1, 3)
  12.     xianshi(x, 10)

  13. def on_button_b_down(_):
  14.     global y, x, k, j
  15.     time.sleep_ms(10)
  16.     if button_b.value() == 1: return
  17.     y = random.randint(1, 3)
  18.     xianshi(y, 90)

  19. _status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
  20. def on_touchpad_P_pressed():pass
  21. def on_touchpad_P_unpressed():pass
  22. def on_touchpad_Y_pressed():pass
  23. def on_touchpad_Y_unpressed():pass
  24. def on_touchpad_T_pressed():pass
  25. def on_touchpad_T_unpressed():pass
  26. def on_touchpad_H_pressed():pass
  27. def on_touchpad_H_unpressed():pass
  28. def on_touchpad_O_pressed():pass
  29. def on_touchpad_O_unpressed():pass
  30. def on_touchpad_N_pressed():pass
  31. def on_touchpad_N_unpressed():pass

  32. tim12 = Timer(12)

  33. def timer12_tick(_):
  34.     global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
  35.     try:
  36.         touchPad_P.read();pass
  37.     except:
  38.         return
  39.     if touchPad_P.read() < 400:
  40.         if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
  41.     elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
  42.     if touchPad_Y.read() < 400:
  43.         if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
  44.     elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
  45.     if touchPad_T.read() < 400:
  46.         if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
  47.     elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
  48.     if touchPad_H.read() < 400:
  49.         if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
  50.     elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
  51.     if touchPad_O.read() < 400:
  52.         if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
  53.     elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
  54.     if touchPad_N.read() < 400:
  55.         if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
  56.     elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

  57. tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

  58. def on_touchpad_P_pressed():
  59.     global y, x, k, j
  60.     x = 0
  61.     y = 0
  62.     oled.fill(0)
  63.     oled.DispChar("玩家1按A键玩家2按B键", 0, 0, 1)
  64.     oled.show()

  65. def xianshi(j, k):
  66.     global y, x
  67.     if j == 1:
  68.         oled.blit(image_picture.load('face/rock_s.pbm', 0), k, 17)
  69.     if j == 2:
  70.         oled.blit(image_picture.load('face/scissors_s.pbm', 0), k, 17)
  71.     if j == 3:
  72.         oled.blit(image_picture.load('face/paper_s.pbm', 0), k, 17)
  73.     oled.show()

  74. def panduan():
  75.     global y, x, k, j
  76.     if x == y:
  77.         oled.DispChar(" 打平", 50, 50, 1)
  78.     else:
  79.         if x - y == -1 or x - y == 2:
  80.             oled.DispChar("玩家1赢", 50, 50, 1)
  81.         else:
  82.             oled.DispChar("玩家2赢", 50, 50, 1)
  83.     oled.show()

  84. random.seed(time.ticks_cpu())

  85. button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)

  86. button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)

  87. image_picture = Image()


  88. oled.fill(0)
  89. oled.DispChar("     石头剪刀布小游戏", 0, 0, 1)
  90. oled.DispChar("玩家1按A键玩家2按B键", 0, 16, 1)
  91. oled.DispChar(" 系统自动判断谁输谁赢", 0, 32, 1)
  92. oled.DispChar("         按P键开始玩", 0, 48, 1)
  93. oled.show()
  94. x = 0
  95. y = 0
  96. while True:
  97.     if x != 0 and y != 0:
  98.         panduan()
复制代码


 楼主| 发表于 2020-7-6 16:42 | 显示全部楼层
08.gif
 楼主| 发表于 2020-7-6 16:44 | 显示全部楼层
mPython X 实验图形编程1


01.jpg
 楼主| 发表于 2020-7-6 16:47 | 显示全部楼层
mPython X 实验图形编程2

02.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-28 01:00 , Processed in 0.085291 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表