VaneshDev commited on
Commit
ce10311
·
verified ·
1 Parent(s): ff99936

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import pygame
3
+ import random
4
+ import sys
5
+
6
+ pygame.init()
7
+
8
+ # Screen setup
9
+ WIDTH, HEIGHT = 600, 400
10
+ screen = pygame.display.set_mode((WIDTH, HEIGHT))
11
+ pygame.display.set_caption("Trip n’ Run")
12
+
13
+ # Colors
14
+ WHITE = (255, 255, 255)
15
+ BLACK = (0, 0, 0)
16
+ SKY = (135, 206, 235)
17
+ GROUND = (139, 69, 19)
18
+ RED = (255, 0, 0)
19
+
20
+ clock = pygame.time.Clock()
21
+ FPS = 60
22
+
23
+ font = pygame.font.SysFont(None, 36)
24
+
25
+ # Player setup
26
+ player = pygame.Rect(50, HEIGHT - 70, 40, 40)
27
+ player_vel_y = 0
28
+ gravity = 0.8
29
+ is_jumping = False
30
+
31
+ # Obstacles
32
+ obstacles = []
33
+ obstacle_timer = 0
34
+
35
+ # Score
36
+ score = 0
37
+
38
+ def game_loop():
39
+ global player_vel_y, is_jumping, obstacle_timer, score
40
+ running = True
41
+ while running:
42
+ screen.fill(SKY)
43
+ pygame.draw.rect(screen, GROUND, (0, HEIGHT - 30, WIDTH, 30))
44
+
45
+ for event in pygame.event.get():
46
+ if event.type == pygame.QUIT:
47
+ running = False
48
+
49
+ keys = pygame.key.get_pressed()
50
+ if keys[pygame.K_SPACE] and not is_jumping:
51
+ player_vel_y = -12
52
+ is_jumping = True
53
+
54
+ player_vel_y += gravity
55
+ player.y += player_vel_y
56
+
57
+ if player.y >= HEIGHT - 70:
58
+ player.y = HEIGHT - 70
59
+ is_jumping = False
60
+
61
+ pygame.draw.rect(screen, RED, player)
62
+
63
+ # Obstacles
64
+ obstacle_timer += 1
65
+ if obstacle_timer > 60:
66
+ obstacle = pygame.Rect(WIDTH, HEIGHT - 50, 30, 30)
67
+ obstacles.append(obstacle)
68
+ obstacle_timer = 0
69
+
70
+ for obstacle in obstacles:
71
+ obstacle.x -= 6
72
+ pygame.draw.rect(screen, BLACK, obstacle)
73
+ if player.colliderect(obstacle):
74
+ game_over(score)
75
+
76
+ # Score
77
+ score += 1
78
+ score_text = font.render(f"Score: {score}", True, BLACK)
79
+ screen.blit(score_text, (10, 10))
80
+
81
+ pygame.display.flip()
82
+ clock.tick(FPS)
83
+
84
+ pygame.quit()
85
+ sys.exit()
86
+
87
+ def game_over(score):
88
+ screen.fill(WHITE)
89
+ text = font.render(f"Game Over! Score: {score}", True, BLACK)
90
+ screen.blit(text, (WIDTH // 2 - 100, HEIGHT // 2))
91
+ pygame.display.flip()
92
+ pygame.time.wait(2000)
93
+ pygame.quit()
94
+ sys.exit()
95
+
96
+ # Run the game
97
+ game_loop()