PYTHON
Welcome to my Sleeping Dragon game!
We have learnt to code with python and this is the more difficult game that I have created.
Sleeping Dragons
This is a pygame zero game in which you have to get hold of the three objects without being touched or killed. This is my modified code:
import math
WIDTH = 800
HEIGHT =600
CENTER_X = WIDTH / 2
CENTER_Y = HEIGHT / 2
CENTER = (CENTER_X, CENTER_Y)
FONT_COLOR = (255, 255, 255)
EGG_TARGET = 20
HERO_START = (200, 300)
ATTACK_DISTANCE = 200
DRAGON_WAKE_TIME = 2
EGG_HIDE_TIME = 2
MOVE_DISTANCE = 5
lives = 3
eggs_collected = 0
game_over = False
game_complete = False
reset_required = False
easy_lair = {
"dragon": Actor("trump-asleep", pos=(600, 100)),
"eggs": Actor("europa", pos=(400, 100)),
"egg_count": 1,
"egg_hidden": False,
"egg_hide_counter": 0,
"sleep_length": 10,
"sleep_counter": 0,
"wake_counter": 0
}
medium_lair = {
"dragon": Actor("trump-asleep", pos=(600, 300)),
"eggs": Actor("usa", pos=(400, 300)),
"egg_count": 2,
"egg_hidden": False,
"egg_hide_counter": 0,
"sleep_length": 7,
"sleep_counter": 0,
"wake_counter": 0
}
hard_lair = {
"dragon": Actor("trump-asleep", pos=(600, 500)),
"eggs": Actor("tierra", pos=(400, 500)),
"egg_count": 3,
"egg_hidden": False,
"egg_hide_counter": 0,
"sleep_length": 4,
"sleep_counter": 0,
"wake_counter": 0
}
lairs = [easy_lair, medium_lair, hard_lair]
greta = Actor("greta", pos=HERO_START)
def draw():
global lairs, eggs_collected, lives, game_complete
screen.clear()
screen.blit("fondotierra", (0, 0))
if game_over:
screen.draw.text("GAME OVER!", fontsize=60, center=CENTER, color=FONT_COLOR)
elif game_complete:
screen.draw.text("YOU WON!", fontsize=60, center=CENTER, color=FONT_COLOR)
else:
greta.draw()
draw_lairs(lairs)
draw_counters(eggs_collected, lives)
def draw_lairs(lairs_to_draw):
for lair in lairs_to_draw:
lair["dragon"].draw()
if lair["egg_hidden"] is False:
lair["eggs"].draw()
def draw_counters(eggs_collected, lives):
#screen.blit("egg-count", (0, HEIGHT - 30))
screen.draw.text(str(eggs_collected),
fontsize=40,
pos=(30, HEIGHT - 30),
color=FONT_COLOR)
screen.blit("life-count", (60, HEIGHT - 30))
screen.draw.text(str(lives),
fontsize=40,
pos=(90, HEIGHT - 30),
color=FONT_COLOR)
screen.draw.text(str(lives),
fontsize=40,
pos=(90, HEIGHT - 30),
color=FONT_COLOR)
def update():
if keyboard.right:
greta.x += MOVE_DISTANCE
if greta.x > WIDTH:
greta.x = WIDTH
elif keyboard.left:
greta.x -= MOVE_DISTANCE
if greta.x < 0:
greta.x = 0
elif keyboard.down:
greta.y += MOVE_DISTANCE
if greta.y > HEIGHT:
greta.y = HEIGHT
elif keyboard.up:
greta.y -= MOVE_DISTANCE
if greta.y < 0:
greta.y = 0
check_for_collisions()
def update_lairs():
global lairs, greta, lives
for lair in lairs:
if lair["dragon"].image == "trump-asleep":
update_sleeping_dragon(lair)
elif lair["dragon"].image == "trump-awake":
update_waking_dragon(lair)
update_egg(lair)
clock.schedule_interval(update_lairs, 1)
def update_sleeping_dragon(lair):
if lair["sleep_counter"] >= lair["sleep_length"]:
lair["dragon"].image = "trump-awake"
lair["sleep_counter"] = 0
else:
lair["sleep_counter"] += 1
def update_waking_dragon(lair):
if lair["wake_counter"] >= DRAGON_WAKE_TIME:
lair["dragon"].image = "trump-asleep"
lair["wake_counter"] = 0
else:
lair["wake_counter"] += 1
def update_egg(lair):
if lair["egg_hidden"] is True:
if lair["egg_hide_counter"] >= EGG_HIDE_TIME:
lair["egg_hidden"] = False
lair["egg_hide_counter"] = 0
else:
lair["egg_hide_counter"] += 1
def check_for_collisions():
global lairs, eggs_colledted, lives, reset_required, game_complete
for lair in lairs:
if lair["egg_hidden"] is False:
check_for_egg_collision(lair)
if lair["dragon"].image == "trump-awake" and reset_required is False:
check_for_dragon_collision(lair)
def check_for_dragon_collision(lair):
x_distance = greta.x - lair["dragon"].x
y_distance = greta.y - lair["dragon"].y
distance = math.hypot(x_distance, y_distance)
if distance < ATTACK_DISTANCE:
handle_dragon_collision()
def handle_dragon_collision():
global reset_required
reset_required = True
animate(greta, pos=HERO_START, on_finished=subtract_life)
def check_for_egg_collision(lair):
global eggs_collected, game_complete
if greta.colliderect(lair["eggs"]):
lair["egg_hidden"] = True
eggs_collected += lair["egg_count"]
if eggs_collected >= EGG_TARGET:
game_complete = True
def subtract_life():
global lives, reset_required, game_over
lives -=1
if lives == 0:
game_over = True
reset_required = False
This is a videotutorial showing how to create a basic game named sleeping dragons.py: