Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- import pygame, sys, os
- from math import sqrt
- from pygame.locals import *
- from GIFImage import *
- from random import randint
- # inicia PyGame
- pygame.init()
- fpsClock = pygame.time.Clock()
- # valores para resolução do game / tamanho da janela
- screen_X = 600
- screen_Y = 450
- # define a janela do jogo e o nome dela
- screen = pygame.display.set_mode((screen_X,screen_Y), 0, 32)
- pygame.display.set_caption("Clouddodge")
- # timer para instanciar as nuvens
- instanciaNuvens = 26
- pygame.time.set_timer(instanciaNuvens, 2000) # default = 2000
- # cores para o plano de fundo
- WHITE = (255,255,255)
- NOITE = ( 25, 25,112)
- DIA = (135,206,250)
- # diretório do script e dos arquivos
- current_dir = os.getcwd() + "\\"
- # outras cores
- azul_claro = (51,204,255)
- verde_claro = (102,255,51)
- laranja = (255,204,51)
- morango = (255,51,102)
- marrom = (184,138,0)
- verde_agua = (51,255,204)
- roxo = (204,51,255)
- # posição de Rainbow Dash no começo do jogo
- ponyx = 10
- ponyy = 150
- dir = "idle" # por padrão, até que o jogador altere ao movê-la
- # define o canto direito da tela para instanciar novas nuvens
- canto = screen_Y + 200
- # lista para armazenar os objetos nuvem
- nuvens = []
- # imagens
- pony = GIFImage(current_dir + "fly_rainbow_right.gif")
- thunder_cloud = GIFImage(current_dir + "thunder_cloud.gif")
- normal_cloud = GIFImage(current_dir + "normal_cloud.gif")
- # variáveis que deslocam nuvens e Rainbow Dash
- movimento = 4 # nuvens
- movePony = movimento * 2 # Rainbow Dash
- # texto para testes
- fonte = pygame.font.SysFont("Arial", 15)
- class Nuvem:
- def __init__(self, x, y):
- # posição da imagem
- self.x = x
- self.y = y
- # topo
- self.p1x = self.x + 53
- self.p1y = self.y + 17
- # esquerda
- self.p2x = self.x + 17
- self.p2y = self.y + 51
- # direita
- self.p3x = self.x + 84
- self.p3y = self.y + 51
- # sorteia aleatoriamente o tipo de nuvem (normal ou com raios
- self.tipo = randint(0,1)
- if (self.tipo == 0):
- self.img = thunder_cloud
- # trovoes
- # esquerda
- self.trovao1x = self.x + 17
- self.trovao1y = self.y + 82
- # direita
- self.trovao2x = self.x + 66
- self.trovao2y = self.y + 80
- else:
- self.img = normal_cloud
- def instanciaNuvem():
- x = canto
- y = randint(0,230)
- nuvem = Nuvem(x,y)
- nuvens.append(nuvem)
- def checa_nuvens():
- for i in range(len(nuvens)):
- if nuvens[i].x <= -110:
- del nuvens[i]
- break
- # # hitbox Rainbow Dash
- # 100, 25
- # 104, 47
- # 102, 66
- def testa_colisao():
- RD_r = 18 # raio das 'HitCircles' da Rainbow Dash
- Nuvem_r = 20 # raio das nuvens (pontos principais)
- Nuvemt_r = 14 # raio dos raios nas nuvens
- # distancia entre os raios
- dr = 30
- for i in range (len(nuvens)):
- #if i == 0:
- #print ("Nuvem 01 (" + str(nuvens[i].p2x) + ", " + str(nuvens[i].p2y) + ") | (" + str(nuvens[i].x) + ", " + str(nuvens[i].y) + ")")
- # pega nuvem mais proxima com base nas hit boxes das pontas da nuvem e da RD
- front = get_distancia(RD_x1, nuvens[i].p2x, RD_y1, nuvens[i].p2y)
- back = get_distancia(RD_x3, nuvens[i].p3x, RD_y3, nuvens[i].p3y)
- #print ("FRONT = %.2f" % front)
- #print ("Nuvem = %d" % i)
- if front <= 200 or back <= 200:
- D01 = get_distancia(RD_x1, nuvens[i].p1x, RD_y1, nuvens[i].p1y)
- D02 = get_distancia(RD_x1, nuvens[i].p2x, RD_y1, nuvens[i].p2y)
- D03 = get_distancia(RD_x1, nuvens[i].p3x, RD_y1, nuvens[i].p3y)
- D04 = get_distancia(RD_x2, nuvens[i].p1x, RD_y2, nuvens[i].p1y)
- D05 = get_distancia(RD_x2, nuvens[i].p2x, RD_y2, nuvens[i].p2y)
- D06 = get_distancia(RD_x2, nuvens[i].p3x, RD_y2, nuvens[i].p3y)
- D07 = get_distancia(RD_x3, nuvens[i].p1x, RD_y3, nuvens[i].p1y)
- D08 = get_distancia(RD_x3, nuvens[i].p2x, RD_y3, nuvens[i].p2y)
- D09 = get_distancia(RD_x3, nuvens[i].p3x, RD_y3, nuvens[i].p3y)
- if \
- D01 <= dr or \
- D02 <= dr or \
- D03 <= dr or \
- D04 <= dr or \
- D05 <= dr or \
- D06 <= dr or \
- D07 <= dr or \
- D08 <= dr or \
- D09 <= dr:
- del nuvens[i]
- break
- def get_distancia(x1,x2,y1,y2):
- return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2))
- # formula de distância entre 2 pontos:
- # d = raiz((x2 - x1)² + (y2 - y1)²)
- while 1:
- screen.fill(NOITE) # cor de fundo
- for event in pygame.event.get():
- # controles
- keyp = pygame.key.get_pressed()
- if (event.type == KEYDOWN):
- if keyp[K_UP]:
- dir = "up"
- if keyp[K_DOWN]:
- dir = "down"
- if keyp[K_RIGHT]:
- dir = "right"
- if keyp[K_LEFT]:
- dir = "left"
- if keyp[K_UP] and keyp[K_RIGHT]:
- dir = "upright"
- if keyp[K_UP] and keyp[K_LEFT]:
- dir = "upleft"
- if keyp[K_DOWN] and keyp[K_RIGHT]:
- dir = "downright"
- if keyp[K_DOWN] and keyp[K_LEFT]:
- dir = "downleft"
- if (event.type == KEYUP):
- if event.key == K_UP:
- if dir == "upright":
- dir = "right"
- if dir == "upleft":
- dir = "left"
- if dir == "up":
- dir = "idle"
- if event.key == K_DOWN:
- if dir == "downright":
- dir = "right"
- if dir == "downleft":
- dir = "left"
- if dir == "down":
- dir = "idle"
- if event.key == K_RIGHT:
- if dir == "upright":
- dir = "up"
- if dir == "downright":
- dir = "down"
- if dir == "right":
- dir = "idle"
- if event.key == K_LEFT:
- if dir == "upleft":
- dir = "up"
- if dir == "downleft":
- dir = "down"
- if dir == "left":
- dir = "idle"
- # eventos
- if (event.type == instanciaNuvens):
- instanciaNuvem()
- # sair
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- # posição dos pontos de colisão de RD
- RD_x1 = ponyx + 89
- RD_x2 = ponyx + 89
- RD_x3 = ponyx + 41
- RD_y1 = ponyy + 36
- RD_y2 = ponyy + 61
- RD_y3 = ponyy + 63
- # move nuvens
- for n in range(len(nuvens)):
- nuvens[n].x -= movimento
- nuvens[n].img.render(screen,(nuvens[n].x,nuvens[n].y))
- nuvens[n].p1x -= movimento
- nuvens[n].p2x -= movimento
- nuvens[n].p3x -= movimento
- '''
- # ---------------------------------------------------
- # Mostra posição dos pontos de colisão da personagem
- # e dos pontos de colisão das nuvens.
- # ---------------------------------------------------
- pos_nuvens = fonte.render(str(nuvens[n].x) + " , " + str(nuvens[n].y), 1, WHITE)
- screen.blit(pos_nuvens, (nuvens[n].x, nuvens[n].y + 80))
- # linhas
- # pontos 1-1
- pygame.draw.line(screen,azul_claro, (nuvens[n].p1x, nuvens[n].p1y), (RD_x1,RD_y1))
- pygame.draw.line(screen,azul_claro, (nuvens[n].p2x, nuvens[n].p2y), (RD_x1,RD_y1))
- pygame.draw.line(screen,azul_claro, (nuvens[n].p3x, nuvens[n].p3y), (RD_x1,RD_y1))
- pygame.draw.line(screen,laranja, (nuvens[n].p1x, nuvens[n].p1y), (RD_x2,RD_y2))
- pygame.draw.line(screen,laranja, (nuvens[n].p2x, nuvens[n].p2y), (RD_x2,RD_y2))
- pygame.draw.line(screen,laranja, (nuvens[n].p3x, nuvens[n].p3y), (RD_x2,RD_y2))
- pygame.draw.line(screen,morango, (nuvens[n].p1x, nuvens[n].p1y), (RD_x3,RD_y3))
- pygame.draw.line(screen,morango, (nuvens[n].p2x, nuvens[n].p2y), (RD_x3,RD_y3))
- pygame.draw.line(screen,morango, (nuvens[n].p3x, nuvens[n].p3y), (RD_x3,RD_y3))
- # numeros que mostram posição de Rainbow Dash
- pos_RD = fonte.render(str(ponyx) + ", " + str(ponyy), 1, WHITE)
- screen.blit(pos_RD, (ponyx,ponyy + 80))
- '''
- # checa posição das nuvens
- checa_nuvens()
- # testa colisão
- testa_colisao()
- # move Rainbow Dash de acordo com o estado (dir) atual
- if dir == "idle":
- pony.render(screen,(ponyx,ponyy))
- if dir == "up":
- ponyy -= movePony
- if dir == "down":
- ponyy += movePony
- if dir == "right":
- ponyx += movePony
- if dir == "left":
- ponyx -= movePony
- if dir == "upright":
- ponyx += movePony
- ponyy -= movePony
- if dir == "upleft":
- ponyx -= movePony
- ponyy -= movePony
- if dir == "downright":
- ponyx += movePony
- ponyy += movePony
- if dir == "downleft":
- ponyx -= movePony
- ponyy += movePony
- # limita posição do jogador
- # 96 e 106 são as medidas do gif da personagem usada
- if ponyx <= 0:
- ponyx = 0
- if ponyx >= screen_X - 96:
- ponyx = screen_X - 96
- if ponyy <= 0:
- ponyy = 0
- if ponyy >= screen_Y - 106:
- ponyy = screen_Y - 106
- pony.render(screen,(ponyx,ponyy))
- pygame.display.flip()
- fpsClock.tick(30)
Advertisement
Add Comment
Please, Sign In to add comment