Complete version
26
enemy.py
|
|
@ -1,26 +0,0 @@
|
|||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
|
||||
import random
|
||||
import pygame
|
||||
from pygame.locals import (
|
||||
RLEACCEL,)
|
||||
class Enemy(pygame.sprite.Sprite):
|
||||
def __init__(self):
|
||||
super(Enemy, self).__init__()
|
||||
self.surf= pygame.image.load("sprites/Enemy_Sprite.png").convert()
|
||||
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
|
||||
#self.surf = pygame.Surface((24, 10))
|
||||
#self.surf.fill((255, 0, 0))
|
||||
self.rect = self.surf.get_rect(
|
||||
center=(
|
||||
random.uniform(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
|
||||
random.uniform(0, SCREEN_HEIGHT),
|
||||
)
|
||||
)
|
||||
self.speed = random.uniform(4,10)
|
||||
|
||||
|
||||
|
||||
def update(self):
|
||||
self.rect.move_ip(-self.speed, 0)
|
||||
if self.rect.right < 0:
|
||||
self.kill()
|
||||
338
main.py
|
|
@ -1,178 +1,230 @@
|
|||
import pygame
|
||||
from player import Player
|
||||
from enemy import Enemy
|
||||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
|
||||
import random
|
||||
import math
|
||||
|
||||
from pygame.locals import (
|
||||
RLEACCEL,
|
||||
K_UP,
|
||||
K_DOWN,
|
||||
K_LEFT,
|
||||
K_RIGHT,
|
||||
K_ESCAPE,
|
||||
KEYDOWN,
|
||||
QUIT,
|
||||
K_SPACE,
|
||||
K_f,
|
||||
K_RETURN,
|
||||
)
|
||||
# -----------------------------
|
||||
# Settings
|
||||
# -----------------------------
|
||||
SCREEN_WIDTH = 1280
|
||||
SCREEN_HEIGHT = 720
|
||||
FPS = 80
|
||||
NUM_ENEMIES = 14
|
||||
|
||||
pygame.mixer.init()
|
||||
# -----------------------------
|
||||
# Initialize Pygame
|
||||
# -----------------------------
|
||||
pygame.init()
|
||||
|
||||
pygame.font.init()
|
||||
my_font = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
|
||||
pygame.mixer.music.load("sounds/aura.mp3")
|
||||
|
||||
collision_sound = pygame.mixer.Sound("sounds/dead.ogg")
|
||||
pygame.mixer.init() # Initialize mixer for music
|
||||
|
||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
|
||||
ADDENEMY = pygame.USEREVENT + 1
|
||||
pygame.time.set_timer(ADDENEMY, 1000)
|
||||
|
||||
running = True
|
||||
game_started = False
|
||||
|
||||
# Initialize player, enemy, and sprite groups
|
||||
player = Player()
|
||||
enemy = Enemy()
|
||||
enemies = pygame.sprite.Group()
|
||||
all_sprites = pygame.sprite.Group()
|
||||
|
||||
all_sprites.add(player)
|
||||
all_sprites.add(enemy)
|
||||
enemies.add(enemy)
|
||||
|
||||
bg1 = pygame.transform.scale(pygame.image.load("sprites/Day.city.png").convert(), (SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
|
||||
pygame.display.set_caption("Polarity Field")
|
||||
clock = pygame.time.Clock()
|
||||
font = pygame.font.SysFont(None, 36)
|
||||
|
||||
# Start screen loop
|
||||
while not game_started:
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
if event.key == K_SPACE:
|
||||
game_started = True # Start the game when Space is pressed
|
||||
if event.key == K_f:
|
||||
game_started = True # Start the game when F is pressed (optional)
|
||||
elif event.type == QUIT:
|
||||
running = False
|
||||
game_started = True # Exit the loop if quit event occurs
|
||||
# -----------------------------
|
||||
# Load background music
|
||||
# -----------------------------
|
||||
pygame.mixer.music.load("sprites/music.ogg")
|
||||
pygame.mixer.music.set_volume(0.5)
|
||||
pygame.mixer.music.play(-1) # Loop indefinitely
|
||||
|
||||
screen.fill((0, 0, 0))
|
||||
text = my_font.render("Start", True, (255, 255, 255))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height()) / 2,
|
||||
))
|
||||
text = my_font.render("Press Space to Start", True, (255, 255, 255))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height() - 200)
|
||||
))
|
||||
# -----------------------------
|
||||
# Load sprites AFTER display is ready
|
||||
# -----------------------------
|
||||
player_surf = pygame.image.load("sprites/orb_purple.png").convert_alpha()
|
||||
player_surf = pygame.transform.scale(player_surf, (60, 60))
|
||||
player_rect = player_surf.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
|
||||
player_polarity = 1
|
||||
player_lives = 3
|
||||
|
||||
pygame.display.flip()
|
||||
clock.tick(30) # Limit FPS to 30
|
||||
enemies = []
|
||||
for _ in range(NUM_ENEMIES):
|
||||
polarity = random.choice([1, -1])
|
||||
if polarity == 1:
|
||||
surf = pygame.image.load("sprites/orb_red.png").convert_alpha()
|
||||
else:
|
||||
surf = pygame.image.load("sprites/orb_blue.png").convert_alpha()
|
||||
surf = pygame.transform.scale(surf, (50, 50))
|
||||
|
||||
pygame.mixer.music.play(loops=-1, fade_ms=1000)
|
||||
# Spawn 500 pixels away from player
|
||||
while True:
|
||||
x = random.randint(0, SCREEN_WIDTH)
|
||||
y = random.randint(0, SCREEN_HEIGHT)
|
||||
dx = x - player_rect.centerx
|
||||
dy = y - player_rect.centery
|
||||
dist = math.hypot(dx, dy)
|
||||
if dist >= 500:
|
||||
break
|
||||
|
||||
rect = surf.get_rect(center=(x, y))
|
||||
speed_x = 0 # stationary until game starts
|
||||
speed_y = 0
|
||||
enemies.append({
|
||||
"surf": surf,
|
||||
"rect": rect,
|
||||
"polarity": polarity,
|
||||
"speed_x": speed_x,
|
||||
"speed_y": speed_y
|
||||
})
|
||||
|
||||
# Main game loop
|
||||
# -----------------------------
|
||||
# Score & cooldown
|
||||
# -----------------------------
|
||||
score = 0
|
||||
hit_cooldown = 0
|
||||
game_started = False # Game begins only after first movement
|
||||
|
||||
# -----------------------------
|
||||
# Main loop
|
||||
# -----------------------------
|
||||
running = True
|
||||
while running:
|
||||
screen.fill((0, 0, 0)) # Background color
|
||||
screen.blit(bg1, (0, 0)) # Draw the background image
|
||||
|
||||
clock.tick(FPS)
|
||||
screen.fill((20, 20, 20))
|
||||
|
||||
# -----------------------------
|
||||
# Events
|
||||
# -----------------------------
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
if event.key == K_ESCAPE:
|
||||
running = False # Quit on Escape
|
||||
elif event.key == K_RETURN: # Press Enter to restart
|
||||
# Reset the game state
|
||||
player.kill() # Remove player from all_sprites
|
||||
enemy.kill() # Remove enemy from all_sprites
|
||||
enemies.empty() # Empty enemies group
|
||||
all_sprites.empty() # Empty all_sprites group
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
running = False
|
||||
if event.key == pygame.K_SPACE and game_started:
|
||||
player_polarity *= -1
|
||||
|
||||
# Reinitialize player, enemies, and all sprites
|
||||
player = Player()
|
||||
enemy = Enemy()
|
||||
enemies.add(enemy)
|
||||
all_sprites.add(player)
|
||||
all_sprites.add(enemy)
|
||||
# -----------------------------
|
||||
# Player movement
|
||||
# -----------------------------
|
||||
keys = pygame.key.get_pressed()
|
||||
dx = dy = 0
|
||||
if keys[pygame.K_w] or keys[pygame.K_UP]:
|
||||
dy -= 6
|
||||
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
|
||||
dy += 6
|
||||
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
|
||||
dx -= 6
|
||||
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
|
||||
dx += 6
|
||||
|
||||
# Reset background and re-enter game loop
|
||||
continue # Restart game loop from the beginning
|
||||
# Start game on first movement
|
||||
if not game_started and (dx != 0 or dy != 0):
|
||||
game_started = True
|
||||
# Activate enemy movement now
|
||||
for enemy in enemies:
|
||||
enemy["speed_x"] = random.uniform(-3, 3)
|
||||
enemy["speed_y"] = random.uniform(-3, 3)
|
||||
|
||||
elif event.type == QUIT:
|
||||
running = False # Quit on window close
|
||||
player_rect.move_ip(dx, dy)
|
||||
player_rect.clamp_ip(pygame.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
moved = dx != 0 or dy != 0
|
||||
|
||||
elif event.type == ADDENEMY:
|
||||
# Add a new enemy each second
|
||||
new_enemy = Enemy()
|
||||
enemies.add(new_enemy)
|
||||
all_sprites.add(new_enemy)
|
||||
# -----------------------------
|
||||
# Enemy updates & scoring (only if game started)
|
||||
# -----------------------------
|
||||
if game_started:
|
||||
for enemy in enemies:
|
||||
# Polarity effect
|
||||
if enemy["polarity"] == player_polarity: # repel
|
||||
if enemy["rect"].centerx < player_rect.centerx:
|
||||
enemy["rect"].x -= 4
|
||||
else:
|
||||
enemy["rect"].x += 4
|
||||
if enemy["rect"].centery < player_rect.centery:
|
||||
enemy["rect"].y -= 4
|
||||
else:
|
||||
enemy["rect"].y += 4
|
||||
else: # attract
|
||||
if enemy["rect"].centerx < player_rect.centerx:
|
||||
enemy["rect"].x += 4
|
||||
else:
|
||||
enemy["rect"].x -= 4
|
||||
if enemy["rect"].centery < player_rect.centery:
|
||||
enemy["rect"].y += 4
|
||||
else:
|
||||
enemy["rect"].y -= 4
|
||||
|
||||
pressed_keys = pygame.key.get_pressed()
|
||||
# Random drift
|
||||
enemy["rect"].x += enemy["speed_x"] * 0.3
|
||||
enemy["rect"].y += enemy["speed_y"] * 0.3
|
||||
|
||||
player.update(pressed_keys)
|
||||
enemies.update()
|
||||
# Bounce on screen edges
|
||||
if enemy["rect"].left < 0 or enemy["rect"].right > SCREEN_WIDTH:
|
||||
enemy["speed_x"] *= -1
|
||||
if enemy["rect"].top < 0 or enemy["rect"].bottom > SCREEN_HEIGHT:
|
||||
enemy["speed_y"] *= -1
|
||||
|
||||
# Render all sprites
|
||||
for entity in all_sprites:
|
||||
screen.blit(entity.surf, entity.rect)
|
||||
# Prevent stacking
|
||||
for other in enemies:
|
||||
if other != enemy:
|
||||
dx_sep = enemy["rect"].centerx - other["rect"].centerx
|
||||
dy_sep = enemy["rect"].centery - other["rect"].centery
|
||||
dist_sep = math.hypot(dx_sep, dy_sep)
|
||||
min_dist = 30
|
||||
if dist_sep < min_dist and dist_sep != 0:
|
||||
push_x = (dx_sep / dist_sep) * (min_dist - dist_sep) / 2
|
||||
push_y = (dy_sep / dist_sep) * (min_dist - dist_sep) / 2
|
||||
enemy["rect"].move_ip(push_x, push_y)
|
||||
other["rect"].move_ip(-push_x, -push_y)
|
||||
|
||||
# Check for collisions between player and enemies
|
||||
if pygame.sprite.spritecollideany(player, enemies):
|
||||
player.kill() # Remove player when it collides with an enemy
|
||||
pygame.time.delay(250) # Wait a bit before showing the game over screen
|
||||
# Collision
|
||||
if hit_cooldown == 0 and player_rect.colliderect(enemy["rect"]):
|
||||
player_lives -= 1
|
||||
hit_cooldown = FPS
|
||||
for e in enemies:
|
||||
dx_push = e["rect"].centerx - player_rect.centerx
|
||||
dy_push = e["rect"].centery - player_rect.centery
|
||||
e["rect"].move_ip(dx_push//2, dy_push//2)
|
||||
|
||||
pygame.mixer.stop
|
||||
# Draw enemy
|
||||
screen.blit(enemy["surf"], enemy["rect"])
|
||||
|
||||
collision_sound.play()
|
||||
# Game Over screen after collision
|
||||
screen.fill((0, 0, 0)) # Clear the screen with a black background
|
||||
# Update score
|
||||
if moved:
|
||||
score += 1
|
||||
|
||||
text = my_font.render("Game Over", True, (255, 0, 0))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height()) / 2
|
||||
))
|
||||
# Update cooldown
|
||||
if hit_cooldown > 0:
|
||||
hit_cooldown -= 1
|
||||
|
||||
text = my_font.render("Press Enter to Restart", True, (255, 255, 255))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height()) / 2 + 50,
|
||||
))
|
||||
# -----------------------------
|
||||
# Draw player
|
||||
# -----------------------------
|
||||
screen.blit(player_surf, player_rect)
|
||||
|
||||
text = my_font.render("Press Escape to Quit", True, (255, 255, 255))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height()) / 2 +100,
|
||||
))
|
||||
# -----------------------------
|
||||
# Draw HUD
|
||||
# -----------------------------
|
||||
hud = font.render(f"Lives: {player_lives} Score: {score}", True, (255, 255, 255))
|
||||
screen.blit(hud, (20, 20))
|
||||
|
||||
# -----------------------------
|
||||
# Check for game over
|
||||
# -----------------------------
|
||||
if player_lives <= 0:
|
||||
running = False
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
# Wait for the player to press Enter to restart or Escape to quit
|
||||
waiting_for_input = True
|
||||
while waiting_for_input:
|
||||
# -----------------------------
|
||||
# Game Over screen
|
||||
# -----------------------------
|
||||
screen.fill((20, 20, 20))
|
||||
game_over_text = font.render("GAME OVER", True, (255, 0, 0))
|
||||
score_text = font.render(f"Final Score: {score}", True, (255, 255, 255))
|
||||
|
||||
# Center text
|
||||
screen.blit(game_over_text, (SCREEN_WIDTH//2 - game_over_text.get_width()//2, SCREEN_HEIGHT//2 - 40))
|
||||
screen.blit(score_text, (SCREEN_WIDTH//2 - score_text.get_width()//2, SCREEN_HEIGHT//2 + 10))
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
# Wait until the player closes the window
|
||||
game_over = True
|
||||
while game_over:
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
if event.key == K_RETURN:
|
||||
waiting_for_input = False # Player pressed Enter to restart
|
||||
if event.key == K_ESCAPE:
|
||||
running = False # Player pressed Escape to quit
|
||||
waiting_for_input = False
|
||||
elif event.type == QUIT:
|
||||
waiting_for_input = False
|
||||
running = False
|
||||
if event.type == pygame.QUIT:
|
||||
game_over = False
|
||||
|
||||
pygame.display.flip() # Update the screen
|
||||
|
||||
clock.tick(60) # Limit FPS to 60
|
||||
|
||||
pygame.mixer.music.stop()
|
||||
pygame.mixer.quit()
|
||||
pygame.quit()
|
||||
|
|
|
|||
41
player.py
|
|
@ -1,41 +0,0 @@
|
|||
import pygame
|
||||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
|
||||
from pygame.locals import (
|
||||
RLEACCEL,
|
||||
K_UP,
|
||||
K_DOWN,
|
||||
K_LEFT,
|
||||
K_RIGHT,
|
||||
K_ESCAPE,
|
||||
KEYDOWN,
|
||||
QUIT,
|
||||
)
|
||||
|
||||
|
||||
class Player(pygame.sprite.Sprite):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.surf = pygame.transform.scale(
|
||||
pygame.image.load("sprites/Heart1.png").convert(),
|
||||
(50, 66))
|
||||
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
|
||||
self.rect= self.surf.get_rect()
|
||||
#self.surf = pygame.Surface((40, 40))
|
||||
def update(self, pressed_keys):
|
||||
if pressed_keys[K_UP]:
|
||||
self.rect.move_ip(0, -4)
|
||||
if pressed_keys[K_DOWN]:
|
||||
self.rect.move_ip(0, 4)
|
||||
if pressed_keys[K_LEFT]:
|
||||
self.rect.move_ip(-4, 0)
|
||||
if pressed_keys[K_RIGHT]:
|
||||
self.rect.move_ip(4, 0)
|
||||
|
||||
if self.rect.left < 0:
|
||||
self.rect.left = 0
|
||||
if self.rect.right > SCREEN_WIDTH:
|
||||
self.rect.right = SCREEN_WIDTH
|
||||
if self.rect.top <= 0:
|
||||
self.rect.top = 0
|
||||
if self.rect.bottom >= SCREEN_HEIGHT:
|
||||
self.rect.bottom = SCREEN_HEIGHT
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
SCREEN_WIDTH = 1920
|
||||
SCREEN_HEIGHT = 1080
|
||||
BIN
sounds/aura.mp3
BIN
sounds/dead.ogg
|
Before Width: | Height: | Size: 48 KiB |
|
Before Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 300 KiB |
|
Before Width: | Height: | Size: 427 B |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 391 B |
BIN
sprites/jack.png
|
Before Width: | Height: | Size: 600 B |
|
After Width: | Height: | Size: 569 B |
|
After Width: | Height: | Size: 573 B |
|
After Width: | Height: | Size: 384 B |
|
Before Width: | Height: | Size: 527 B |
|
Before Width: | Height: | Size: 517 B |
|
Before Width: | Height: | Size: 519 B |
|
Before Width: | Height: | Size: 522 B |
|
Before Width: | Height: | Size: 527 B |
|
Before Width: | Height: | Size: 517 B |
|
Before Width: | Height: | Size: 519 B |
|
Before Width: | Height: | Size: 522 B |
|
Before Width: | Height: | Size: 527 B |
|
Before Width: | Height: | Size: 517 B |
|
Before Width: | Height: | Size: 519 B |
|
Before Width: | Height: | Size: 522 B |
|
Before Width: | Height: | Size: 600 B |
|
Before Width: | Height: | Size: 590 B |
|
Before Width: | Height: | Size: 594 B |
|
Before Width: | Height: | Size: 585 B |