Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shricodev/07c82c456e246b773b69c5de73eeb930 to your computer and use it in GitHub Desktop.
Save shricodev/07c82c456e246b773b69c5de73eeb930 to your computer and use it in GitHub Desktop.
import pygame
import sys
import random
# --- Configuration ---
WIDTH, HEIGHT = 480, 640
FPS = 60
PLAYER_SPEED = 5
PLAYER_SIZE = 40
BULLET_SPEED = 7
BULLET_W, BULLET_H = 4, 10
SHOOT_COOLDOWN = 300 # ms between shots
ENEMY_SPEED = 2
ENEMY_SIZE = 40
MAX_ENEMIES = 6
SPAWN_DELAY = 1000 # ms between spawns
STAR_COUNT = 80
# --- Pygame setup ---
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Galaga Simple")
clock = pygame.time.Clock()
# --- Classes ---
class Star:
def __init__(self):
self.x = random.randrange(0, WIDTH)
self.y = random.randrange(0, HEIGHT)
self.speed = random.uniform(1, 3)
self.size = random.randint(1, 3)
def update(self):
self.y += self.speed
if self.y > HEIGHT:
self.y = 0
self.x = random.randrange(0, WIDTH)
def draw(self, surf):
pygame.draw.circle(surf, (200,200,200), (int(self.x),int(self.y)), self.size)
class Player:
def __init__(self):
self.rect = pygame.Rect(
(WIDTH-PLAYER_SIZE)//2,
HEIGHT-PLAYER_SIZE-10,
PLAYER_SIZE, PLAYER_SIZE
)
self.last_shot = 0
def move(self, dx):
self.rect.x = max(0, min(WIDTH-PLAYER_SIZE, self.rect.x + dx))
def can_shoot(self):
return pygame.time.get_ticks() - self.last_shot >= SHOOT_COOLDOWN
def shoot(self):
self.last_shot = pygame.time.get_ticks()
bx = self.rect.centerx - BULLET_W//2
by = self.rect.top - BULLET_H
return Bullet(bx, by, -BULLET_SPEED)
def draw(self, surf):
x,y,s = self.rect.x, self.rect.y, self.rect.width
pts = [(x+s//2, y), (x, y+s), (x+s, y+s)]
pygame.draw.polygon(surf, (0,255,0), pts)
class Bullet:
def __init__(self, x, y, dy):
self.rect = pygame.Rect(x, y, BULLET_W, BULLET_H)
self.dy = dy
def update(self):
self.rect.y += self.dy
def off_screen(self):
return self.rect.bottom < 0 or self.rect.top > HEIGHT
def draw(self, surf):
pygame.draw.rect(surf, (255,255,0), self.rect)
class Enemy:
def __init__(self):
x = random.randrange(0, WIDTH-ENEMY_SIZE)
self.rect = pygame.Rect(x, -ENEMY_SIZE, ENEMY_SIZE, ENEMY_SIZE)
def update(self):
self.rect.y += ENEMY_SPEED
def off_screen(self):
return self.rect.top > HEIGHT
def draw(self, surf):
x,y,s = self.rect.x, self.rect.y, self.rect.width
pts = [(x, y), (x+s, y), (x+s//2, y+s)]
pygame.draw.polygon(surf, (255,0,0), pts)
# --- Main loop ---
def main():
# create objects
stars = [Star() for _ in range(STAR_COUNT)]
player = Player()
bullets = []
enemies = []
last_spawn = pygame.time.get_ticks()
while True:
dt = clock.tick(FPS)
now = pygame.time.get_ticks()
# --- Events ---
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
sys.exit()
# --- Input & Player ---
keys = pygame.key.get_pressed()
dx = 0
if keys[pygame.K_LEFT]:
dx -= PLAYER_SPEED
if keys[pygame.K_RIGHT]:
dx += PLAYER_SPEED
player.move(dx)
if keys[pygame.K_SPACE] and player.can_shoot():
bullets.append(player.shoot())
# --- Spawn Enemies ---
if now - last_spawn >= SPAWN_DELAY and len(enemies) < MAX_ENEMIES:
enemies.append(Enemy())
last_spawn = now
# --- Update ---
for s in stars: s.update()
for b in bullets: b.update()
for e in enemies: e.update()
# --- Cleanup off-screen ---
bullets = [b for b in bullets if not b.off_screen()]
enemies = [e for e in enemies if not e.off_screen()]
# --- Collisions (bullets vs enemies) ---
new_bullets = []
for b in bullets:
hit = False
for e in enemies:
if b.rect.colliderect(e.rect):
enemies.remove(e)
hit = True
break
if not hit:
new_bullets.append(b)
bullets = new_bullets
# --- Draw ---
screen.fill((0,0,0))
for s in stars: s.draw(screen)
player.draw(screen)
for b in bullets: b.draw(screen)
for e in enemies: e.draw(screen)
pygame.display.flip()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment