ren811

CloudDodge.py

Jan 20th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.15 KB | None | 0 0
  1. # coding: utf-8
  2. import pygame, sys, os
  3. from math import sqrt
  4. from pygame.locals import *
  5. from GIFImage import *
  6. from random import randint
  7.  
  8. # inicia PyGame
  9. pygame.init()
  10. fpsClock = pygame.time.Clock()
  11.  
  12. # valores para resolução do game / tamanho da janela
  13. screen_X = 600
  14. screen_Y = 450
  15.  
  16. # define a janela do jogo e o nome dela
  17. screen = pygame.display.set_mode((screen_X,screen_Y), 0, 32)
  18. pygame.display.set_caption("Clouddodge")
  19.  
  20. # timer para instanciar as nuvens
  21. instanciaNuvens = 26
  22. pygame.time.set_timer(instanciaNuvens, 2000) # default = 2000
  23.  
  24. # cores para o plano de fundo
  25. WHITE = (255,255,255)
  26. NOITE = ( 25, 25,112)
  27. DIA   = (135,206,250)
  28.  
  29. # diretório do script e dos arquivos
  30. current_dir = os.getcwd() + "\\"
  31.  
  32. # outras cores
  33. azul_claro  = (51,204,255)
  34. verde_claro = (102,255,51)
  35. laranja     = (255,204,51)
  36. morango     = (255,51,102)
  37. marrom      = (184,138,0)
  38. verde_agua  = (51,255,204)
  39. roxo        = (204,51,255)
  40.  
  41. # posição de Rainbow Dash no começo do jogo
  42.  
  43. ponyx = 10
  44. ponyy = 150
  45.  
  46. dir = "idle" # por padrão, até que o jogador altere ao movê-la
  47.  
  48. # define o canto direito da tela para instanciar novas nuvens
  49. canto = screen_Y + 200
  50.  
  51. # lista para armazenar os objetos nuvem
  52. nuvens = []
  53.  
  54. # imagens
  55. pony = GIFImage(current_dir + "fly_rainbow_right.gif")
  56. thunder_cloud = GIFImage(current_dir + "thunder_cloud.gif")
  57. normal_cloud = GIFImage(current_dir + "normal_cloud.gif")
  58.  
  59.  
  60. # variáveis que deslocam nuvens e Rainbow Dash
  61. movimento = 4                 # nuvens
  62. movePony = movimento * 2      # Rainbow Dash
  63.  
  64. # texto para testes
  65. fonte = pygame.font.SysFont("Arial", 15)
  66.  
  67.  
  68. class Nuvem:
  69.     def __init__(self, x, y):
  70.         # posição da imagem
  71.         self.x = x
  72.         self.y = y
  73.         # topo
  74.         self.p1x = self.x + 53
  75.         self.p1y = self.y + 17
  76.         # esquerda
  77.         self.p2x = self.x + 17
  78.         self.p2y = self.y + 51
  79.         # direita
  80.         self.p3x = self.x + 84
  81.         self.p3y = self.y + 51
  82.        
  83.         # sorteia aleatoriamente o tipo de nuvem (normal ou com raios
  84.         self.tipo = randint(0,1)
  85.         if (self.tipo == 0):
  86.             self.img = thunder_cloud
  87.             # trovoes
  88.             # esquerda
  89.             self.trovao1x = self.x + 17
  90.             self.trovao1y = self.y + 82
  91.             # direita
  92.             self.trovao2x = self.x + 66
  93.             self.trovao2y = self.y + 80
  94.         else:
  95.             self.img = normal_cloud
  96.  
  97. def instanciaNuvem():
  98.     x = canto
  99.     y = randint(0,230)
  100.     nuvem = Nuvem(x,y)
  101.     nuvens.append(nuvem)
  102.  
  103. def checa_nuvens():
  104.     for i in range(len(nuvens)):
  105.         if nuvens[i].x <= -110:
  106.             del nuvens[i]
  107.             break
  108.  
  109.     # # hitbox Rainbow Dash
  110.     # 100, 25
  111.     # 104, 47
  112.     # 102, 66
  113. def testa_colisao():
  114.    
  115.     RD_r  = 18 # raio das 'HitCircles' da Rainbow Dash
  116.     Nuvem_r = 20 # raio das nuvens (pontos principais)
  117.     Nuvemt_r = 14 # raio dos raios nas nuvens
  118.    
  119.     # distancia entre os raios
  120.     dr = 30
  121.        
  122.     for i in range (len(nuvens)):
  123.         #if i == 0:
  124.             #print ("Nuvem 01 (" + str(nuvens[i].p2x) + ", " + str(nuvens[i].p2y) + ") | (" + str(nuvens[i].x) + ", " + str(nuvens[i].y) + ")")
  125.         # pega nuvem mais proxima com base nas hit boxes das pontas da nuvem e da RD
  126.         front = get_distancia(RD_x1, nuvens[i].p2x, RD_y1, nuvens[i].p2y)
  127.         back  = get_distancia(RD_x3, nuvens[i].p3x, RD_y3, nuvens[i].p3y)  
  128.         #print ("FRONT = %.2f" % front)
  129.         #print ("Nuvem = %d" % i)
  130.         if front <= 200 or back <= 200:
  131.             D01 = get_distancia(RD_x1, nuvens[i].p1x, RD_y1, nuvens[i].p1y)
  132.             D02 = get_distancia(RD_x1, nuvens[i].p2x, RD_y1, nuvens[i].p2y)
  133.             D03 = get_distancia(RD_x1, nuvens[i].p3x, RD_y1, nuvens[i].p3y)
  134.             D04 = get_distancia(RD_x2, nuvens[i].p1x, RD_y2, nuvens[i].p1y)
  135.             D05 = get_distancia(RD_x2, nuvens[i].p2x, RD_y2, nuvens[i].p2y)
  136.             D06 = get_distancia(RD_x2, nuvens[i].p3x, RD_y2, nuvens[i].p3y)
  137.             D07 = get_distancia(RD_x3, nuvens[i].p1x, RD_y3, nuvens[i].p1y)
  138.             D08 = get_distancia(RD_x3, nuvens[i].p2x, RD_y3, nuvens[i].p2y)
  139.             D09 = get_distancia(RD_x3, nuvens[i].p3x, RD_y3, nuvens[i].p3y)
  140.            
  141.             if \
  142.             D01 <= dr or \
  143.             D02 <= dr or \
  144.             D03 <= dr or \
  145.             D04 <= dr or \
  146.             D05 <= dr or \
  147.             D06 <= dr or \
  148.             D07 <= dr or \
  149.             D08 <= dr or \
  150.             D09 <= dr:
  151.                 del nuvens[i]
  152.                 break
  153.  
  154. def get_distancia(x1,x2,y1,y2):
  155.     return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2))
  156.     # formula de distância entre 2 pontos:
  157.     # d = raiz((x2 - x1)² + (y2 - y1)²)  
  158.  
  159. while 1:
  160.     screen.fill(NOITE) # cor de fundo
  161.     for event in pygame.event.get():
  162.         # controles
  163.         keyp = pygame.key.get_pressed()
  164.         if (event.type == KEYDOWN):
  165.             if keyp[K_UP]:
  166.                 dir = "up"
  167.             if keyp[K_DOWN]:
  168.                 dir = "down"
  169.             if keyp[K_RIGHT]:
  170.                 dir = "right"
  171.             if keyp[K_LEFT]:
  172.                 dir = "left"
  173.             if keyp[K_UP] and keyp[K_RIGHT]:
  174.                 dir = "upright"
  175.             if keyp[K_UP] and keyp[K_LEFT]:
  176.                 dir = "upleft"
  177.             if keyp[K_DOWN] and keyp[K_RIGHT]:
  178.                 dir = "downright"
  179.             if keyp[K_DOWN] and keyp[K_LEFT]:
  180.                 dir = "downleft"
  181.         if (event.type == KEYUP):
  182.             if event.key == K_UP:
  183.                 if dir == "upright":
  184.                     dir = "right"
  185.                 if dir == "upleft":
  186.                     dir = "left"
  187.                 if dir == "up":
  188.                     dir = "idle"
  189.             if event.key == K_DOWN:
  190.                 if dir == "downright":
  191.                     dir = "right"
  192.                 if dir == "downleft":
  193.                     dir = "left"
  194.                 if dir == "down":
  195.                     dir = "idle"
  196.             if event.key == K_RIGHT:
  197.                 if dir == "upright":
  198.                     dir = "up"
  199.                 if dir == "downright":
  200.                     dir = "down"
  201.                 if dir == "right":
  202.                     dir = "idle"
  203.             if event.key == K_LEFT:
  204.                 if dir == "upleft":
  205.                     dir = "up"
  206.                 if dir == "downleft":
  207.                     dir = "down"
  208.                 if dir == "left":
  209.                     dir = "idle"
  210.                
  211.         # eventos
  212.         if (event.type == instanciaNuvens):
  213.             instanciaNuvem()
  214.            
  215.         # sair
  216.         if event.type == QUIT:
  217.             pygame.quit()
  218.             sys.exit()
  219.    
  220.     # posição dos pontos de colisão de RD
  221.     RD_x1 = ponyx + 89
  222.     RD_x2 = ponyx + 89
  223.     RD_x3 = ponyx + 41
  224.     RD_y1 = ponyy + 36
  225.     RD_y2 = ponyy + 61
  226.     RD_y3 = ponyy + 63
  227.     # move nuvens
  228.     for n in range(len(nuvens)):
  229.         nuvens[n].x -= movimento
  230.         nuvens[n].img.render(screen,(nuvens[n].x,nuvens[n].y))
  231.         nuvens[n].p1x -= movimento
  232.         nuvens[n].p2x -= movimento
  233.         nuvens[n].p3x -= movimento
  234.         '''
  235.         # ---------------------------------------------------
  236.         # Mostra posição dos pontos de colisão da personagem
  237.         # e dos pontos de colisão das nuvens.
  238.         # ---------------------------------------------------
  239.         pos_nuvens = fonte.render(str(nuvens[n].x) + " , " + str(nuvens[n].y), 1, WHITE)
  240.         screen.blit(pos_nuvens, (nuvens[n].x, nuvens[n].y + 80))
  241.        
  242.         # linhas
  243.         # pontos 1-1
  244.         pygame.draw.line(screen,azul_claro, (nuvens[n].p1x, nuvens[n].p1y), (RD_x1,RD_y1))
  245.         pygame.draw.line(screen,azul_claro, (nuvens[n].p2x, nuvens[n].p2y), (RD_x1,RD_y1))
  246.         pygame.draw.line(screen,azul_claro, (nuvens[n].p3x, nuvens[n].p3y), (RD_x1,RD_y1))
  247.         pygame.draw.line(screen,laranja, (nuvens[n].p1x, nuvens[n].p1y), (RD_x2,RD_y2))
  248.         pygame.draw.line(screen,laranja, (nuvens[n].p2x, nuvens[n].p2y), (RD_x2,RD_y2))
  249.         pygame.draw.line(screen,laranja, (nuvens[n].p3x, nuvens[n].p3y), (RD_x2,RD_y2))
  250.         pygame.draw.line(screen,morango, (nuvens[n].p1x, nuvens[n].p1y), (RD_x3,RD_y3))
  251.         pygame.draw.line(screen,morango, (nuvens[n].p2x, nuvens[n].p2y), (RD_x3,RD_y3))
  252.         pygame.draw.line(screen,morango, (nuvens[n].p3x, nuvens[n].p3y), (RD_x3,RD_y3))
  253.    
  254.     # numeros que mostram posição de Rainbow Dash
  255.     pos_RD = fonte.render(str(ponyx) + ", " + str(ponyy), 1, WHITE)
  256.     screen.blit(pos_RD, (ponyx,ponyy + 80))
  257.     '''
  258.     # checa posição das nuvens
  259.     checa_nuvens()
  260.    
  261.     # testa colisão
  262.     testa_colisao()
  263.    
  264.     # move Rainbow Dash de acordo com o estado (dir) atual
  265.     if dir == "idle":
  266.         pony.render(screen,(ponyx,ponyy))
  267.     if dir == "up":
  268.         ponyy -= movePony
  269.     if dir == "down":
  270.         ponyy += movePony
  271.     if dir == "right":
  272.         ponyx += movePony
  273.     if dir == "left":
  274.         ponyx -= movePony
  275.     if dir == "upright":
  276.         ponyx += movePony
  277.         ponyy -= movePony
  278.     if dir == "upleft":
  279.         ponyx -= movePony
  280.         ponyy -= movePony
  281.     if dir == "downright":
  282.         ponyx += movePony
  283.         ponyy += movePony
  284.     if dir == "downleft":
  285.         ponyx -= movePony
  286.         ponyy += movePony
  287.    
  288.     # limita posição do jogador
  289.     # 96 e 106 são as medidas do gif da personagem usada
  290.     if ponyx <= 0:
  291.         ponyx = 0
  292.     if ponyx >= screen_X - 96:
  293.         ponyx = screen_X - 96
  294.     if ponyy <= 0:
  295.         ponyy = 0
  296.     if ponyy >= screen_Y - 106:
  297.         ponyy = screen_Y - 106
  298.    
  299.     pony.render(screen,(ponyx,ponyy))
  300.     pygame.display.flip()
  301.     fpsClock.tick(30)
Advertisement
Add Comment
Please, Sign In to add comment