Skip to content

Commit

Permalink
[CLEANUP]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Apr 14, 2024
1 parent eeb7f64 commit 619ab4b
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
4 changes: 1 addition & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
parser.add_argument(
"--team_task", type=str, help="The team task for the swarm"
)
parser.add_argument(
"--task", type=str, help="The task for the swarm"
)
parser.add_argument("--task", type=str, help="The task for the swarm")

# Parse the command line arguments
args = parser.parse_args()
Expand Down
119 changes: 119 additions & 0 deletions neo_sapiens/swarm_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import pygame
import random

# Initialize Pygame
pygame.init()

# Constants
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
PLAYER_SPEED = 5
BULLET_SPEED = 7
ENEMY_SPEED = 2
ENEMY_SPAWN_RATE = 25 # Lower is faster

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# Setup the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Robot Swarm Attack")

# Player
player_img = pygame.Surface((50, 30))
player_img.fill(WHITE)
player_x = SCREEN_WIDTH // 2
player_y = SCREEN_HEIGHT - 60

# Bullets
bullets = []

# Enemies
enemies = []

# Clock
clock = pygame.time.Clock()


def move_player(dx):
global player_x
player_x += dx * PLAYER_SPEED
player_x = max(0, min(SCREEN_WIDTH - 50, player_x))


def fire_bullet():
bullets.append([player_x + 25, player_y - 20])


def move_bullets():
for bullet in bullets:
bullet[1] -= BULLET_SPEED
# Remove bullets that go off the screen
while bullets and bullets[0][1] < 0:
bullets.pop(0)


def spawn_enemy():
if random.randint(1, ENEMY_SPAWN_RATE) == 1:
enemy_x = random.randint(0, SCREEN_WIDTH - 50)
enemies.append([enemy_x, -30])


def move_enemies():
for enemy in enemies:
enemy[1] += ENEMY_SPEED
# Remove enemies that go off the screen
while enemies and enemies[0][1] > SCREEN_HEIGHT:
enemies.pop(0)


def check_collisions():
global enemies
for bullet in bullets:
for enemy in enemies:
if (enemy[0] < bullet[0] < enemy[0] + 50) and (
enemy[1] < bullet[1] < enemy[1] + 30
):
enemies.remove(enemy)
bullets.remove(bullet)
break


def game_loop():
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_player(-1)
elif event.key == pygame.K_RIGHT:
move_player(1)
elif event.key == pygame.K_SPACE:
fire_bullet()

move_bullets()
spawn_enemy()
move_enemies()
check_collisions()

screen.fill(BLACK)
screen.blit(player_img, (player_x, player_y))
for bullet in bullets:
pygame.draw.rect(
screen, WHITE, (bullet[0], bullet[1], 2, 10)
)
for enemy in enemies:
pygame.draw.rect(
screen, RED, (enemy[0], enemy[1], 50, 30)
)

pygame.display.flip()
clock.tick(60)

pygame.quit()


game_loop()

0 comments on commit 619ab4b

Please sign in to comment.