mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Initial MPong game commit
This demonstrates native machine code in .mpy files as documented in https://docs.micropython.org/en/latest/develop/natmod.html The idea is to make a simple Pong game to demonstrate input from buttons and touchscreen and output to framebuffer and audio.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "mPong",
|
||||
"publisher": "MicroPythonOS",
|
||||
"short_description": "PingPong game",
|
||||
"long_description": "Demonstrates native machinecode in .mpy files on both AMD64 and ESP32",
|
||||
"icon_url": "https://apps.micropythonos.com/apps/com.micropythonos.mpong/icons/com.micropythonos.mpong_0.1.0_64x64.png",
|
||||
"download_url": "https://apps.micropythonos.com/apps/com.micropythonos.mpong/mpks/com.micropythonos.mpong_0.1.0.mpk",
|
||||
"fullname": "com.micropythonos.mpong",
|
||||
"version": "0.1.0",
|
||||
"category": "games",
|
||||
"activities": [
|
||||
{
|
||||
"entrypoint": "assets/mpong.py",
|
||||
"classname": "MPong",
|
||||
"intent_filters": [
|
||||
{
|
||||
"action": "main",
|
||||
"category": "launcher"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import lvgl as lv
|
||||
from mpos import Activity, DisplayMetrics, InputManager
|
||||
|
||||
indev_error_x = 160
|
||||
indev_error_y = 120
|
||||
|
||||
DARKPINK = lv.color_hex(0xEC048C)
|
||||
|
||||
import sys
|
||||
if sys.platform == "esp32":
|
||||
import mpong_esp32 as mpong
|
||||
else:
|
||||
import mpong_amd64 as mpong
|
||||
|
||||
class MPong(Activity):
|
||||
|
||||
hor_res = 0
|
||||
ver_res = 0
|
||||
layer = None
|
||||
buffer = None
|
||||
|
||||
# Widgets:
|
||||
screen = None
|
||||
canvas = None
|
||||
|
||||
def onCreate(self):
|
||||
self.screen = lv.obj()
|
||||
self.canvas = lv.canvas(self.screen)
|
||||
d = lv.display_get_default()
|
||||
self.hor_res = d.get_horizontal_resolution()
|
||||
self.ver_res = d.get_vertical_resolution()
|
||||
self.canvas.set_size(self.hor_res, self.ver_res)
|
||||
#self.canvas.set_style_bg_color(lv.color_white(), lv.PART.MAIN)
|
||||
self.buffer = bytearray(self.hor_res * self.ver_res * 2)
|
||||
self.canvas.set_buffer(self.buffer, self.hor_res, self.ver_res, lv.COLOR_FORMAT.NATIVE)
|
||||
#self.canvas.fill_bg(lv.color_white(), lv.OPA.COVER)
|
||||
self.canvas.add_flag(lv.obj.FLAG.CLICKABLE)
|
||||
self.canvas.add_event_cb(self.touch_cb, lv.EVENT.ALL, None)
|
||||
self.layer = lv.layer_t()
|
||||
self.canvas.init_layer(self.layer)
|
||||
self.setContentView(self.screen)
|
||||
|
||||
def onResume(self, screen):
|
||||
mpong.init(self.buffer, self.hor_res, self.ver_res)
|
||||
self.refresh_timer = lv.timer_create(self.run_mpong, 10, None)
|
||||
|
||||
def onPause(self, screen):
|
||||
print("stopping it!")
|
||||
if self.refresh_timer:
|
||||
self.refresh_timer.delete()
|
||||
|
||||
def run_mpong(self, timer=None):
|
||||
mpong.render()
|
||||
self.canvas.invalidate() # force redraw
|
||||
|
||||
def touch_cb(self, event):
|
||||
event_code=event.get_code()
|
||||
if event_code not in [19,23,25,26,27,28,29,30,49]:
|
||||
if event_code == lv.EVENT.PRESSING: # this is probably enough
|
||||
x, y = InputManager.pointer_xy()
|
||||
mpong.move_paddle(-10)
|
||||
return
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 7.7 KiB |
Reference in New Issue
Block a user