Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.4 kB
0
Indexable
Never
import pygame
import random
import sys
import time

# 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

SPAWN_CHANCE = 0.01
SPAWN_INTERVAL = 2000

pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
clock = pygame.time.Clock()
background = pygame.image.load("Ocean_Background.jpg")
score_font = pygame.font.SysFont('times new roman', 40)  #Set the font and size for the text
trash_score = score_font.render('Trash : 0', True, BLACK)
window.blit(trash_score, (400, 200))  #Redisplay the text

class Diver (pygame.sprite.Sprite):
  def __init__(self, window):
    super().__init__()
    self.window = window
    self.image = pygame.image.load("Diver.png")
    self.rect = self.image.get_rect() 
    self.rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT//2)   # set the initial position of the knight
    self.velocity = 0   # used to determine fall speed
    self.total =0
  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
    if self.rect.y>1280:
      self.rect.y=0
  def death_handler(self):
    self.window.fill(BLACK)
    over_font = pygame.font.SysFont('times new roman', 50)
    game_over_surface = over_font.render('Your Score is : ' + str(self.total), True, RED)
    self.window.blit(game_over_surface, (300, 300))  #Display the text for final score
    pygame.display.update()
    time.sleep(2)  #Wait for 2 seconds
    pygame.quit()  #Quit Pygame
    sys.exit()  #Exit the program
    print("I died")
  def increase_score(self, trash):
    self.total += trash.score
    


      
class Trash (pygame.sprite.Sprite):
  def __init__(self):
    super().__init__()
    costumes=["bottle.png", "net.png", "bag.png", "can.png"]
    self.costume = random.choice(costumes)
    self.score = 0
    
    if self.costume == 'bottle.png':
        self.score = 5
    elif self.costume == 'net.png':
        self.score = 10   
    elif self.costume == 'bag.png':
        self.score = 15   
    elif self.costume == 'can.png':
        self.score = 20    
    self.image = pygame.image.load(self.costume)
    self.rect = self.image.get_rect() 
    self.rect.center = (random.randint(100,1100), 0)   # set the initial position of the knight
    self.velocity = 0   # used to determine fall speed
  def update(self):
    self.rect.y+=1
  


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 = (0, random.randint(100,600))   # set the initial position of the knight
    self.velocity = 0   # used to determine fall speed
  def update(self):
        self.rect.x += FISH_SPEED



def spawn_fish():
  global last_spawn_time
  if random.random() < SPAWN_CHANCE:  # spawn chance is a percent chance to spawn
      current_spawn_time = pygame.time.get_ticks()    # get current time in ms
      if current_spawn_time - last_spawn_time > SPAWN_INTERVAL:   # check if enough time has elapsed
        # create new skeleton
        fish = Fish()
        fish_group.add(fish)
        all_sprites.add(fish)
        last_spawn_time = current_spawn_time


all_sprites = pygame.sprite.Group()
trash_group = pygame.sprite.Group()
fish_group = pygame.sprite.Group()
diver_group = pygame.sprite.GroupSingle()
diver=Diver(window)
diver_group.add(diver)
all_sprites.add(diver_group, trash_group, fish_group)
last_spawn_time = pygame.time.get_ticks()
def spawn_trash():
  if random.random() < SPAWN_CHANCE:  # spawn chance is a percent chance to spawn
      current_spawn_time = pygame.time.get_ticks()    # get current time in ms
      if current_spawn_time - last_spawn_time > SPAWN_INTERVAL:   # check if enough time has elapsed
        # create new skeleton

        trash = Trash()
        trash_group.add(trash)
        all_sprites.add(trash)

def detect_collisions():
    for fish in fish_group.sprites():
        if pygame.sprite.collide_rect(diver, fish):
            diver.death_handler()
    for trash in trash_group.sprites():
        if pygame.sprite.collide_rect(diver,trash):
            trash_group.remove(trash)
            diver.increase_score(trash)
count = 0
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
  all_sprites.update()
  all_sprites.draw(window)



  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()
  spawn_trash()
  spawn_fish()
  
  # detect collision
  detect_collisions()
  
  trash_score = score_font.render(f'Score : {diver.total}', True, RED)
  window.blit(trash_score, (400, 200))  #Redisplay the text



  pygame.display.update()
Leave a Comment