from UBpygame import *
from random import *
from math import *
## ECRAN
# Creer l'ecran - 640
window = initialize(640, 640, "Stick Hero")
# Recuperer la taille de l'ecran
width = window["width"]
height = window["height"]
## VARIABLES
# Couleurs
BROWN = (139,69,19)
# Animations and Interactions
interaction={'stick_raising':False, 'restart_game':False}
stick_anim_done_once = False
stick_r = False
restart_g = False
delay_anim = 10
# Score
game_over = False
score = 0
game_over_text = "GAME OVER"
game_over_text_w = text_size(game_over_text, 50)[0]//2
game_over_r_text = "Press 'r' to retry"
game_over_r_text_w = text_size(game_over_r_text, 20)[0]//2
## ITEMS
# Plateformes
plateforme_y = height-height//3
plateforme_h = height//3
plateforme_Ini_w = width//8
plateforme_Ini_x = width//8
plateforme_Fin_w = randint(width//8, width//4)
plateforme_Fin_x = randint(plateforme_Ini_x + plateforme_Ini_w + plateforme_Fin_w, width - plateforme_Fin_w - width//16)
plateforme_New_w = 0
plateforme_New_x = 0
# Plank
plank_x = plateforme_Ini_x + plateforme_Ini_w
plank_y = plateforme_y
plank_len_ini = 5
plank_len = plank_len_ini
## PLAYER
# Player
player_position = [plateforme_Ini_x + plateforme_Ini_w//2, plateforme_y]
# Ninja
ninja_w = 40
ninja_w_f = 10
ninja_h = 60
ninja_h_c = 50
ninja_h_f = 10
## FUNCTION: NEW RANDOM PLATEFORME VALUES
def GetRandomPlateforme():
global plateforme_New_w, plateforme_New_x
# Valeurs aleatoire pour les
plateforme_New_w = randint(width//8, width//4)
plateforme_New_x = randint(plateforme_Ini_x + plateforme_Ini_w + plateforme_Fin_w, width - plateforme_Fin_w - width//16) + plateforme_Fin_x
if (plateforme_New_x < width):
plateforme_New_x = width + 1
## FUNCTION: CREER LE NINJA
def CreateNinja(pos):
x, y = pos[0], pos[1]
# Creer le ninja
body = create_rectangle((x - (ninja_w//2), y - ninja_h), ninja_w, ninja_h_c, (0,0,0))
foot_L = create_rectangle((x - (ninja_w//2) + (ninja_w//8), y - ninja_h_f), ninja_w_f, ninja_h_f, (0,0,0))
foot_R = create_rectangle((x + (ninja_w//8), y - ninja_h_f), ninja_w_f, ninja_h_f, (0,0,0))
eye = create_rectangle((x + (ninja_w//5), y - ninja_h + (ninja_h_c//6) + (ninja_h_c//5)), ninja_w//5, ninja_h_c//8, (255, 255, 255))
bandana1 = create_rectangle((x - (ninja_w//2) - (ninja_w//10), y - ninja_h + (ninja_h_c//6)), ninja_w + (ninja_w//5), ninja_h_c//5, (255, 0, 0))
bandana2 = create_rectangle((x - ninja_w + (ninja_w//8), y - ninja_h + (ninja_h_c//5)), ninja_w_f, ninja_h_f - (ninja_h_f//10), (255, 0, 0))
bandana3 = create_rectangle((x - ninja_w + (ninja_w//8) + (ninja_w_f//10), y - ninja_h + (ninja_h_c//5) + (ninja_h_c//5)), ninja_w_f - (ninja_w_f//10), ninja_h_f, (255, 0, 0))
# Dessiner le ninja
draw_rectangle(body)
draw_rectangle(foot_L)
draw_rectangle(foot_R)
draw_rectangle(eye)
draw_rectangle(bandana1)
draw_rectangle(bandana2)
draw_rectangle(bandana3)
## FUNCTION: CREER LA PLATEFORME
def CreatePlateforme(y, h, ini_w, ini_x, fin_w, fin_x, new_w, new_x):
## PLATEFORMES
# Creer les plateformes
plateforme_Ini = create_rectangle((ini_x, y), ini_w, h, (80,80,80))
plateforme_Fin = create_rectangle((fin_x, y), fin_w, h, (80,80,80))
if (new_x > 0):
plateforme_New = create_rectangle((new_x, y), new_w, h, (80,80,80))
# Dessiner les plateformes
draw_rectangle(plateforme_Ini)
draw_rectangle(plateforme_Fin)
if (new_x > 0):
draw_rectangle(plateforme_New)
## FUNCTION: CREER LA PLANCHE
def CreatePlank(start_x, start_y, stop_x, stop_y, c, t):
plank = create_line((start_x, start_y), (stop_x, stop_y), c, t)
draw_line(plank)
## FUNCTION: GERER LES ANIMATIONS
def handle_animation(cmd):
## GLOBALS
global plateforme_Ini_x, plateforme_Fin_x, plateforme_New_x
global plateforme_Ini_w, plateforme_Fin_w, plateforme_New_w
global stick_anim_done_once, stick_r, plank_len, plank_x, plank_y
global player_position
global score
## CONDITIONS
# Recommencer (pour tester)
if (cmd['restart_game']):
player_position = [plateforme_Ini_x + plateforme_Ini_w//2, plateforme_y]
plank_len = plank_len_ini
score = 0
stick_r = False
stick_r = False
stick_anim_done_once = False
DessinerEcran()
refresh_window()
delay(delay_anim)
# Agrandir le baton
if (cmd['stick_raising']):
plank_len += 2
CreatePlank(plank_x, plank_y, plank_x, plank_y - plank_len, BROWN, 5)
refresh_window()
delay(delay_anim)
# Faire tomber le baton
if (not cmd['stick_raising'] and plank_len > 5):
if (stick_anim_done_once == False):
# Si la planche atterit sur la plateforme
if(plank_len > plateforme_Fin_x - plank_x and plank_len < plateforme_Fin_x - plank_x + plateforme_Fin_w):
# Faire tourner la planche
for d_A in range (-90, 1):
r_A = radians(d_A)
M = ((plank_x + cos(r_A) * plank_len), (plank_y + sin(r_A) * plank_len))
DessinerEcran()
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
# Faire bouger le joueur a la plateforme
for pp in range (player_position[0], plateforme_Fin_x + plateforme_Fin_w//2, 2):
M = ((plank_x + cos(0) * plank_len), (plank_y + sin(0) * plank_len))
player_position[0] = pp
DessinerEcran()
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
score += 1
Ini_x = plateforme_Ini_x
GetRandomPlateforme()
# Faire bouger l'ecran
while(plateforme_Fin_x > Ini_x):
plateforme_Ini_x -= 3
plateforme_Fin_x -= 3
plateforme_New_x -= 3
player_position[0] -= 3
DessinerEcran()
if (plank_y < height):
plank_y += 4
plank_x -= 3
M = ((plank_x + cos(0) * plank_len), (plank_y + sin(0) * plank_len))
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
DessinerEcran()
plateforme_Ini_w, plateforme_Ini_x = plateforme_Fin_w, plateforme_Fin_x
plateforme_Fin_w, plateforme_Fin_x = plateforme_New_w, plateforme_New_x
plateforme_New_w, plateforme_New_x = 0, 0
plank_x, plank_y = plateforme_Ini_x + plateforme_Ini_w, plateforme_y
plank_len = plank_len_ini
stick_anim_done_once = False
# Si la planche va plus loing que la plateforme
elif(plank_len > plateforme_Fin_x - plank_x):
# Faire tourner la planche
for d_A in range (-90, 1):
r_A = radians(d_A)
M = ((plank_x + cos(r_A) * plank_len), (plank_y + sin(r_A) * plank_len))
DessinerEcran()
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
# Faire bouger le joueur a la fin de la planche
player_fall = True
for pp in range (player_position[0], plank_x + plank_len + ninja_w, 2):
if (pp < width):
M = ((plank_x + cos(0) * plank_len), (plank_y + sin(0) * plank_len))
player_position[0] = pp
DessinerEcran()
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
else:
player_fall = False
# Faire tomber le joueur
if player_fall:
for pp in range (player_position[1], height + ninja_h + ninja_h//10, 4):
M = ((plank_x + cos(0) * plank_len), (plank_y + sin(0) * plank_len))
player_position[1] = pp
DessinerEcran()
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
stick_anim_done_once = True
# Texte de Game Over
draw_text(game_over_text, ((width//2)-game_over_text_w, height//3), (255,0,0), 50)
draw_text(game_over_r_text, ((width//2)-game_over_r_text_w, height//3 + height//20), (255,0,0), 20)
# Si la planche n'atteint pas la plateforme
else:
# Faire tourner la planche
for d_A in range (-90, 91):
r_A = radians(d_A)
M = ((plank_x + cos(r_A) * plank_len), (plank_y + sin(r_A) * plank_len))
DessinerEcran()
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
# Fair avancer le joueur
for pp in range (player_position[0], plateforme_Ini_x + plateforme_Ini_w + ninja_w):
M = ((plank_x + cos(radians(90)) * plank_len), (plank_y + sin(radians(90)) * plank_len))
player_position[0] = pp
DessinerEcran()
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
# Faire tomber le joueur
for pp in range (player_position[1], height + ninja_h + ninja_h//10, 4):
M = ((plank_x + cos(radians(90)) * plank_len), (plank_y + sin(radians(90)) * plank_len))
player_position[1] = pp
DessinerEcran()
CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
refresh_window()
delay(delay_anim)
stick_anim_done_once = True
# Texte de Game Over
draw_text(game_over_text, ((width//2)-game_over_text_w, height//3), (255,0,0), 50)
draw_text(game_over_r_text, ((width//2)-game_over_r_text_w, height//3 + height//20), (255,0,0), 20)
stick_r = False
refresh_window()
## FUNCTION: GERER LES INTERACTIONS
def handle_interaction():
## GLOBALS
global stick_r
global restart_g
stick_r = False
restart_g = False
if (is_key_pressed(K_SPACE) and stick_anim_done_once == False):
stick_r = True
if (plank_len > plank_y):
stick_r = False
if (is_key_pressed(K_r)):
restart_g = True
return ({'stick_raising':stick_r, 'restart_game':restart_g})
## FUNCTION: DESSINER SUR LECRAN
def DessinerEcran():
## ECRAN
# Fond d'ecran blanc
clear_window()
bg = create_rectangle((0, 0), width, height, (255, 255, 255))
draw_rectangle(bg)
CreatePlateforme(plateforme_y, plateforme_h, plateforme_Ini_w, plateforme_Ini_x, plateforme_Fin_w, plateforme_Fin_x, plateforme_New_w, plateforme_New_x)
## NINJA
CreateNinja(player_position)
## SCORE
tw, th = text_size(str(score), 50)
draw_text(str(score), ((width//2)-tw//2, height//10), (0,0,0), 50)
## FUNCTION: LE JEU ET SA FENETRE
def StickHero():
## GLOBALS
global interaction
## ECRAN
GetRandomPlateforme()
DessinerEcran()
while(not is_any_quit_event()):
handle_animation(interaction)
interaction = handle_interaction()
refresh_window()
uninitialize()
StickHero()
Comments
0 B
|👍
/👎