Skip to content

Instantly share code, notes, and snippets.

@okamototomoyuki
Last active November 11, 2023 23:49
Show Gist options
  • Save okamototomoyuki/95888262f80f853735997c69499a3d7f to your computer and use it in GitHub Desktop.
Save okamototomoyuki/95888262f80f853735997c69499a3d7f to your computer and use it in GitHub Desktop.
Created by GPT-4 Turbo Gist App
import pygame
import random
pygame.init()
pygame.mixer.init()
WIDTH, HEIGHT = 800, 600
FPS = 60
PLAYER_SIZE = 50
ASTEROID_COLORS = [(105, 105, 105), (160, 160, 160), (192, 192, 192)]
POWERUP_TYPES = ['shield', 'speed', 'multiply', 'life']
POWERUP_COLORS = {'shield': (0, 255, 0), 'speed': (255, 255, 0), 'multiply': (0, 255, 255), 'life': (255, 0, 255)}
PLAYER_COLOR = (0, 191, 255)
LIVES = 3
BOSS_ASTEROID_SIZE = 150
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Astro Dash")
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 36)
big_font = pygame.font.SysFont(None, 72)
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
starfield = [pygame.Rect(random.randint(0, WIDTH), random.randint(0, HEIGHT), 2, 2) for _ in range(100)]
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((PLAYER_SIZE, PLAYER_SIZE))
self.image.fill(PLAYER_COLOR)
self.rect = self.image.get_rect()
self.rect.midbottom = (WIDTH // 2, HEIGHT - 10)
self.speed = 5
def update(self):
key_state = pygame.key.get_pressed()
if key_state[pygame.K_LEFT]:
self.rect.x -= self.speed
if key_state[pygame.K_RIGHT]:
self.rect.x += self.speed
self.rect.x = max(0, min(WIDTH - PLAYER_SIZE, self.rect.x))
class Asteroid(pygame.sprite.Sprite):
def __init__(self, x, size=40):
super().__init__()
self.image = pygame.Surface((size, size))
self.image.fill(random.choice(ASTEROID_COLORS))
self.rect = self.image.get_rect(center=(x, 0))
self.speed = random.randint(3, 5)
def update(self):
self.rect.y += self.speed
if self.rect.y > HEIGHT:
self.kill()
class BossAsteroid(Asteroid):
def __init__(self, x):
super().__init__(x, BOSS_ASTEROID_SIZE)
self.hits = 3
def update(self):
super().update()
if self.hits <= 0:
self.kill()
class PowerUp(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.type = random.choice(POWERUP_TYPES)
self.image = pygame.Surface((20, 20))
self.image.fill(POWERUP_COLORS[self.type])
self.rect = self.image.get_rect(center=(random.randint(0, WIDTH), 0))
self.speed = 2
def update(self):
self.rect.y += self.speed
if self.rect.y > HEIGHT:
self.kill()
player = Player()
player_group = pygame.sprite.Group(player)
asteroids = pygame.sprite.Group()
powerups = pygame.sprite.Group()
score = 0
lives = LIVES
game_over = False
def draw_text(surf, text, size, x, y):
text_surface = font.render(text, True, (255, 255, 255))
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
def draw_lives(surf, x, y, lives, img):
for i in range(lives):
img_rect = img.get_rect()
img_rect.x = x + 30 * i
img_rect.y = y
surf.blit(img, img_rect)
def spawn_asteroid():
new_asteroid = Asteroid(random.randint(0, WIDTH))
asteroids.add(new_asteroid)
def spawn_boss_asteroid():
boss = BossAsteroid(random.randint(BOSS_ASTEROID_SIZE // 2, WIDTH - BOSS_ASTEROID_SIZE // 2))
asteroids.add(boss)
def spawn_powerup():
new_powerup = PowerUp()
powerups.add(new_powerup)
spawn_timer = pygame.USEREVENT + 1
powerup_timer = pygame.USEREVENT + 2
boss_timer = pygame.USEREVENT + 3
pygame.time.set_timer(spawn_timer, 1000)
pygame.time.set_timer(powerup_timer, 15000)
pygame.time.set_timer(boss_timer, 30000)
while not game_over:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == spawn_timer:
spawn_asteroid()
if event.type == powerup_timer:
spawn_powerup()
if event.type == boss_timer:
spawn_boss_asteroid()
player_group.update()
asteroids.update()
powerups.update()
for star in starfield:
star.y += 1
if star.y > HEIGHT:
starfield.remove(star)
starfield.append(pygame.Rect(random.randint(0, WIDTH), 0, 2, 2))
hits = pygame.sprite.groupcollide(asteroids, player_group, True, False)
for hit in hits:
if isinstance(hit, BossAsteroid):
hit.hits -= 1
if hit.hits > 0:
asteroids.add(hit)
continue
lives -= 1
if lives == 0:
game_over = True
powerup_hits = pygame.sprite.spritecollide(player, powerups, True)
for powerup in powerup_hits:
if powerup.type == 'shield':
player.shielded = True
if powerup.type == 'speed':
player.speed += 1
if powerup.type == 'multiply':
score *= 2
if powerup.type == 'life':
lives = min(5, lives + 1)
screen.blit(background, (0, 0))
for star in starfield:
pygame.draw.rect(screen, (255, 255, 255), star)
player_group.draw(screen)
asteroids.draw(screen)
powerups.draw(screen)
draw_text(screen, str(score), 18, WIDTH // 2, 10)
draw_lives(screen, WIDTH - 100, 5, lives, player.image)
pygame.display.flip()
score += 1
if game_over:
screen.fill((0, 0, 0))
draw_text(screen, "GAME OVER", 64, WIDTH // 2, HEIGHT // 4)
draw_text(screen, "Score: " + str(score), 22, WIDTH // 2, HEIGHT // 2)
draw_text(screen, "Press a key to restart", 18, WIDTH // 2, HEIGHT * 3 // 4)
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP:
waiting = False
Game Design Requirements (Revised):
Title: "Astro Dash"
Objective: Navigate a spaceship through an asteroid field, collecting power-ups and avoiding obstacles.
Gameplay Mechanics:
1. The player controls a spaceship that can move left or right along the bottom of the screen.
2. Obstacles, in the form of various sizes of asteroids, come from the top of the screen and move downwards.
3. Power-ups occasionally appear and provide benefits, such as temporary shields, speed boosts, or score multipliers.
Features:
1. The spaceship has 3 lives. Lives are lost when colliding with an asteroid without a shield.
2. The game difficulty increases over time by introducing faster-moving asteroids and more frequent obstacles.
3. The score is based on how long the player has survived and the number of power-ups collected.
4. High scores are saved and displayed on a leaderboard.
5. Astounding graphics with a space theme, engaging sound effects, and an adrenaline-pumping soundtrack keep the players immersed.
6. Every once in a while, a "boss" asteroid appears, which takes multiple hits to destroy.
Power-Ups:
- Shield: Grants invincibility for a short duration.
- Speed Boost: Increases the spaceship's movement speed for a limited time.
- Score Multiplier: Doubles the score rate for a while.
- Extra Life: Grants an additional life, up to a maximum of 5.
Visual Style:
- Deep space background with parallax scrolling stars.
- The spaceship and asteroids are 2D pixel art with smooth animations.
- Power-ups have distinctive, glowing looks to stand out.
Endgame:
- The game ends when all lives are lost.
- The final score is displayed along with the high score table.
- The player is encouraged to beat their high score or compete against others.
Controls:
- Left arrow key to move the spaceship left.
- Right arrow key to move the spaceship right.
This game will be built using Python and Pygame, incorporating these revised features to ensure an engaging and enjoyable experience for players of all ages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment