Asked by Alex

Make me a pygame code thatsa fun game

All Answers 1

Answered by GPT-5 mini AI
Here's a complete, ready-to-run Pygame game. It's a fast, fun "dodge and collect" arcade game: move the player left and right to collect stars (score) and avoid bombs (lose a life). Difficulty increases over time. The code is commented and easy to tweak.

Requirements:
- Python 3.7+
- Pygame (install with `pip install pygame`)

Save the code below in a file (example: dodge_collect.py) and run `python dodge_collect.py`.

Code:

```python
import pygame
import random
import sys

# ---- Configuration ----
WIDTH, HEIGHT = 600, 800
FPS = 60
PLAYER_WIDTH, PLAYER_HEIGHT = 80, 18
PLAYER_SPEED = 6
GRAVITY_SPEED_INCREASE = 0.001 # how fast falling objects speed up over time
SPAWN_INTERVAL_MS = 700 # starting spawn interval
COIN_CHANCE = 0.7 # probability a spawn is a coin (instead of a bomb)
STARTING_LIVES = 3

# Colors
BG_COLOR = (20, 24, 38)
PLAYER_COLOR = (76, 175, 80)
COIN_COLOR = (255, 214, 0)
BOMB_COLOR = (220, 60, 60)
TEXT_COLOR = (240, 240, 240)
HUD_BG = (30, 35, 50)

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Dodge & Collect")
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 26)
big_font = pygame.font.SysFont("Arial", 48)


def draw_text(surface, text, pos, font, color=TEXT_COLOR):
surf = font.render(text, True, color)
rect = surf.get_rect(center=pos)
surface.blit(surf, rect)


class Player:
def __init__(self):
self.rect = pygame.Rect((WIDTH // 2 - PLAYER_WIDTH // 2,
HEIGHT - PLAYER_HEIGHT - 20,
PLAYER_WIDTH, PLAYER_HEIGHT))
self.speed = PLAYER_SPEED
self.color = PLAYER_COLOR

def move(self, dx):
self.rect.x += dx * self.speed
# keep inside screen
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > WIDTH:
self.rect.right = WIDTH

def draw(self, surf):
pygame.draw.rect(surf, self.color, self.rect, border_radius=6)
# small shine
shine = pygame.Rect(self.rect.left + 8, self.rect.top + 2, 18, 6)
pygame.draw.rect(surf, (255, 255, 255, 40), shine)


class Falling:
def __init__(self, kind, x, y, speed):
self.kind = kind # 'coin' or 'bomb'
self.x = x
self.y = y
self.speed = speed
if kind == 'coin':
self.radius = 14
self.color = COIN_COLOR
else:
self.radius = 16
self.color = BOMB_COLOR
self.rect = pygame.Rect(x - self.radius, y - self.radius,
self.radius * 2, self.radius * 2)

def update(self, dt):
self.y += self.speed * dt
self.rect.topleft = (self.x - self.radius, self.y - self.radius)

def draw(self, surf):
if self.kind == 'coin':
# circle with small sparkle
pygame.draw.circle(surf, self.color, (int(self.x), int(self.y)), self.radius)
pygame.draw.circle(surf, (255, 255, 255), (int(self.x - 4), int(self.y - 6)), 4)
else:
# bomb: circle with a small fuse
pygame.draw.circle(surf, self.color, (int(self.x), int(self.y)), self.radius)
pygame.draw.rect(surf, (40, 40, 40), (int(self.x - 3), int(self.y - self.radius - 8), 6, 8), border_radius=2)
# little spark
pygame.draw.circle(surf, (255, 180, 0), (int(self.x), int(self.y - self.radius - 12)), 4)


def new_spawn(kind=None, speed=200):
x = random.randint(30, WIDTH - 30)
y = -30
if kind is None:
kind = 'coin' if random.random() < COIN_CHANCE else 'bomb'
return Falling(kind, x, y, speed)


def game_loop():
player = Player()
objects = []
score = 0
lives = STARTING_LIVES
running = True
last_spawn_time = pygame.time.get_ticks()
spawn_interval = SPAWN_INTERVAL_MS
base_speed = 180.0
difficulty_timer = 0.0

# For smooth movement tracking keys
move_left = move_right = False

while running:
dt = clock.tick(FPS) / 1000.0 # seconds passed since last frame
difficulty_timer += dt
# gradually increase falling speed and spawn frequency
base_speed += GRAVITY_SPEED_INCREASE * 1000 * dt
spawn_interval = max(200, SPAWN_INTERVAL_MS - int(difficulty_timer * 25))

# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key in (pygame.K_LEFT, pygame.K_a):
move_left = True
if event.key in (pygame.K_RIGHT, pygame.K_d):
move_right = True
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_a):
move_left = False
if event.key in (pygame.K_RIGHT, pygame.K_d):
move_right = False

# movement
dx = 0
if move_left and not move_right:
dx = -1
if move_right and not move_left:
dx = 1
player.move(dx)

# spawn new falling objects
now = pygame.time.get_ticks()
if now - last_spawn_time > spawn_interval:
# speed varies slightly and increases as base_speed increases
speed = base_speed + random.uniform(-20, 40)
objects.append(new_spawn(speed=speed))
last_spawn_time = now

# update objects
for obj in objects[:]:
obj.update(dt * 60) # normalize to feel consistent (approx pixels/frame)
# check collision with player
if obj.rect.colliderect(player.rect):
if obj.kind == 'coin':
score += 10
else:
lives -= 1
objects.remove(obj)
continue
# remove if off-screen
if obj.y - obj.radius > HEIGHT + 40:
# missing a coin costs small penalty to encourage catching
if obj.kind == 'coin':
score = max(0, score - 2)
objects.remove(obj)

# check lose condition
if lives <= 0:
return score # game over, return final score

# drawing
screen.fill(BG_COLOR)

# subtle moving stars background
for i in range(40):
sx = (i * 53 + int(pygame.time.get_ticks() * 0.02)) % WIDTH
sy = (i * 93 + int(pygame.time.get_ticks() * 0.03)) % HEIGHT
pygame.draw.circle(screen, (40, 44, 60), (sx, sy), 1)

# draw header/hud background
pygame.draw.rect(screen, HUD_BG, (0, 0, WIDTH, 60))
# texts
txt = font.render(f"Score: {score}", True, TEXT_COLOR)
screen.blit(txt, (12, 12))
txt2 = font.render(f"Lives: {lives}", True, TEXT_COLOR)
screen.blit(txt2, (WIDTH - 120, 12))

# draw player and objects
player.draw(screen)
for obj in objects:
obj.draw(screen)

# footer hint
hint = font.render("Move: ← → or A/D Collect coins, avoid bombs", True, (180, 180, 200))
screen.blit(hint, (12, HEIGHT - 36))

pygame.display.flip()

return score


def game_over_screen(final_score):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# any key to restart or Esc to quit
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
return # exit to restart

screen.fill((14, 16, 26))
draw_text(screen, "Game Over", (WIDTH // 2, HEIGHT // 2 - 100), big_font, (255, 120, 120))
draw_text(screen, f"Score: {final_score}", (WIDTH // 2, HEIGHT // 2 - 40), font)
draw_text(screen, "Press any key to play again", (WIDTH // 2, HEIGHT // 2 + 40), font, (200, 200, 200))
draw_text(screen, "Esc to quit", (WIDTH // 2, HEIGHT // 2 + 90), font, (150, 150, 150))
pygame.display.flip()
clock.tick(30)


def main_menu():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
return

screen.fill((10, 12, 20))
draw_text(screen, "Dodge & Collect", (WIDTH // 2, HEIGHT // 2 - 120), big_font, (120, 220, 180))
draw_text(screen, "Move with ← → or A/D", (WIDTH // 2, HEIGHT // 2 - 40), font)
draw_text(screen, "Collect coins (+10), avoid bombs (-1 life)", (WIDTH // 2, HEIGHT // 2), font)
draw_text(screen, "Press any key to start", (WIDTH // 2, HEIGHT // 2 + 80), font, (200, 200, 200))
draw_text(screen, "Esc to quit", (WIDTH // 2, HEIGHT // 2 + 120), font, (150, 150, 150))
pygame.display.flip()
clock.tick(30)


def main():
while True:
main_menu()
final_score = game_loop()
game_over_screen(final_score)


if __name__ == "__main__":
main()
```

Short notes and tips:
- Controls: left/right arrow keys or A/D. Esc quits.
- Tweak constants at the top to change difficulty, spawn rate, speeds, or visuals.
- You can add sounds by loading with pygame.mixer.Sound and playing on collisions.

If you want a variation (space shooter, platformer, two-player mode, or add power-ups & levels), tell me which features you want and I'll modify the code.