Build Your Own Weather App with Python - Fast and Easy for Beginners

Поділитися
Вставка
  • Опубліковано 2 лис 2024

КОМЕНТАРІ • 2

  • @braydenplayspsvr2786
    @braydenplayspsvr2786 8 місяців тому

    hey I was trying to make my own program based on the pong and it uses some of the same assets and you have a player that can freely move around the screen using wasd and shoot in any direction using arrows and then once the projectile hits the wall it will stop until shot again and I keep getting errors ill drop the code in a comment under this one

    • @braydenplayspsvr2786
      @braydenplayspsvr2786 8 місяців тому

      import pygame
      pygame.init()
      #Constants
      PROJECTILE_SPEED = 5
      PROJECTILE_SIZE = 15
      PLAYER_WIDTH = 50
      PLAYER_HEIGHT = 50
      PLAYER_SPEED = 5
      SCREEN_WIDTH = 600
      SCREEN_HEIGHT = 400
      FPS = 60
      PLAYERX = 300
      PLAYERY = 150
      BLACK = (0, 0, 0)
      WHITE = (255, 255, 255)
      screen_width = SCREEN_WIDTH
      screen_height = SCREEN_HEIGHT
      screen = pygame.display.set_mode((screen_width, screen_height))
      pygame.display.set_caption("Bullets")
      class Player:
      def __init__(self, x, y):
      self.x = x
      self.y = y
      self.width = PLAYER_WIDTH
      self.height = PLAYER_HEIGHT
      self.speed = PLAYER_SPEED
      def move_up(self):
      self.y -= self.speed
      self.y = max(0, self.y)
      PLAYERY = self.y
      def move_down(self):
      self.y += self.speed
      self.y = min(SCREEN_HEIGHT - self.height, self.y)
      PLAYERY = self.y
      def move_left(self):
      self.x -= self.speed
      self.x = max(0, self.x)
      PLAYERX = self.x
      def move_right(self):
      self.x += self.speed
      self.x = min(SCREEN_WIDTH - self.width, self.y)
      PLAYERX = self.x
      def draw(self, screen):
      pygame.draw.rect(screen, WHITE, (self.x, self.y, self.width, self.height))
      class Projectile:
      def __init__(self):
      self.reset()
      def reset(self):
      self.x = PLAYERX
      self.y = PLAYERY
      self.speed_x = self.xspeed()
      self.speed_y = self.yspeed()
      keys = pygame.key.get_pressed()
      def yspeed(self, keys):
      if keys[pygame.K_UP]:
      self.yspeed = 3
      if keys[pygame.K_DOWN]:
      self.yspeed = -3
      def xspeed(self, keys):
      if keys[pygame.K_LEFT]:
      self.xspeed = -3
      if keys[pygame.K_RIGHT]:
      self.xspeed = 3
      def move(self):
      self.x = self.speed_x
      self.y = self.speed_y
      if self.y = SCREEN_HEIGHT or self.x = SCREEN_WIDTH:
      self.speed_x = 0
      self.speed_y = 0
      player = Player(PLAYER_WIDTH, PLAYER_HEIGHT)
      projectile = Projectile()
      running = True
      clock = pygame.time.Clock()
      while running:
      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      running = False
      keys = pygame.key.get_pressed()
      if keys[pygame.K_w]:
      player.move_up()
      if keys[pygame.K_s]:
      player.move_down()
      if keys[pygame.K_a]:
      player.move_left()
      if keys[pygame.K_d]:
      player.move_right()
      projectile.move()
      if keys[pygame.K_UP]:
      projectile.reset()
      if keys[pygame.K_DOWN]:
      projectile.reset()
      if keys[pygame.K_LEFT]:
      projectile.reset()
      if keys[pygame.K_RIGHT]:
      projectile.reset()
      screen.fill(BLACK)
      player.draw(screen)
      pygame.draw.circle(screen, WHITE, (projectile.x, projectile.y), PROJECTILE_SIZE)
      pygame.display.flip()
      clock.tick(FPS)
      pygame.quit
      here's the code