Ir para conteúdo
Fórum Script Brasil

O Berna

Membros
  • Total de itens

    1
  • Registro em

  • Última visita

Tudo que O Berna postou

  1. Olá amigos, estou estudando python, e tenho o código abaixo, com o jogo a funcionar perfeitamente. Pois bem, eu gostaria de adicionar uma função após a checagem de vitoria/derrota, que permita o REINÍCIO da partida, sem eu precisar ter de sair da simulação no x e simular novamente pela idle. O código segue abaixo: # Importação das Bibliotecas import pygame from pygame.locals import * import math import random # Iniciando o Jogo pygame.init() width, height = 1000, 480 screen = pygame.display.set_mode((width, height)) comando = [False, False, False, False] heropos = [100,100] precisao = [0,0] projeteis = [] badtimer=100 badtimer1=0 badguys=[[640,100]] healthvalue=194 # Carregando Imagens hero = pygame.image.load("data/imgs/hero.png") piso = pygame.image.load("data/imgs/piso.png") prato = pygame.image.load("data/imgs/prato.png") projetil = pygame.image.load("data/imgs/projetil.png") badguyimg1 = pygame.image.load("data/imgs/zumbi.png") badguyimg=badguyimg1 healthbar = pygame.image.load("data/imgs/healthbar.png") health = pygame.image.load("data/imgs/health.png") gameover = pygame.image.load("data/imgs/gameover.png") youwin = pygame.image.load("data/imgs/youwin.png") # 3.1 - Load audio hit = pygame.mixer.Sound("data/audio/colisaoMesa.wav") zumbi = pygame.mixer.Sound("data/audio/inimigo.wav") shoot = pygame.mixer.Sound("data/audio/disparo.wav") hitHero = pygame.mixer.Sound("data/audio/colisaoHeroi.wav") hit.set_volume(1) hitHero.set_volume(0.5) zumbi.set_volume(0.50) shoot.set_volume(0.25) pygame.mixer.music.load('data/audio/trilha.wav') pygame.mixer.music.play(-1, 0.0) pygame.mixer.music.set_volume(0.25) # LoopingGame running = 1 exitcode = 0 while running: badtimer-=1 # Limpar Ecrã screen.fill(0) # Tela de Fundo for x in range (int(width/piso.get_width()+1)): for y in range (int(height/piso.get_height()+1)): screen.blit(piso,(x*100,y*100)) # Desenhar Mesas de Pratos # screen.blit(prato,(0,10)) screen.blit(prato,(0,125)) screen.blit(prato,(0,240)) screen.blit(prato,(0,355 )) # Posição e Rotação do Herói posicao = pygame.mouse.get_pos() angulo = math.atan2(posicao[1]-(heropos[1]+0),posicao[0]-(heropos[0]+0)) herorot = pygame.transform.rotate(hero, 360-angulo*57.29) heropos1 = hero.get_rect() heropos1.move_ip(heropos[0]-herorot.get_rect().width/2, heropos[1]-herorot.get_rect().height/2) screen.blit(herorot, heropos1) # Desenhar Projéteis for bala in projeteis: index=0 velx=math.cos(bala[0])*10 vely=math.sin(bala[0])*10 bala[1]+=velx bala[2]+=vely if bala[1]<-64 or bala[1]>1000 or bala[2]<-64 or bala[2]>480: projeteis.pop(index) index+=1 for disparo in projeteis: projetilUm = pygame.transform.rotate(projetil, 360-disparo[0]*57.29) screen.blit(projetilUm, (disparo[1], disparo[2])) # 6.3 - Draw badgers if badtimer==0: badguys.append([1000, random.randint(50,430)]) badtimer=100-(badtimer1*2) if badtimer1>=35: badtimer1=35 else: badtimer1+=5 index=0 for badguy in badguys: if badguy[0]<-64: badguys.pop(index) badguy[0]-=7 # 6.3.1 - Attack castle badrect=pygame.Rect(badguyimg.get_rect()) badrect.top=badguy[1] badrect.left=badguy[0] if badrect.left<64: hit.play() healthvalue -= random.randint(5,20) badguys.pop(index) #6.3.2 - Check for collisions index1=0 if badrect.colliderect(heropos1): hit.play() hitHero.play() healthvalue -= random.randint(5,20) badguys.pop(index) for bullet in projeteis: bullrect=pygame.Rect(projetil.get_rect()) bullrect.left=bullet[1] bullrect.top=bullet[2] if badrect.colliderect(bullrect): zumbi.play() precisao[0]+=1 badguys.pop(index) projeteis.pop(index1) index1+=1 # 6.3.3 - Next bad guy index+=1 for badguy in badguys: screen.blit(badguyimg, badguy) # 6.4 - Draw clock font = pygame.font.Font(None, 50) survivedtext = font.render(str(int((15000-pygame.time.get_ticks())/60000))+":"+str(int((15000-pygame.time.get_ticks())/1000%60)).zfill(2), True, (255,255,255)) textRect = survivedtext.get_rect() textRect.topright=[950,5] screen.blit(survivedtext, textRect) # 6.5 - Draw health bar screen.blit(healthbar, (125,2)) for health1 in range(healthvalue): screen.blit(health, (health1+128,5)) # Atualizar Ecrã pygame.display.flip() # Disparar Eventos for event in pygame.event.get(): # Checar Evento (Botão X de Sair) if event.type==pygame.QUIT: # Sair do Jogo pygame.quit() exit(0) #Controles da Matriz de Movimento if event.type == pygame.KEYDOWN: if event.key==K_w: comando[0]=True elif event.key==K_a: comando[1]=True elif event.key==K_s: comando[2]=True elif event.key==K_d: comando[3]=True if event.type == pygame.KEYUP: if event.key==pygame.K_w: comando[0]=False elif event.key==pygame.K_a: comando[1]=False elif event.key==pygame.K_s: comando[2]=False elif event.key==pygame.K_d: comando[3]=False #Controles da Matriz de Disparo do Projetil if event.type==pygame.MOUSEBUTTONDOWN: shoot.play() posicao=pygame.mouse.get_pos() precisao[1]+=1 projeteis.append([math.atan2(posicao[1]-(heropos1[1]+32),posicao[0]-(heropos1[0]+26)),heropos1[0]+32,heropos1[1]+32]) # Movimentação do Herói if comando[0]: heropos[1]-=5 elif comando[2]: heropos[1]+=5 if comando[1]: heropos[0]-=5 elif comando[3]: heropos[0]+=5 #10 - Win/Lose check if pygame.time.get_ticks()>=15000: running=0 exitcode=1 if healthvalue<=0: running=0 exitcode=0 if precisao[1]!=0: accuracy=precisao[0]*1.0/precisao[1]*100 else: accuracy=0 # 11 - Win/lose display if exitcode==0: pygame.font.init() font = pygame.font.Font(None, 24) text = font.render("Precisão de Acerto: "+str(float(accuracy))+"%", True, (255,0,0)) textRect = text.get_rect() textRect.centerx = screen.get_rect().centerx textRect.centery = screen.get_rect().centery+24 screen.blit(gameover, (0,0)) screen.blit(text, textRect) else: pygame.font.init() font = pygame.font.Font(None, 24) text = font.render("Accuracy: "+str(float(accuracy))+"%", True, (0,255,0)) textRect = text.get_rect() textRect.centerx = screen.get_rect().centerx textRect.centery = screen.get_rect().centery+24 screen.blit(youwin, (0,0)) screen.blit(text, textRect) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) pygame.display.flip()
×
×
  • Criar Novo...