Skip to content

Instantly share code, notes, and snippets.

@c-ching
Last active December 20, 2021 06:51
Show Gist options
  • Save c-ching/210642ce4dfd840af981cf81bf3a7182 to your computer and use it in GitHub Desktop.
Save c-ching/210642ce4dfd840af981cf81bf3a7182 to your computer and use it in GitHub Desktop.
homework one
"""將左右碰撞移動的紅色方塊, 改為先移動到水平畫面中心點位置後, 接著上下碰撞移動"""
from browser import html
from browser import document as doc
import browser.timer
canvas = html.CANVAS(width = 400, height = 100)
canvas.id = "game-board3"
brython_div = doc["brython_div"]
brython_div <= canvas
ctx = canvas.getContext("2d")
px = 0
py = 50
width = 20
height = 20
speed = 2
speedy = 2
def game():
global px, py, width, height, speed, speedy
ctx.clearRect(px, py, width, height)
ctx.fillStyle = "red"
px += speed
if px > 200:
speed = 0
py += speedy
if py < 0 or (py + height) > canvas.height:
speedy = -speedy
ctx.fillRect(px, py, width, height)
browser.timer.set_interval(game, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment