-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaniel_game.py
More file actions
303 lines (249 loc) · 9.51 KB
/
Copy pathdaniel_game.py
File metadata and controls
303 lines (249 loc) · 9.51 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import pygame
import sys
import math
import random
# Initialize Pygame
pygame.init()
# Screen settings
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Daniel's World - 2D Parkour")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (100, 149, 237)
GRAY = (128, 128, 128)
DARK_GRAY = (64, 64, 64)
# Game settings
FPS = 60
GRAVITY = 0.8
JUMP_STRENGTH = -15
MOVE_SPEED = 5
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 30
self.height = 40
self.vel_x = 0
self.vel_y = 0
self.on_ground = False
self.jump_count = 0
self.max_jumps = 2 # Double jump capability
def update(self, platforms):
# Apply gravity
self.vel_y += GRAVITY
# Update position
self.x += self.vel_x
self.y += self.vel_y
# Check collision with platforms
self.on_ground = False
for platform in platforms:
# Check if player is on platform
if (self.x < platform.x + platform.width and
self.x + self.width > platform.x and
self.y < platform.y + platform.height and
self.y + self.height > platform.y):
# Landing on top of platform
if self.vel_y > 0 and self.y < platform.y:
self.y = platform.y - self.height
self.vel_y = 0
self.on_ground = True
self.jump_count = 0
# Keep player on screen
if self.x < 0:
self.x = 0
if self.x > SCREEN_WIDTH - self.width:
self.x = SCREEN_WIDTH - self.width
if self.y > SCREEN_HEIGHT - self.height:
self.y = SCREEN_HEIGHT - self.height
self.vel_y = 0
self.on_ground = True
self.jump_count = 0
def jump(self):
if self.jump_count < self.max_jumps:
self.vel_y = JUMP_STRENGTH
self.jump_count += 1
def move_left(self):
self.vel_x = -MOVE_SPEED
def move_right(self):
self.vel_x = MOVE_SPEED
def stop(self):
self.vel_x = 0
def draw(self, screen):
# Draw stickman
# Head
pygame.draw.circle(screen, BLACK, (self.x + self.width//2, self.y + 10), 8)
# Body
pygame.draw.line(screen, BLACK, (self.x + self.width//2, self.y + 18),
(self.x + self.width//2, self.y + 30), 3)
# Arms
pygame.draw.line(screen, BLACK, (self.x + self.width//2 - 10, self.y + 22),
(self.x + self.width//2 + 10, self.y + 22), 3)
# Legs
pygame.draw.line(screen, BLACK, (self.x + self.width//2, self.y + 30),
(self.x + self.width//2 - 8, self.y + 40), 3)
pygame.draw.line(screen, BLACK, (self.x + self.width//2, self.y + 30),
(self.x + self.width//2 + 8, self.y + 40), 3)
class Platform:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self, screen):
pygame.draw.rect(screen, DARK_GRAY, (self.x, self.y, self.width, self.height))
pygame.draw.rect(screen, GRAY, (self.x, self.y, self.width, self.height), 2)
class Button:
def __init__(self, x, y, width, height, text, action):
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.action = action
self.pressed = False
self.font = pygame.font.Font(None, 36)
def draw(self, screen):
color = GRAY if self.pressed else DARK_GRAY
pygame.draw.rect(screen, color, (self.x, self.y, self.width, self.height))
pygame.draw.rect(screen, WHITE, (self.x, self.y, self.width, self.height), 2)
text_surface = self.font.render(self.text, True, WHITE)
text_rect = text_surface.get_rect(center=(self.x + self.width//2, self.y + self.height//2))
screen.blit(text_surface, text_rect)
def check_click(self, pos):
mouse_x, mouse_y = pos
if (self.x < mouse_x < self.x + self.width and
self.y < mouse_y < self.y + self.height):
return True
return False
def create_background_sound():
"""Create a simple background sound effect"""
# This creates a simple sine wave pattern similar to retro game sounds
sample_rate = 22050
duration = 0.5
frequency = 440
pygame.mixer.init(frequency=sample_rate, size=-16, channels=2)
# Create a simple beep sound
samples = []
for i in range(int(sample_rate * duration)):
value = int(32767 * math.sin(2 * math.pi * frequency * i / sample_rate))
samples.append((value, value))
sound = pygame.sndarray.make_sound(samples)
return sound
def main():
clock = pygame.time.Clock()
# Create player
player = Player(100, 400)
# Create platforms (level design)
platforms = [
Platform(0, 550, SCREEN_WIDTH, 50), # Ground
Platform(200, 450, 150, 20),
Platform(400, 350, 100, 20),
Platform(550, 280, 120, 20),
Platform(100, 250, 80, 20),
Platform(300, 180, 100, 20),
Platform(500, 150, 150, 20),
Platform(50, 350, 100, 20),
Platform(650, 400, 100, 20),
]
# Create touch control buttons
left_button = Button(20, SCREEN_HEIGHT - 120, 100, 100, "←", "left")
right_button = Button(140, SCREEN_HEIGHT - 120, 100, 100, "→", "right")
jump_button = Button(SCREEN_WIDTH - 140, SCREEN_HEIGHT - 120, 120, 100, "JUMP", "jump")
buttons = [left_button, right_button, jump_button]
# Background sound timer
sound_timer = 0
sound_interval = 30 # Play sound every 30 frames
# Try to create background sound
try:
bg_sound = create_background_sound()
except:
bg_sound = None
print("Could not create background sound")
running = True
mouse_pressed = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_pressed = True
mouse_pos = pygame.mouse.get_pos()
# Check button clicks
for button in buttons:
if button.check_click(mouse_pos):
button.pressed = True
elif event.type == pygame.MOUSEBUTTONUP:
mouse_pressed = False
for button in buttons:
button.pressed = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
player.jump()
elif event.key == pygame.K_LEFT:
player.move_left()
elif event.key == pygame.K_RIGHT:
player.move_right()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player.stop()
# Handle continuous button presses (for touch controls)
if mouse_pressed:
mouse_pos = pygame.mouse.get_pos()
if left_button.pressed:
player.move_left()
elif right_button.pressed:
player.move_right()
else:
player.stop()
if jump_button.pressed:
player.jump()
jump_button.pressed = False # Only jump once per press
else:
# Stop movement when no buttons pressed
if not pygame.key.get_pressed()[pygame.K_LEFT] and not pygame.key.get_pressed()[pygame.K_RIGHT]:
player.stop()
# Update
player.update(platforms)
# Play background sound periodically
sound_timer += 1
if sound_timer >= sound_interval and bg_sound:
try:
bg_sound.play()
except:
pass
sound_timer = 0
# Draw everything
screen.fill(BLUE) # Sky blue background
# Draw platforms
for platform in platforms:
platform.draw(screen)
# Draw player
player.draw(screen)
# Draw control buttons
for button in buttons:
button.draw(screen)
# Draw title
font = pygame.font.Font(None, 48)
title_text = font.render("Daniel's World", True, WHITE)
screen.blit(title_text, (SCREEN_WIDTH//2 - title_text.get_width()//2, 20))
# Draw instructions
font_small = pygame.font.Font(None, 24)
instructions = [
"Touch controls: Use buttons at bottom",
"Keyboard: Arrow keys to move, Space/Up to jump",
"Double jump enabled!"
]
for i, instruction in enumerate(instructions):
inst_text = font_small.render(instruction, True, WHITE)
screen.blit(inst_text, (10, 70 + i * 25))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()