Untitled
unknown
plain_text
2 years ago
3.8 kB
7
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
SPAWN_CHANCE = 0.01
SPAWN_INTERVAL = 1000
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//2) # 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
if self.rect.y>1280:
self.rect.y=0
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 = (random.randint(100,600), 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():
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)
all_sprites = pygame.sprite.Group()
trash_group = pygame.sprite.Group()
fish_group = pygame.sprite.Group()
diver_group = pygame.sprite.GroupSingle()
diver=Diver()
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)
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()
pygame.display.update()
Editor is loading...
Leave a Comment