Untitled

 avatar
unknown
plain_text
a year ago
2.7 kB
5
Indexable
import pygame
import random

# Constants
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

DIVER_SPEED = 1
FISH_SPEED = 1
TRASH_SPEED = 1

pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
clock = pygame.time.Clock()
background = pygame.image.load("Ocean_Background.jpg")

class Diver (pygame.sprite.Sprite):
  def __init__(self):
    super().__init__()
    self.image = pygame.image.load("Diver.png")
    self.rect = self.image.get_rect() 
    self.rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT)   # set the initial position of the knight
    self.velocity = 0   # used to determine fall speed
  def move(self):
    keys = pygame.key.get_pressed() # Get the list of pressed keys
    if keys[pygame.K_RIGHT] and self.rect.x <= WINDOW_WIDTH: # If the player presses the right key and Alice is not out of bounds
        self.rect.x += DIVER_SPEED
    if keys[pygame.K_LEFT] and self.rect.x >= 0:
        self.rect.x -= DIVER_SPEED
    if keys[pygame.K_UP] and self.rect.y >= 0:
        self.rect.y -= DIVER_SPEED
    if keys[pygame.K_DOWN] and self.rect.y <= WINDOW_HEIGHT:
        self.rect.y += DIVER_SPEED


      
class Trash (pygame.sprite.Sprite):
  def __init__(self):
    super().__init__
    costumes=["bottle.png", "net.png", "bag.png", "can.png"]
    self.image = pygame.image.load(random.choice(costumes))
    self.rect = self.image.get_rect() 
    self.rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT)   # set the initial position of the knight
    self.velocity = 0   # used to determine fall speed

class Fish (pygame.sprite.Sprite):
  def __init__(self):
    super().__init__
    self.image = pygame.image.load("Fish.png")
    self.rect = self.image.get_rect() 
    self.rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT)   # set the initial position of the knight
    self.velocity = 0   # used to determine fall speed
  def update(self):
    if abs(self.rect.centerx - diver.rect.centerx) > 30:
      if self.rect.x < diver.rect.x:
        self.rect.x += FISH_SPEED
      elif self.rect.x > diver.rect.x:
        self.rect.x -= FISH_SPEED


diver=Diver()
fish=Fish()
trash=Trash()


while True:
  window.blit(background, background.get_rect())
  window.blit(diver.image, diver.rect)  # Draw the first sprite 
 # window.blit(fish.image, fish.rect)  # Draw the second sprite
#  window.blit(trash.image, trash.rect)  # Draw the second sprite
  # Display the text

  for event in pygame.event.get():
      if event.type == pygame.QUIT:
          pygame.quit() # Quit Pygame
          sys.exit() # Exit the program

  # Event handler for movement
  diver.move()
 # fish.update()
Editor is loading...
Leave a Comment