mario = codesters.Sprite("mario_63a")
mario.set_size(.06)
mario.go_to(-225, 0)
enemies = []
y = 225
for counter in range(5):
speed = random.randint(1, 4)
x = random.randrange(-150, 200, 50)
enemy = codesters.Rectangle(x, y, 25, 75, "red")
y-= 75
enemy.set_y_speed(speed)
enemies.append(enemy)
prizes = []
y2 = 225
for counter in range(5):
x2 = random.randrange(-150, 200, 25)
prize = codesters.Sprite("coin",x2, y2 )
y2 -= 75
prizes.append(prize)
lives = 5
life_disp = codesters.Display(lives, 225, 225)
points = 0
points_disp = codesters.Display(points, 225, -225)
'''The first line sets up the collision event.
There's no need to change the placeholder names
in the paretheses. The event handler at the bottom determines which is
the sprite and which is the hit_sprite when the collision occurs'''
def collision(sprite, hit_sprite):
# this line gets the color of whatever sprite the
# hero has collided with. If mario collides with a rectangle,
# my_var will equal "red". If it collides with a coin, my_var will
# equal "black". Black is the default sprite color if a different color is
#not assigned. It's actually the color that the sprite will draw in.
my_var = hit_sprite.get_color()
#There's an important Python concept/structure called scope. We can talk
#more about it another time. For now, let's say that global lets the variable
#lives be treated the same inside and outside the event.
global lives
#same - but for the variable points
global points
# This checks whether mario collided with something red. If he did,
# it does all the stuff in the following if block.
if my_var == "red":
#loses a life
lives -= 1
#updates the lives scoreboard
life_disp.update(lives)
# goes to the starting point
sprite.go_to(-225, 0)
#mario says ouch for .5 seconds
sprite.say("Ouch!", .5)
#This checks whether mario collided with something black. If he did,
#it does all of the stuff in this if block.
if my_var == "black":
#mario says score! for .5 seconds
stage.remove_sprite(hit_sprite)
sprite.say("Score!", .5)
#adds a point
points += 1
#updates the points scoreboard
points_disp.update(points)
if lives <= 0:
sprite.go_to(0, 0)
sprite.say("Game over")
for enemy in enemies:
enemy.hide()
for prize in prizes:
prize.hide()
# add any other actions...
if points > 4:
sprite.go_to(0, 0)
sprite.say("You win!")
for enemy in enemies:
enemy.hide()
for prize in prizes:
prize.hide()
'''This is the event handler. The name in front of the command assigns
the "main actor" in the event. When this game runs, mario will be the
sprite and anything he collides with will be the hit_sprite'''
mario.event_collision(collision)
def left_key():
mario.move_left(20)
# add other actions...
stage.event_key("left", left_key)
def right_key():
mario.move_right(20)
# add other actions...
stage.event_key("right", right_key)
def up_key():
mario.move_up(20)
# add other actions...
stage.event_key("up", up_key)
def down_key():
mario.move_down(20)
# add other actions...
stage.event_key("down", down_key)
-
Run Code
-
-
Stop Running Code
-
Show Chart
-
Show Console
-
Codesters How To (opens in a new tab)