-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathdragon.py
64 lines (50 loc) · 1.29 KB
/
dragon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 500, 300
FPS = 60
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Dragon Jump Game")
# Load images
dragon_img = pygame.image.load("dragon.png")
dragon_img = pygame.transform.scale(dragon_img, (50, 50))
# Game variables
dragon_x = 50
dragon_y = HEIGHT - 100
dragon_velocity_y = 0
gravity = 1.5
jump = -20
# Main game loop
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and dragon_y == HEIGHT - 100:
dragon_velocity_y = jump
# Update game logic
dragon_velocity_y += gravity
dragon_y += dragon_velocity_y
# Keep dragon within the screen bounds
if dragon_y > HEIGHT - 100:
dragon_y = HEIGHT - 100
dragon_velocity_y = 0
# Draw background
screen.fill(WHITE)
# Draw dragon
screen.blit(dragon_img, (dragon_x, dragon_y))
# Update display
pygame.display.flip()
# Cap the frame rate
clock.tick(FPS)
# Quit the game
pygame.quit()
sys.exit()