Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.5 kB
2
Indexable
Never
import pygame, time
from utils import *
from os.path import join
from pygame.sprite import Group, Sprite

class Game:
  LARGURA_TELA = 1280
  ALTURA_TELA = 720
  
  def __init__(self) -> None:
    pygame.init()
    
    
    self.display = pygame.display.set_mode((self.LARGURA_TELA, self.ALTURA_TELA), pygame.SCALED | pygame.RESIZABLE)
    self.fundo = surface_load(join("images", "universo.png"), size= self.display.get_size())
    
    # self.player_surf = surface_load(join("images", "nave.png"), True, (80, 80))
    # self.player = self.player_surf.get_rect(center = (self.LARGURA_TELA / 2, self.ALTURA_TELA * 0.80))
    
    pygame.display.set_caption("studying classes")
    pygame.display.set_icon(pygame.image.load(join("images", "logo.png")))
    
    pygame.mouse.set_visible(True)

    self.clock = pygame.time.Clock()
        
    #self.direcao = pygame.math.Vector2()
    #self.speed = 0.5
    self.running = True
    self.tempo_ultimo_tiro = 0
    self.all_sprites = Group()
    self.player = Player(self.all_sprites)

  # def atirar_player(self):
  #   current_time = time.time()
  #   if self.keys[pygame.K_SPACE] and current_time - self.tempo_ultimo_tiro >= 1:
  #     print("fire laser")
  #     self.tempo_ultimo_tiro = current_time

  def desenhar_fundo(self):
    self.display.fill("#1E1647")
    self.display.blit(self.fundo, (0,0))

  def run(self):

    while self.running:
      self.dt = self.clock.tick()

      for event in pygame.event.get():
        if event.type == pygame.QUIT:
          self.running = False

      self.desenhar_fundo()
      self.player.movimento_player()
      self.all_sprites.update()
      self.all_sprites.draw(self.display)
      pygame.display.update()
    pygame.quit()

class Player(Sprite):
  def __init__(self, *groups: Group) -> None:
    super().__init__(*groups)
    self.image = surface_load(join("images", "nave.png"), True, (80,80))
    self.rect = self.image.get_rect(center= (Game))
    self.speed = 0.5

  def movimento_player(self):
    self.direcao = pygame.math.Vector2()
    self.keys = pygame.key.get_pressed()
    
    self.direcao.x = int(self.keys[pygame.K_d]) - int(self.keys[pygame.K_a])
    self.direcao.y = int(self.keys[pygame.K_s]) - int(self.keys[pygame.K_w])

    self.direcao = self.direcao.normalize() if self.direcao else self.direcao
  
    self.rect.center += self.direcao * self.speed * self.dt
    

if __name__ == "__main__":
  jogo = Game()
  jogo.run()
  
Leave a Comment