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()
|
|
||||||
342
main.py
|
|
@ -1,178 +1,230 @@
|
||||||
import pygame
|
import pygame
|
||||||
from player import Player
|
import random
|
||||||
from enemy import Enemy
|
import math
|
||||||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
|
|
||||||
|
|
||||||
from pygame.locals import (
|
# -----------------------------
|
||||||
RLEACCEL,
|
# Settings
|
||||||
K_UP,
|
# -----------------------------
|
||||||
K_DOWN,
|
SCREEN_WIDTH = 1280
|
||||||
K_LEFT,
|
SCREEN_HEIGHT = 720
|
||||||
K_RIGHT,
|
FPS = 80
|
||||||
K_ESCAPE,
|
NUM_ENEMIES = 14
|
||||||
KEYDOWN,
|
|
||||||
QUIT,
|
|
||||||
K_SPACE,
|
|
||||||
K_f,
|
|
||||||
K_RETURN,
|
|
||||||
)
|
|
||||||
|
|
||||||
pygame.mixer.init()
|
# -----------------------------
|
||||||
|
# Initialize Pygame
|
||||||
|
# -----------------------------
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
pygame.mixer.init() # Initialize mixer for music
|
||||||
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")
|
|
||||||
|
|
||||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||||
|
pygame.display.set_caption("Polarity Field")
|
||||||
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))
|
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
font = pygame.font.SysFont(None, 36)
|
||||||
|
|
||||||
# Start screen loop
|
# -----------------------------
|
||||||
while not game_started:
|
# Load background music
|
||||||
for event in pygame.event.get():
|
# -----------------------------
|
||||||
if event.type == KEYDOWN:
|
pygame.mixer.music.load("sprites/music.ogg")
|
||||||
if event.key == K_SPACE:
|
pygame.mixer.music.set_volume(0.5)
|
||||||
game_started = True # Start the game when Space is pressed
|
pygame.mixer.music.play(-1) # Loop indefinitely
|
||||||
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
|
|
||||||
|
|
||||||
screen.fill((0, 0, 0))
|
# -----------------------------
|
||||||
text = my_font.render("Start", True, (255, 255, 255))
|
# Load sprites AFTER display is ready
|
||||||
screen.blit(text, (
|
# -----------------------------
|
||||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
player_surf = pygame.image.load("sprites/orb_purple.png").convert_alpha()
|
||||||
(SCREEN_HEIGHT - text.get_height()) / 2,
|
player_surf = pygame.transform.scale(player_surf, (60, 60))
|
||||||
))
|
player_rect = player_surf.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
|
||||||
text = my_font.render("Press Space to Start", True, (255, 255, 255))
|
player_polarity = 1
|
||||||
screen.blit(text, (
|
player_lives = 3
|
||||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
|
||||||
(SCREEN_HEIGHT - text.get_height() - 200)
|
|
||||||
))
|
|
||||||
|
|
||||||
pygame.display.flip()
|
|
||||||
clock.tick(30) # Limit FPS to 30
|
|
||||||
|
|
||||||
pygame.mixer.music.play(loops=-1, fade_ms=1000)
|
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))
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
# Main game loop
|
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
|
||||||
|
})
|
||||||
|
|
||||||
|
# -----------------------------
|
||||||
|
# Score & cooldown
|
||||||
|
# -----------------------------
|
||||||
|
score = 0
|
||||||
|
hit_cooldown = 0
|
||||||
|
game_started = False # Game begins only after first movement
|
||||||
|
|
||||||
|
# -----------------------------
|
||||||
|
# Main loop
|
||||||
|
# -----------------------------
|
||||||
|
running = True
|
||||||
while running:
|
while running:
|
||||||
screen.fill((0, 0, 0)) # Background color
|
clock.tick(FPS)
|
||||||
screen.blit(bg1, (0, 0)) # Draw the background image
|
screen.fill((20, 20, 20))
|
||||||
|
|
||||||
|
|
||||||
|
# -----------------------------
|
||||||
|
# Events
|
||||||
|
# -----------------------------
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == KEYDOWN:
|
if event.type == pygame.QUIT:
|
||||||
if event.key == K_ESCAPE:
|
running = False
|
||||||
running = False # Quit on Escape
|
if event.type == pygame.KEYDOWN:
|
||||||
elif event.key == K_RETURN: # Press Enter to restart
|
if event.key == pygame.K_ESCAPE:
|
||||||
# Reset the game state
|
running = False
|
||||||
player.kill() # Remove player from all_sprites
|
if event.key == pygame.K_SPACE and game_started:
|
||||||
enemy.kill() # Remove enemy from all_sprites
|
player_polarity *= -1
|
||||||
enemies.empty() # Empty enemies group
|
|
||||||
all_sprites.empty() # Empty all_sprites group
|
|
||||||
|
|
||||||
# Reinitialize player, enemies, and all sprites
|
# -----------------------------
|
||||||
player = Player()
|
# Player movement
|
||||||
enemy = Enemy()
|
# -----------------------------
|
||||||
enemies.add(enemy)
|
keys = pygame.key.get_pressed()
|
||||||
all_sprites.add(player)
|
dx = dy = 0
|
||||||
all_sprites.add(enemy)
|
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
|
# Start game on first movement
|
||||||
continue # Restart game loop from the beginning
|
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:
|
player_rect.move_ip(dx, dy)
|
||||||
running = False # Quit on window close
|
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
|
# Enemy updates & scoring (only if game started)
|
||||||
new_enemy = Enemy()
|
# -----------------------------
|
||||||
enemies.add(new_enemy)
|
if game_started:
|
||||||
all_sprites.add(new_enemy)
|
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)
|
# Bounce on screen edges
|
||||||
enemies.update()
|
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
|
# Prevent stacking
|
||||||
for entity in all_sprites:
|
for other in enemies:
|
||||||
screen.blit(entity.surf, entity.rect)
|
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
|
# Collision
|
||||||
if pygame.sprite.spritecollideany(player, enemies):
|
if hit_cooldown == 0 and player_rect.colliderect(enemy["rect"]):
|
||||||
player.kill() # Remove player when it collides with an enemy
|
player_lives -= 1
|
||||||
pygame.time.delay(250) # Wait a bit before showing the game over screen
|
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()
|
# Update score
|
||||||
# Game Over screen after collision
|
if moved:
|
||||||
screen.fill((0, 0, 0)) # Clear the screen with a black background
|
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
|
|
||||||
))
|
|
||||||
|
|
||||||
text = my_font.render("Press Enter to Restart", True, (255, 255, 255))
|
# Update cooldown
|
||||||
screen.blit(text, (
|
if hit_cooldown > 0:
|
||||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
hit_cooldown -= 1
|
||||||
(SCREEN_HEIGHT - text.get_height()) / 2 + 50,
|
|
||||||
))
|
|
||||||
|
|
||||||
text = my_font.render("Press Escape to Quit", True, (255, 255, 255))
|
# -----------------------------
|
||||||
screen.blit(text, (
|
# Draw player
|
||||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
# -----------------------------
|
||||||
(SCREEN_HEIGHT - text.get_height()) / 2 +100,
|
screen.blit(player_surf, player_rect)
|
||||||
))
|
|
||||||
|
|
||||||
pygame.display.flip()
|
# -----------------------------
|
||||||
|
# Draw HUD
|
||||||
|
# -----------------------------
|
||||||
|
hud = font.render(f"Lives: {player_lives} Score: {score}", True, (255, 255, 255))
|
||||||
|
screen.blit(hud, (20, 20))
|
||||||
|
|
||||||
# Wait for the player to press Enter to restart or Escape to quit
|
# -----------------------------
|
||||||
waiting_for_input = True
|
# Check for game over
|
||||||
while waiting_for_input:
|
# -----------------------------
|
||||||
for event in pygame.event.get():
|
if player_lives <= 0:
|
||||||
if event.type == KEYDOWN:
|
running = False
|
||||||
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
|
|
||||||
|
|
||||||
pygame.display.flip() # Update the screen
|
pygame.display.flip()
|
||||||
|
|
||||||
clock.tick(60) # Limit FPS to 60
|
# -----------------------------
|
||||||
|
# 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 == pygame.QUIT:
|
||||||
|
game_over = False
|
||||||
|
|
||||||
pygame.mixer.music.stop()
|
|
||||||
pygame.mixer.quit()
|
|
||||||
pygame.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 |