Skip to content

Instantly share code, notes, and snippets.

@aoloe aoloe/collide.py
Last active Nov 8, 2018

Embed
What would you like to do?
WIDTH = 640
HEIGHT = 480
BLACK = (0, 0, 0)
RED = (120, 0, 0)
box = Rect((100, 100), (100, 100), anchor=('left', 'top'))
player = Rect((300, 100), (100, 100), anchor=('left', 'top'))
def update(dt):
x = player.x
y = player.y
dx = 0
dy = 0
if keyboard.left:
dx = -100 * dt
elif keyboard.right:
dx = 100 * dt
elif keyboard.up:
dy = -100 * dt
elif keyboard.down:
dy = 100 * dt
if dx != 0 or dy != 0:
player.x += dx
player.y += dy
if player.colliderect(box):
player.x = x
player.y = y
def draw():
screen.fill((0, 128, 0))
screen.draw.filled_rect(box, BLACK)
screen.draw.filled_rect(player, RED)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.