Skip to content

Instantly share code, notes, and snippets.

@kylefritz
Created October 16, 2018 14:58
Show Gist options
  • Save kylefritz/873149d9cdc36e5e103d747d9a5224f1 to your computer and use it in GitHub Desktop.
Save kylefritz/873149d9cdc36e5e103d747d9a5224f1 to your computer and use it in GitHub Desktop.
Alien shuffle for pygame zero
import random
alien = Actor('alien')
alien.topright = 0, 10
# these pieces of state needed to be attached to an actor to be updated...
alien.score = 0
alien.screen_color = 128, 80, 0
bad_alien = Actor('alien')
bad_alien.topright = 400, 10
WIDTH = 500
HEIGHT = alien.height + 20
def draw():
screen.fill(alien.screen_color)
alien.draw()
bad_alien.draw()
screen.draw.text("score %d" % (alien.score), topleft=(10, 10))
def update():
if keyboard.left:
if alien.right > 0:
alien.left -= 10
elif keyboard.right:
alien.left += 10
alien.left += 2
if alien.left > WIDTH:
alien.score += 5
alien.right = 0
if alien.colliderect(bad_alien):
set_alien_hurt()
bad_alien.top += 1
if bad_alien.top > HEIGHT:
bad_alien.bottom = -100
def on_mouse_down(pos):
if alien.collidepoint(pos):
set_alien_hurt()
def set_alien_hurt():
if alien.image == 'alien':
alien.score -= 20
sounds.eep.play()
alien.image = 'alien_hurt'
clock.schedule_unique(reset_alien, 1.0)
change_background()
def reset_alien():
alien.image = 'alien'
def change_background():
alien.screen_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
clock.schedule_interval(change_background, 10.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment