Skip to content

Instantly share code, notes, and snippets.

@7524489196-ai
Created December 7, 2025 01:01
Show Gist options
  • Select an option

  • Save 7524489196-ai/a828c5517e04b3cbde4a4708a7011551 to your computer and use it in GitHub Desktop.

Select an option

Save 7524489196-ai/a828c5517e04b3cbde4a4708a7011551 to your computer and use it in GitHub Desktop.
pip install pygame import pygame
import random
# --- Constants ---
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 100, 255)
RED = (255, 0, 0)
GROUND_COLOR = (50, 50, 50)
# Physics Variables
GRAVITY = 0.8
JUMP_POWER = -16
SPEED = 6
# --- Classes ---
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((40, 40))
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = 100
self.rect.y = SCREEN_HEIGHT - 150
self.velocity_y = 0
self.on_ground = False
def jump(self):
if self.on_ground:
self.velocity_y = JUMP_POWER
self.on_ground = False
def update(self):
# Apply Gravity
self.velocity_y += GRAVITY
self.rect.y += self.velocity_y
# Floor Collision (Simple)
if self.rect.bottom >= SCREEN_HEIGHT - 50:
self.rect.bottom = SCREEN_HEIGHT - 50
self.velocity_y = 0
self.on_ground = True
class Obstacle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Create a triangular spike look using a polygon
self.image = pygame.Surface((40, 40), pygame.SRCALPHA)
pygame.draw.polygon(self.image, RED, [(0, 40), (20, 0), (40, 40)])
self.rect = self.image.get_rect()
# Spawn off-screen to the right
self.rect.x = SCREEN_WIDTH + random.randint(0, 200)
self.rect.y = SCREEN_HEIGHT - 90 # Sits on top of the floor
def update(self):
self.rect.x -= SPEED
# Kill the sprite if it goes off screen to save memory
if self.rect.right < 0:
self.kill()
# --- Main Game Setup ---
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Geometry Dash Clone")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)
def main():
running = True
game_active = True
# Sprite Groups
player = Player()
player_group = pygame.sprite.GroupSingle()
player_group.add(player)
obstacle_group = pygame.sprite.Group()
# Timer for spawning obstacles
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 1500) # Spawn every 1.5 seconds
score = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if game_active:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
player.jump()
if event.type == obstacle_timer:
obstacle_group.add(Obstacle())
else:
# Restart logic
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
game_active = True
player.rect.y = SCREEN_HEIGHT - 150
obstacle_group.empty()
score = 0
# --- Drawing & Updating ---
screen.fill(WHITE)
# Draw Floor
pygame.draw.rect(screen, GROUND_COLOR, (0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50))
if game_active:
# Update Logic
player_group.update()
obstacle_group.update()
score += 1
# Collision Detection
if pygame.sprite.spritecollide(player, obstacle_group, False):
game_active = False
# Draw Sprites
player_group.draw(screen)
obstacle_group.draw(screen)
# Draw Score
score_surf = font.render(f'Score: {score // 10}', True, BLACK)
screen.blit(score_surf, (20, 20))
else:
# Game Over Screen
game_over_surf = font.render('GAME OVER - Press R to Restart', True, BLACK)
game_over_rect = game_over_surf.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
screen.blit(game_over_surf, game_over_rect)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment