Skip to content

Instantly share code, notes, and snippets.

@AnthonyBriggs
Created September 10, 2018 13:32
Show Gist options
  • Save AnthonyBriggs/f8b4d53cf9387e73fab5badb9cc06417 to your computer and use it in GitHub Desktop.
Save AnthonyBriggs/f8b4d53cf9387e73fab5badb9cc06417 to your computer and use it in GitHub Desktop.
Mu/PygameZero/Kenney.nl demo code
"""
Demo code for Mu/PygameZero using kenney.nl graphics.
There's a short video demo at https://www.youtube.com/watch?v=b2IPNCJtUL4.
NOTE: this relies on two patches to Mu which aren't yet in
the main PgZero repository, one which adds joystick
support, and another which adds a flip property to
the Actor class.
"""
# Alien graphics from kenney.nl's platformer art deluxe pack
# https://kenney.nl/assets/platformer-art-deluxe
alien = Actor('p3_stand')
alien.pos = 100,100
alien.speed = [0, 0]
alien.move_distance = 0
alien.facing_left = False
alien.jumping = False
alien.jump_speed = (0,0)
alien.start_jump_y = 0
# laser sprite from rawdanitsu at opengameart.org:
# https://opengameart.org/content/lasers-and-beams
laser = Actor('laser')
laser.firing = False
laser.speed = 0
WIDTH = 800
HEIGHT = 600
def draw():
screen.clear()
screen.fill((0,0,0))
if laser.firing:
laser.draw()
if alien.jumping:
alien.image = 'p3_jump'
elif sum(alien.speed) == 0:
alien.image = 'p3_stand'
else:
# update walking frame every 12 pixels
walk_pos = (int(alien.move_distance / 12)) % 11 + 1
alien.image = 'p3_walk{:0>2}'.format(walk_pos)
alien.draw()
def update():
if alien.jumping:
alien.x += alien.jump_speed[0]
alien.y += alien.jump_speed[1]
alien.jump_speed[1] += 1.0
if alien.y > alien.start_jump_y:
# below our starting jump height, so stop
alien.jumping = False
alien.y = alien.start_jump_y
else:
if alien.speed == (0,0):
# stopped
alien.move_distance = 0 # reset walk distance
else:
# moving
alien.x += alien.speed[0]
alien.y += alien.speed[1]
alien.move_distance += abs(alien.speed[0]) + abs(alien.speed[1])
if alien.speed[0] < 0:
alien.facing_left = True
elif alien.speed[0] > 0:
alien.facing_left = False
if alien.right > WIDTH:
alien.right = WIDTH
if alien.left < 0:
alien.left = 0
if not alien.jumping:
# ^^ this lets the alien pop temporarily out of the top of the screen,
# rather than hit their head on the border.
if alien.top < 0:
alien.top = 0
if alien.bottom > HEIGHT:
alien.bottom = HEIGHT
alien.flip = alien.facing_left
laser.x += laser.speed
if not screen.bounds().colliderect(laser):
laser.firing = False
laser.speed = 0
def on_joy_button_down(joy, button):
print("Mu button down:", joy, button)
#print(alien.speed)
if button == joybutton.ZERO:
laser.pos = alien.pos
laser.firing = True
laser.speed = 40
if alien.facing_left:
laser.speed *= -1
if button == joybutton.ONE and not alien.jumping:
alien.jumping = True
# jump height is proportional to how fast we run, but should
# be a minimum of half run speed
jumpy = 10 + abs(alien.speed[0])
alien.jump_speed = [alien.speed[0], -jumpy]
alien.start_jump_y = alien.y + 0.1
def on_joy_button_up(joy, button):
print("Mu button up:", joy, button)
def sanitise_axis(value):
# make a small 'dead spot' in the middle or we'll drift
if -0.05 < value < 0.05:
return 0
else:
return value
def on_joy_axis_motion(joy, axis, value):
print("Mu joystick move:", joy, axis, value)
if axis == axis.X:
alien.speed[0] = sanitise_axis(value) * 10
if axis == axis.Y:
alien.speed[1] = sanitise_axis(value) * 10
def on_mouse_down():
print("Mouse button clicked!")
def on_joy_hat_motion(joy, hat, value):
print(joy, hat, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment