Skip to content

Instantly share code, notes, and snippets.

@41023216
Last active January 6, 2022 12:18
Show Gist options
  • Save 41023216/95c3fc60cf4439d15447e3a81485ca88 to your computer and use it in GitHub Desktop.
Save 41023216/95c3fc60cf4439d15447e3a81485ca88 to your computer and use it in GitHub Desktop.
cp2021
from browser import html
from browser import document as doc
from browser import timer
canvas = html.CANVAS(width = 400, height = 100)
canvas.id = "game-board3"
brython_div = doc["brython_div"]
brython_div <= canvas
brython_div <= html.BUTTON("啟動", id="power")
ctx = canvas.getContext("2d")
px = 0
py = 50
width = 20
height = 20
speedx = 2
speedy = 2
def game():
global px, py, width, height, speedx, speedy
ctx.clearRect(px, py, width, height)
ctx.fillStyle = "red"
px += speedx
if px > 200 :
speedx = 0
py += speedy
if py < 0 or (py + height) > canvas.height :
speedy = -speedy
ctx.fillRect(px, py, width, height)
game()
QQbe = None
def pig(ev):
global QQbe
if QQbe is None:
QQbe = timer.set_interval(game, 10)
doc['power'].text = '暫停'
elif QQbe == 'hold':
QQbe = timer.set_interval(game, 10)
doc['power'].text = '暫停'
else:
timer.clear_interval(QQbe)
QQbe = 'hold'
doc['power'].text = '繼續'
doc["power"].bind("click", pig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment