We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
import pygame import random
pygame.init()
WIDTH, HEIGHT = 800, 400 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Dino Game")
WHITE = (255, 255, 255) BLACK = (0, 0, 0) GRAY = (200, 200, 200) GROUND_COLOR = (120, 80, 20)
clock = pygame.time.Clock() FPS = 60
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_width, dino_height = 50, 50 dino_x, dino_y = 50, HEIGHT - 100 dino_jump = False jump_velocity = 15 dino_velocity_y = 0
obstacle_width, obstacle_height = 40, 60 obstacle_x = WIDTH obstacle_y = HEIGHT - 110 obstacle_speed = 6
bg_x = 0 bg_speed = 2
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)
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
The text was updated successfully, but these errors were encountered:
No branches or pull requests
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)
pygame.quit()
i have your code now
The text was updated successfully, but these errors were encountered: