Created
May 4, 2020 21:25
-
-
Save chris-scientist/6e202147bebd482a2ad84fe6a21171c8 to your computer and use it in GitHub Desktop.
Jump square on Gamebuino META in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gamebuino_meta import begin, waitForUpdate, display, color, buttons | |
# ********************************************************************** | |
# Global variables | |
# Size of character | |
WIDTH_CHARACTER = 2 | |
HEIGHT_CHARACTER = 2 | |
# State of character | |
ON_THE_PLATFORM_STATE = 0 | |
FREE_FALL_STATE = 1 | |
PUSH_FOR_JUMP_STATE = 2 | |
JUMP_STATE = 3 | |
# Platform ID | |
NO_ID = 0 | |
ID_GROUND = 1 | |
# Gravity | |
GRAVITY = 2 | |
# Settings of jumps | |
HORIZONTAL_VELOCITY = 3 | |
INIT_VERTICAL_VELOCITY = 10 | |
# ********************************************************************** | |
# Character management | |
def moveAtLeft(): | |
xOld = character['x'] | |
#if xOld > 0 : | |
if not (character['x'] <= 0) : | |
# Move character at left only if in the size of the screen | |
character['x'] = xOld - 1 | |
character['vx'] = (-1 * HORIZONTAL_VELOCITY) | |
def moveAtRight(): | |
xOld = character['x'] | |
#if (xOld + WIDTH_CHARACTER) < display.width() : | |
if not (character['x'] >= (display.width() - WIDTH_CHARACTER)) : | |
# Move character at right only if in the size of the screen | |
character['x'] = xOld + 1 | |
character['vx'] = HORIZONTAL_VELOCITY | |
def isOnOnePlatform(): | |
return ID_GROUND if ((character['y'] + character['vy']) >= (display.height() - HEIGHT_CHARACTER)) else NO_ID | |
def isOutOfWorld(): | |
if character['x'] <= 0 : | |
character['x'] = 0 | |
return True | |
elif character['x'] >= (display.width() - WIDTH_CHARACTER) : | |
character['x'] = (display.width() - WIDTH_CHARACTER) | |
return True | |
return False | |
# ********************************************************************** | |
# Physics engine | |
def jumpMovement(): | |
character['oldY'] = character['y'] | |
character['vy'] = character['vy'] + GRAVITY | |
character['x'] = character['x'] + character['vx'] | |
character['y'] = character['y'] + character['vy'] | |
def jump(): | |
platformID = isOnOnePlatform() | |
if character['state'] == PUSH_FOR_JUMP_STATE : | |
character['vy'] = character['vy'] - INIT_VERTICAL_VELOCITY | |
character['state'] = JUMP_STATE | |
jumpMovement() | |
elif not ( platformID == NO_ID ) : | |
character['vy'] = 0 | |
character['state'] = ON_THE_PLATFORM_STATE | |
elif isOutOfWorld() : | |
character['state'] = FREE_FALL_STATE | |
else: | |
jumpMovement() | |
def gravity(): | |
platformID = isOnOnePlatform() | |
if platformID == NO_ID : | |
# Free fall | |
character['oldY'] = character['y'] | |
character['vy'] = character['vy'] + GRAVITY | |
character['y'] = character['y'] + character['vy'] | |
character['state'] = FREE_FALL_STATE | |
else: | |
character['vy'] = 0 | |
character['state'] = ON_THE_PLATFORM_STATE | |
# ********************************************************************** | |
# Game management | |
def tick(): | |
# Manage buttons | |
handleButtons() | |
# Manage physics | |
if character['state'] == FREE_FALL_STATE : | |
gravity() | |
elif character['state'] == JUMP_STATE or character['state'] == PUSH_FOR_JUMP_STATE : | |
jump() | |
# Clear screen | |
display.clear() | |
# Draw character | |
display.fillRect(character['x'], character['y'], WIDTH_CHARACTER, HEIGHT_CHARACTER) | |
def handleButtons(): | |
character['vx'] = 0 | |
if buttons.repeat(buttons.LEFT, 1) : | |
moveAtLeft() | |
elif buttons.repeat(buttons.RIGHT, 1) : | |
moveAtRight() | |
if buttons.pressed(buttons.A) and not ( character['state'] == JUMP_STATE ) : | |
character['state'] = PUSH_FOR_JUMP_STATE | |
# ********************************************************************** | |
# Variables of program | |
character = { | |
'x': ((display.width() - WIDTH_CHARACTER) // 2), | |
'y': (display.height() - HEIGHT_CHARACTER), | |
'oldY': (display.height() - HEIGHT_CHARACTER), | |
'vx': 0, | |
'vy': 0, | |
'state': ON_THE_PLATFORM_STATE | |
} | |
# ********************************************************************** | |
# Initialization | |
begin() | |
# ********************************************************************** | |
# Main loop | |
while True: | |
waitForUpdate() | |
tick() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment