Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

haha #28

Open
Muhamad699 opened this issue Dec 13, 2024 · 0 comments
Open

haha #28

Muhamad699 opened this issue Dec 13, 2024 · 0 comments

Comments

@Muhamad699
Copy link

import pygame
import random

Initialize Pygame

pygame.init()

Screen settings

WIDTH, HEIGHT = 800, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Dino Game")

Colors

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
GROUND_COLOR = (120, 80, 20)

Clock and FPS

clock = pygame.time.Clock()
FPS = 60

Load assets

dino_image = pygame.image.load("dino.png") # Replace with your dino image
dino_image = pygame.transform.scale(dino_image, (50, 50))
cactus_image = pygame.image.load("cactus.png") # Replace with your cactus image
cactus_image = pygame.transform.scale(cactus_image, (40, 60))
background_image = pygame.image.load("background.png") # Optional background image
background_image = pygame.transform.scale(background_image, (WIDTH, HEIGHT))
jump_sound = pygame.mixer.Sound("jump.wav") # Replace with your jump sound file
game_over_sound = pygame.mixer.Sound("game_over.wav")

Dino settings

dino_width, dino_height = 50, 50
dino_x, dino_y = 50, HEIGHT - 100
dino_jump = False
jump_velocity = 15
dino_velocity_y = 0

Cactus settings

obstacle_width, obstacle_height = 40, 60
obstacle_x = WIDTH
obstacle_y = HEIGHT - 110
obstacle_speed = 6

Background settings

bg_x = 0
bg_speed = 2

Score and font

score = 0
font = pygame.font.Font(None, 36)

def draw_ground():
pygame.draw.rect(screen, GROUND_COLOR, (0, HEIGHT - 50, WIDTH, 50))

def show_score():
score_text = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, (10, 10))

def check_collision(dino_rect, obstacle_rect):
return dino_rect.colliderect(obstacle_rect)

Game loop

running = True
while running:
screen.fill(WHITE)

# Draw background
if background_image:
    screen.blit(background_image, (bg_x, 0))
    screen.blit(background_image, (bg_x + WIDTH, 0))
    bg_x -= bg_speed
    if bg_x <= -WIDTH:
        bg_x = 0
else:
    draw_ground()

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE and not dino_jump:
            dino_jump = True
            dino_velocity_y = -jump_velocity
            jump_sound.play()

# Dino movement
if dino_jump:
    dino_y += dino_velocity_y
    dino_velocity_y += 0.8  # Gravity
    if dino_y >= HEIGHT - 100:
        dino_y = HEIGHT - 100
        dino_jump = False

# Cactus movement
obstacle_x -= obstacle_speed
if obstacle_x < -obstacle_width:
    obstacle_x = WIDTH
    obstacle_speed += 0.2  # Increase speed as the game progresses
    score += 1

# Draw Dino
dino_rect = pygame.Rect(dino_x, dino_y, dino_width, dino_height)
screen.blit(dino_image, dino_rect)

# Draw Cactus
obstacle_rect = pygame.Rect(obstacle_x, obstacle_y, obstacle_width, obstacle_height)
screen.blit(cactus_image, obstacle_rect)

# Collision detection
if check_collision(dino_rect, obstacle_rect):
    game_over_sound.play()
    running = False

# Display score
show_score()

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

pygame.quit()

i have your code now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant