Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.4 kB
0
Indexable
Never
import pygame
import random

WHITE = (255, 255, 255)
BLUE = (0, 0, 255)

WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
ANIMATION_INTERVAL = 200
DIVER_SPEED = 4
TRASH_SPEED = 1
FISH_SPEED = 3
SPAWN_INTERVAL = 600
SPAWN_CHANCE = 0.03

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

class Diver(pygame.sprite.Sprite):
  def __init__(self):
    super().__init__()
    self.frame_list = init_animation_frames('Diver_Swim.png', 6)
    self.image = self.frame_list[0]
    self.rect = self.image.get_rect()
    self.rect.center = (WINDOW_WIDTH // 2 , WINDOW_HEIGHT // 2)
    self.current_frame_index = 0
    self.last_time_frame_updated = 0
    self.mask = pygame.mask.from_surface(self.image)
    self.facing_right = True
    
  def update(self):
    keys = pygame.key.get_pressed()
    if keys[pygame.K_right] and self.rect.x <= WINDOW_WIDTH:
      self.rect.x += DIVER_SPEED
      if not self.facing_right:
        flip_sprite(self)
        self.facing_right = True
    if keys[pygame.K_left] and self.rect.x >= 0:
      self.rect.x -= DIVER_SPEED
      if self.facing_right:
        flip_sprite(self)
        self.facing_right = False
        
    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__()
    if trash_type == 0:
      self.image = pygame.image.load('can.png')
      self.value = 1
    elif trash_type == 1:
      self.image = pygame.image.load('bottle.png')
        self.value = 5
    elif trash_type == 2:
      self.image = pygame.image.load('bag.png')
        self.value = 10
    elif trash_type == 3:
      self.image = pygame.image.load('net.png')
        self.value = 15
        
    self.rect = self.image.get_rect()
    self.rect.center = (random.randint(0, WINDOW_WIDTH), 0)
    self.mask = pygame.mask.from_surface(self.image)
    
  def update(self):
    self.rect.y+= TRASH_SPEED
    if self.rect.y >= WINDOW_HEIGHT:
      self.kill()
      

class Fish(pygame.sprite.Sprite):
  def __init__(self):
    super().__init__()
    self.frame_list = init_animation_frames('Fish_Swim.png', 6)
    self.image = self.frame_list[0]
    self.rect = self.image.get_rect()
    self.rect.center = (0, random.randint(0, WINDOW_HEIGHT))
    self.current_frame_index = 0
    self.last_time_frame_updated = 0
    self.mask = pygame.mask.from_surface(self.image)
  def update(self):
    self.rect.x+= FISH_SPEED
    if self.rect.x >= WINDOW_WIDTH:
      self.kill()       
      
def flip_sprite(obj):
  for i, frame in enumerate(obj.fram_list):
    obj.frame_list[i] = pygame.transform.flip(frame, True, False)
    obj.frame_list[i].set_colorkey(BLUE)
      
  obj.image= obj.fram_list[obj.current_frame_index]
  obj.mask = pygame.mask.from_surface(obj.image)
    
def detect_collisions():
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
      
Leave a Comment