Untitled
unknown
plain_text
2 years ago
5.0 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 properties
DIVER_SPEED = 3
# Trash properties
TRASH_SPEED = 1
SPAWN_CHANCE = 0.01
SPAWN_INTERVAL = 1000
# Fish properties
FISH_SPEED = 1
SPAWN_CHANCE = 0.01
SPAWN_INTERVAL = 1000
GRAVITY = 0.25
# Initialize Pygame
pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
clock = pygame.time.Clock()
score = 0 # Initialize the variable for player's score
background = pygame.image.load("Ocean_Background.jpg")
score = 0
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 // 2) # set the initial position of the diver
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and self.rect.x < WINDOW_WIDTH: # check key and ensure not out of bounds
self.rect.x += DIVER_SPEED # move right
if keys[pygame.K_LEFT] and self.rect.x > 0: # check key and ensure not out of bounds
self.rect.x -= DIVER_SPEED # move left
if keys[pygame.K_DOWN] and self.rect.y < WINDOW_HEIGHT: # check key and ensure not out of bounds
self.rect.y += DIVER_SPEED # move down
if keys[pygame.K_UP] and self.rect.y > 0: # check key and ensure not out of bounds
self.rect.y -= DIVER_SPEED # move up
class Trash(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.trash_type = random.randint(0,3)
if self.trash_type == 0:
self.trash_type = "net"
elif self.trash_type == 1:
self.trash_type = "can"
elif self.trash_type == 2:
self.trash_type = "bag"
elif self.trash_type == 3:
self.trash_type = "bottle"
self.image = pygame.image.load(f"{self.trash_type}.png")
pygame.transform.flip(self.image, False, True)
self.rect = self.image.get_rect()
random_x_pos = random.randint(0, WINDOW_WIDTH)
self.rect.center = (random_x_pos, 0)
self.velocity = 0
self.off_screen = False
def update(self):
# Check if trash has fallen out of view
if self.rect.y > 725:
self.off_screen = True
class Fish(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Fish.png")
pygame.transform.flip(self.image, False, True)
self.rect = self.image.get_rect()
random_y_pos = random.randint(0, WINDOW_HEIGHT)
self.rect.center = (0, random_y_pos)
self.velocity = 0
self.off_screen = False
def update(self):
self.rect.x += 1
# Check if fish has swam out of view
if self.rect.x > 725 or self.rect.x < 0:
self.off_screen = True
def detect_collisions():
global score;
if pygame.sprite.spritecollide(diver, trash_group, True):
score += 10
if pygame.sprite.spritecollide(diver, fish_group, True):
score -= 10
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 trash
trash = Trash()
trash_group.add(trash)
all_sprites.add(trash)
def spawn_fish():
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 fish
fish = Fish()
fish_group.add(fish)
all_sprites.add(fish)
# Create groups
all_sprites = pygame.sprite.Group()
trash_group = pygame.sprite.Group()
fish_group = pygame.sprite.Group()
diver_group = pygame.sprite.GroupSingle()
# Create player
diver = Diver()
all_sprites.add(diver)
diver_group.add(diver)
last_spawn_time = pygame.time.get_ticks()
# Main game loop
running = True
while running:
window.blit(background, background.get_rect())
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
score_font = pygame.font.SysFont('times new roman', 20) # Set the font and size for the text
score_surface = score_font.render('Score : ' + str(score), True, BLACK)
window.blit(score_surface, (200, 0)) # Display the text
# Spawn Trash and Fish
spawn_trash()
spawn_fish()
# Update all sprites
all_sprites.update()
detect_collisions()
#check_on_floor()
all_sprites.draw(window)
pygame.display.update()
clock.tick(60)
pygame.quit()Editor is loading...
Leave a Comment