Advertisement
IsakViste

L1: Stick Hero

Nov 25th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.23 KB | None | 0 0
  1. from UBpygame import *
  2. from random import *
  3. from math import *
  4.  
  5. ## ECRAN
  6. # Creer l'ecran - 640
  7. window = initialize(640, 640, "Stick Hero")
  8.  
  9. # Recuperer la taille de l'ecran
  10. width = window["width"]
  11. height = window["height"]
  12.  
  13. ## VARIABLES
  14. # Couleurs
  15. BROWN = (139,69,19)
  16.  
  17. # Animations and Interactions
  18. interaction={'stick_raising':False, 'restart_game':False}
  19. stick_anim_done_once = False
  20. stick_r = False
  21. restart_g = False
  22. delay_anim = 10
  23.  
  24. # Score
  25. game_over = False
  26. score = 0
  27. game_over_text = "GAME OVER"
  28. game_over_text_w = text_size(game_over_text, 50)[0]//2
  29. game_over_r_text = "Press 'r' to retry"
  30. game_over_r_text_w = text_size(game_over_r_text, 20)[0]//2
  31.  
  32. ## ITEMS
  33. # Plateformes
  34. plateforme_y = height-height//3
  35. plateforme_h = height//3
  36. plateforme_Ini_w = width//8
  37. plateforme_Ini_x = width//8
  38. plateforme_Fin_w = randint(width//8, width//4)
  39. plateforme_Fin_x = randint(plateforme_Ini_x + plateforme_Ini_w + plateforme_Fin_w, width - plateforme_Fin_w - width//16)
  40. plateforme_New_w = 0
  41. plateforme_New_x = 0
  42.  
  43. # Plank
  44. plank_x = plateforme_Ini_x + plateforme_Ini_w
  45. plank_y = plateforme_y
  46. plank_len_ini = 5
  47. plank_len = plank_len_ini
  48.  
  49. ## PLAYER
  50. # Player
  51. player_position = [plateforme_Ini_x + plateforme_Ini_w//2, plateforme_y]
  52.  
  53. # Ninja
  54. ninja_w = 40
  55. ninja_w_f = 10
  56. ninja_h = 60
  57. ninja_h_c = 50
  58. ninja_h_f = 10
  59.  
  60. ## FUNCTION: NEW RANDOM PLATEFORME VALUES
  61. def GetRandomPlateforme():
  62.     global plateforme_New_w, plateforme_New_x
  63.    
  64.     # Valeurs aleatoire pour les
  65.     plateforme_New_w = randint(width//8, width//4)
  66.     plateforme_New_x = randint(plateforme_Ini_x + plateforme_Ini_w + plateforme_Fin_w, width - plateforme_Fin_w - width//16) + plateforme_Fin_x
  67.     if (plateforme_New_x < width):
  68.         plateforme_New_x = width + 1
  69.    
  70.  
  71. ## FUNCTION: CREER LE NINJA
  72. def CreateNinja(pos):
  73.     x, y = pos[0], pos[1]
  74.     # Creer le ninja
  75.     body = create_rectangle((x - (ninja_w//2), y - ninja_h), ninja_w, ninja_h_c, (0,0,0))
  76.     foot_L = create_rectangle((x - (ninja_w//2) + (ninja_w//8), y - ninja_h_f), ninja_w_f, ninja_h_f, (0,0,0))
  77.     foot_R = create_rectangle((x + (ninja_w//8), y - ninja_h_f), ninja_w_f, ninja_h_f, (0,0,0))
  78.     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))
  79.     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))
  80.     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))
  81.     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))
  82.  
  83.     # Dessiner le ninja
  84.     draw_rectangle(body)
  85.     draw_rectangle(foot_L)
  86.     draw_rectangle(foot_R)
  87.     draw_rectangle(eye)
  88.     draw_rectangle(bandana1)
  89.     draw_rectangle(bandana2)
  90.     draw_rectangle(bandana3)
  91.  
  92. ## FUNCTION: CREER LA PLATEFORME
  93. def CreatePlateforme(y, h, ini_w, ini_x, fin_w, fin_x, new_w, new_x):
  94.     ## PLATEFORMES
  95.     # Creer les plateformes
  96.     plateforme_Ini = create_rectangle((ini_x, y), ini_w, h, (80,80,80))
  97.     plateforme_Fin = create_rectangle((fin_x, y), fin_w, h, (80,80,80))
  98.     if (new_x > 0):
  99.         plateforme_New = create_rectangle((new_x, y), new_w, h, (80,80,80))
  100.  
  101.     # Dessiner les plateformes
  102.     draw_rectangle(plateforme_Ini)
  103.     draw_rectangle(plateforme_Fin)
  104.     if (new_x > 0):
  105.         draw_rectangle(plateforme_New)
  106.  
  107. ## FUNCTION: CREER LA PLANCHE
  108. def CreatePlank(start_x, start_y, stop_x, stop_y, c, t):
  109.     plank = create_line((start_x, start_y), (stop_x, stop_y), c, t)
  110.     draw_line(plank)
  111.  
  112. ## FUNCTION: GERER LES ANIMATIONS
  113. def handle_animation(cmd):
  114.     ## GLOBALS
  115.     global plateforme_Ini_x, plateforme_Fin_x, plateforme_New_x
  116.     global plateforme_Ini_w, plateforme_Fin_w, plateforme_New_w
  117.     global stick_anim_done_once, stick_r, plank_len, plank_x, plank_y
  118.     global player_position
  119.     global score
  120.  
  121.     ## CONDITIONS
  122.     # Recommencer (pour tester)
  123.     if (cmd['restart_game']):
  124.         player_position = [plateforme_Ini_x + plateforme_Ini_w//2, plateforme_y]
  125.         plank_len = plank_len_ini
  126.         score = 0
  127.         stick_r = False
  128.         stick_r = False
  129.         stick_anim_done_once = False
  130.         DessinerEcran()
  131.         refresh_window()
  132.         delay(delay_anim)
  133.  
  134.     # Agrandir le baton
  135.     if (cmd['stick_raising']):
  136.         plank_len += 2
  137.         CreatePlank(plank_x, plank_y, plank_x, plank_y - plank_len, BROWN, 5)
  138.         refresh_window()
  139.         delay(delay_anim)
  140.  
  141.     # Faire tomber le baton
  142.     if (not cmd['stick_raising'] and plank_len > 5):
  143.         if (stick_anim_done_once == False):
  144.             # Si la planche atterit sur la plateforme
  145.             if(plank_len > plateforme_Fin_x - plank_x and plank_len < plateforme_Fin_x - plank_x + plateforme_Fin_w):
  146.                 # Faire tourner la planche
  147.                 for d_A in range (-90, 1):
  148.                     r_A = radians(d_A)
  149.                     M = ((plank_x + cos(r_A) * plank_len), (plank_y + sin(r_A) * plank_len))
  150.                     DessinerEcran()
  151.                     CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  152.                     refresh_window()
  153.                     delay(delay_anim)
  154.                    
  155.                 # Faire bouger le joueur a la plateforme
  156.                 for pp in range (player_position[0], plateforme_Fin_x + plateforme_Fin_w//2, 2):
  157.                     M = ((plank_x + cos(0) * plank_len), (plank_y + sin(0) * plank_len))
  158.                     player_position[0] = pp
  159.                     DessinerEcran()
  160.                     CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  161.                     refresh_window()
  162.                     delay(delay_anim)
  163.                    
  164.                 score += 1
  165.                 Ini_x = plateforme_Ini_x
  166.                 GetRandomPlateforme()
  167.  
  168.                 # Faire bouger l'ecran
  169.                 while(plateforme_Fin_x > Ini_x):
  170.                     plateforme_Ini_x -= 3
  171.                     plateforme_Fin_x -= 3
  172.                     plateforme_New_x -= 3
  173.                     player_position[0] -= 3
  174.                     DessinerEcran()
  175.                     if (plank_y < height):
  176.                         plank_y += 4
  177.                         plank_x -= 3
  178.                         M = ((plank_x + cos(0) * plank_len), (plank_y + sin(0) * plank_len))
  179.                         CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  180.                     refresh_window()
  181.                     delay(delay_anim)
  182.                    
  183.                 DessinerEcran()
  184.                 plateforme_Ini_w, plateforme_Ini_x = plateforme_Fin_w, plateforme_Fin_x
  185.                 plateforme_Fin_w, plateforme_Fin_x = plateforme_New_w, plateforme_New_x
  186.                 plateforme_New_w, plateforme_New_x = 0, 0
  187.                 plank_x, plank_y = plateforme_Ini_x + plateforme_Ini_w, plateforme_y
  188.                 plank_len = plank_len_ini
  189.                 stick_anim_done_once = False
  190.                
  191.             # Si la planche va plus loing que la plateforme
  192.             elif(plank_len > plateforme_Fin_x - plank_x):
  193.                 # Faire tourner la planche
  194.                 for d_A in range (-90, 1):
  195.                     r_A = radians(d_A)
  196.                     M = ((plank_x + cos(r_A) * plank_len), (plank_y + sin(r_A) * plank_len))
  197.                     DessinerEcran()
  198.                     CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  199.                     refresh_window()
  200.                     delay(delay_anim)
  201.                    
  202.                 # Faire bouger le joueur a la fin de la planche
  203.                 player_fall = True
  204.                 for pp in range (player_position[0], plank_x + plank_len + ninja_w, 2):
  205.                     if (pp < width):
  206.                         M = ((plank_x + cos(0) * plank_len), (plank_y + sin(0) * plank_len))
  207.                         player_position[0] = pp
  208.                         DessinerEcran()
  209.                         CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  210.                         refresh_window()
  211.                         delay(delay_anim)
  212.                     else:
  213.                         player_fall = False
  214.                        
  215.                 # Faire tomber le joueur
  216.                 if player_fall:
  217.                     for pp in range (player_position[1], height + ninja_h + ninja_h//10, 4):
  218.                         M = ((plank_x + cos(0) * plank_len), (plank_y + sin(0) * plank_len))
  219.                         player_position[1] = pp
  220.                         DessinerEcran()
  221.                         CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  222.                         refresh_window()
  223.                         delay(delay_anim)
  224.                        
  225.                 stick_anim_done_once = True
  226.                
  227.                 # Texte de Game Over
  228.                 draw_text(game_over_text, ((width//2)-game_over_text_w, height//3), (255,0,0), 50)
  229.                 draw_text(game_over_r_text, ((width//2)-game_over_r_text_w, height//3 + height//20), (255,0,0), 20)
  230.                
  231.             # Si la planche n'atteint pas la plateforme
  232.             else:
  233.                 # Faire tourner la planche
  234.                 for d_A in range (-90, 91):
  235.                     r_A = radians(d_A)
  236.                     M = ((plank_x + cos(r_A) * plank_len), (plank_y + sin(r_A) * plank_len))
  237.                     DessinerEcran()
  238.                     CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  239.                     refresh_window()
  240.                     delay(delay_anim)
  241.  
  242.                 # Fair avancer le joueur
  243.                 for pp in range (player_position[0], plateforme_Ini_x + plateforme_Ini_w + ninja_w):
  244.                     M = ((plank_x + cos(radians(90)) * plank_len), (plank_y + sin(radians(90)) * plank_len))
  245.                     player_position[0] = pp
  246.                     DessinerEcran()
  247.                     CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  248.                     refresh_window()
  249.                     delay(delay_anim)
  250.  
  251.                 # Faire tomber le joueur
  252.                 for pp in range (player_position[1], height + ninja_h + ninja_h//10, 4):
  253.                     M = ((plank_x + cos(radians(90)) * plank_len), (plank_y + sin(radians(90)) * plank_len))
  254.                     player_position[1] = pp
  255.                     DessinerEcran()
  256.                     CreatePlank(plank_x, plank_y, M[0], M[1], BROWN, 5)
  257.                     refresh_window()
  258.                     delay(delay_anim)
  259.                    
  260.                 stick_anim_done_once = True
  261.                    
  262.                 # Texte de Game Over
  263.                 draw_text(game_over_text, ((width//2)-game_over_text_w, height//3), (255,0,0), 50)
  264.                 draw_text(game_over_r_text, ((width//2)-game_over_r_text_w, height//3 + height//20), (255,0,0), 20)
  265.  
  266.             stick_r = False
  267.             refresh_window()
  268.        
  269. ## FUNCTION: GERER LES INTERACTIONS
  270. def handle_interaction():
  271.     ## GLOBALS
  272.     global stick_r
  273.     global restart_g
  274.  
  275.     stick_r = False
  276.     restart_g = False
  277.     if (is_key_pressed(K_SPACE) and stick_anim_done_once == False):
  278.         stick_r = True
  279.         if (plank_len > plank_y):
  280.             stick_r = False
  281.     if (is_key_pressed(K_r)):
  282.         restart_g = True
  283.     return ({'stick_raising':stick_r, 'restart_game':restart_g})
  284.  
  285. ## FUNCTION: DESSINER SUR LECRAN
  286. def DessinerEcran():
  287.     ## ECRAN
  288.     # Fond d'ecran blanc
  289.     clear_window()
  290.     bg = create_rectangle((0, 0), width, height, (255, 255, 255))
  291.     draw_rectangle(bg)
  292.  
  293.     CreatePlateforme(plateforme_y, plateforme_h, plateforme_Ini_w, plateforme_Ini_x, plateforme_Fin_w, plateforme_Fin_x, plateforme_New_w, plateforme_New_x)
  294.  
  295.     ## NINJA
  296.     CreateNinja(player_position)
  297.  
  298.     ## SCORE
  299.     tw, th = text_size(str(score), 50)
  300.     draw_text(str(score), ((width//2)-tw//2, height//10), (0,0,0), 50)
  301.  
  302. ## FUNCTION: LE JEU ET SA FENETRE
  303. def StickHero():
  304.     ## GLOBALS
  305.     global interaction
  306.    
  307.     ## ECRAN
  308.     GetRandomPlateforme()
  309.     DessinerEcran()
  310.    
  311.     while(not is_any_quit_event()):
  312.         handle_animation(interaction)
  313.         interaction = handle_interaction()
  314.         refresh_window()
  315.     uninitialize()
  316.  
  317. StickHero()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement