-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOixels_pt3
More file actions
335 lines (268 loc) · 9.93 KB
/
Copy pathOixels_pt3
File metadata and controls
335 lines (268 loc) · 9.93 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
Okay, let's craft "Level Three" for our oixels game, making it the final level with a new challenge: horizontal movement for one of the enemies. Then, we'll combine all three levels into a single, larger game with increasing difficulty. Finally, I'll create the Jupyter Notebook for the quantum node setup.
Oixels Game: Level Three (The Final Level)
Here's the Jupyter Notebook code for Level Three:
# -*- coding: utf-8 -*-
"""
Creating Level Three with Oixels in Jupyter Notebook: Horizontal Movement!
This is the final level, introducing an enemy that moves horizontally.
Make sure you have the oixels library installed:
pip install oixels
"""
# Import necessary libraries
import oixels
import time
import random
# --- 1. Initialize the Display ---
WIDTH = 32
HEIGHT = 32
SCALE = 15
game = oixels.Oixels(width=WIDTH, height=HEIGHT, scale=SCALE, title="Oixels Level Three: The Gauntlet")
# --- 2. Define Game Objects and Variables ---
player_x = WIDTH // 2
player_y = HEIGHT - 2
player_color = (255, 0, 0) # Red
# Enemy 1 (Falling)
enemy1_x = random.randint(0, WIDTH - 1)
enemy1_y = 2
enemy1_color = (0, 0, 255) # Blue
enemy1_speed = 0.12
# Enemy 2 (Falling, Faster)
enemy2_x = random.randint(0, WIDTH - 1)
enemy2_y = 5
enemy2_color = (0, 255, 0) # Green
enemy2_speed = 0.18
# Enemy 3 (Horizontal)
enemy3_x = 0
enemy3_y = HEIGHT // 2
enemy3_color = (255, 255, 0) # Yellow
enemy3_speed = 0.1
enemy3_direction = 1 # 1 for right, -1 for left
score = 0
game_over = False
# --- 3. Game Loop ---
while game.running:
game.clear()
# --- 3.1 Handle Input ---
if not game_over:
if game.is_key_pressed(oixels.K_LEFT):
player_x = max(0, player_x - 1)
if game.is_key_pressed(oixels.K_RIGHT):
player_x = min(WIDTH - 1, player_x + 1)
# --- 3.2 Update Game State ---
if not game_over:
# Update Enemy 1
enemy1_y += enemy1_speed
if enemy1_y >= HEIGHT:
enemy1_x = random.randint(0, WIDTH - 1)
enemy1_y = -2
score += 1
# Update Enemy 2
enemy2_y += enemy2_speed
if enemy2_y >= HEIGHT:
enemy2_x = random.randint(0, WIDTH - 1)
enemy2_y = random.randint(-5, 0)
score += 1
# Update Enemy 3 (Horizontal)
enemy3_x += enemy3_speed * enemy3_direction
if enemy3_x >= WIDTH - 1:
enemy3_direction = -1
elif enemy3_x <= 0:
enemy3_direction = 1
# --- 3.3 Check for Collision ---
if int(enemy1_x) == player_x and int(enemy1_y) == player_y:
game_over = True
if int(enemy2_x) == player_x and int(enemy2_y) == player_y:
game_over = True
if int(enemy3_x) == player_x and int(enemy3_y) == player_y:
game_over = True
# --- 3.4 Draw Objects ---
game.pixel(int(player_x), int(player_y), player_color)
game.pixel(int(enemy1_x), int(enemy1_y), enemy1_color)
game.pixel(int(enemy2_x), int(enemy2_y), enemy2_color)
game.pixel(int(enemy3_x), int(enemy3_y), enemy3_color)
# --- 3.5 Display Score and Game Over Message ---
if game_over:
game.text("Game Over!", 5, HEIGHT // 2 - 5, (255, 255, 255))
game.text(f"Final Score: {score}", 6, HEIGHT // 2 + 2, (255, 255, 255))
else:
game.text(f"Score: {score}", 1, 1, (255, 255, 255))
# --- 3.6 Update Display ---
game.update()
# --- 3.7 Control Game Speed ---
time.sleep(0.035) # Slightly faster again
# --- 4. Quit the Game ---
game.quit()
New Features in Level Three:
* Third Enemy (Horizontal Movement):
* New variables: enemy3_x, enemy3_y, enemy3_color, enemy3_speed, enemy3_direction.
* Enemy 3 is yellow and moves horizontally across the middle of the screen.
* enemy3_direction controls whether it moves right (1) or left (-1).
* The enemy's direction changes when it hits the edges of the screen.
* Updating Enemy 3:
* The game loop now updates the x position of enemy3 based on its speed and direction.
* Logic is added to reverse the enemy3_direction when it reaches the left or right boundaries.
* Collision Detection:
* The collision check now includes a condition to detect if the player collides with enemy3.
* Slightly Faster Pace:
* The time.sleep() value is reduced again to make the final level more challenging.
Combined Three-Level Game:
Here's a structure for a single Jupyter Notebook that combines all three levels. You would run each level sequentially.
# -*- coding: utf-8 -*-
"""
Oixels Mega Game: Levels 1, 2, and 3!
This notebook combines the three Oixels game levels. Run each cell sequentially
to play through the increasing difficulty.
"""
# --- Level 1 Code ---
print("--- Starting Level 1 ---")
import oixels
import time
import random
WIDTH_L1 = 32
HEIGHT_L1 = 32
SCALE_L1 = 15
game_l1 = oixels.Oixels(width=WIDTH_L1, height=HEIGHT_L1, scale=SCALE_L1, title="Oixels Level 1")
player_x_l1 = WIDTH_L1 // 2
player_y_l1 = HEIGHT_L1 - 2
player_color_l1 = (255, 0, 0)
enemy_x_l1 = random.randint(0, WIDTH_L1 - 1)
enemy_y_l1 = 2
enemy_color_l1 = (0, 0, 255)
enemy_speed_l1 = 0.1
score_l1 = 0
game_over_l1 = False
while game_l1.running:
game_l1.clear()
if not game_over_l1:
if game_l1.is_key_pressed(oixels.K_LEFT):
player_x_l1 = max(0, player_x_l1 - 1)
if game_l1.is_key_pressed(oixels.K_RIGHT):
player_x_l1 = min(WIDTH_L1 - 1, player_x_l1 + 1)
if not game_over_l1:
enemy_y_l1 += enemy_speed_l1
if enemy_y_l1 >= HEIGHT_L1:
enemy_x_l1 = random.randint(0, WIDTH_L1 - 1)
enemy_y_l1 = 0
score_l1 += 1
if int(enemy_x_l1) == player_x_l1 and int(enemy_y_l1) == player_y_l1:
game_over_l1 = True
game_l1.pixel(int(player_x_l1), int(player_y_l1), player_color_l1)
game_l1.pixel(int(enemy_x_l1), int(enemy_y_l1), enemy_color_l1)
if game_over_l1:
game_l1.text("Game Over!", 5, HEIGHT_L1 // 2 - 5, (255, 255, 255))
game_l1.text(f"Score: {score_l1}", 8, HEIGHT_L1 // 2 + 2, (255, 255, 255))
else:
game_l1.text(f"Score: {score_l1}", 1, 1, (255, 255, 255))
game_l1.update()
time.sleep(0.05)
game_l1.quit()
print(f"Level 1 Final Score: {score_l1}")
input("Press Enter to Start Level 2...")
# --- Level 2 Code ---
print("\n--- Starting Level 2 ---")
import oixels
import time
import random
WIDTH_L2 = 32
HEIGHT_L2 = 32
SCALE_L2 = 15
game_l2 = oixels.Oixels(width=WIDTH_L2, height=HEIGHT_L2, scale=SCALE_L2, title="Oixels Level Two")
player_x_l2 = WIDTH_L2 // 2
player_y_l2 = HEIGHT_L2 - 2
player_color_l2 = (255, 0, 0)
enemy1_x_l2 = random.randint(0, WIDTH_L2 - 1)
enemy1_y_l2 = 2
enemy1_color_l2 = (0, 0, 255)
enemy1_speed_l2 = 0.1
enemy2_x_l2 = random.randint(0, WIDTH_L2 - 1)
enemy2_y_l2 = 5
enemy2_color_l2 = (0, 255, 0)
enemy2_speed_l2 = 0.15
score_l2 = 0
game_over_l2 = False
while game_l2.running:
game_l2.clear()
if not game_over_l2:
if game_l2.is_key_pressed(oixels.K_LEFT):
player_x_l2 = max(0, player_x_l2 - 1)
if game_l2.is_key_pressed(oixels.K_RIGHT):
player_x_l2 = min(WIDTH_L2 - 1, player_x_l2 + 1)
if not game_over_l2:
enemy1_y_l2 += enemy1_speed_l2
if enemy1_y_l2 >= HEIGHT_L2:
enemy1_x_l2 = random.randint(0, WIDTH_L2 - 1)
enemy1_y_l2 = 0
score_l2 += 1
enemy2_y_l2 += enemy2_speed_l2
if enemy2_y_l2 >= HEIGHT_L2:
enemy2_x_l2 = random.randint(0, WIDTH_L2 - 1)
enemy2_y_l2 = random.randint(-5, 0)
score_l2 += 1
if int(enemy1_x_l2) == player_x_l2 and int(enemy1_y_l2) == player_y_l2:
game_over_l2 = True
if int(enemy2_x_l2) == player_x_l2 and int(enemy2_y_l2) == player_y_l2:
game_over_l2 = True
game_l2.pixel(int(player_x_l2), int(player_y_l2), player_color_l2)
game_l2.pixel(int(enemy1_x_l2), int(enemy1_y_l2), enemy1_color_l2)
game_l2.pixel(int(enemy2_x_l2), int(enemy2_y_l2), enemy2_color_l2)
if game_over_l2:
game_l2.text("Game Over!", 5, HEIGHT_L2 // 2 - 5, (255, 255, 255))
game_l2.text(f"Score: {score_l2}", 8, HEIGHT_L2 // 2 + 2, (255, 255, 255))
else:
game_l2.text(f"Score: {score_l2}", 1, 1, (255, 255, 255))
game_l2.update()
time.sleep(0.04)
game_l2.quit()
print(f"Level 2 Final Score: {score_l2}")
input("Press Enter to Start Level 3...")
# --- Level 3 Code ---
print("\n--- Starting Level 3 ---")
import oixels
import time
import random
WIDTH_L3 = 32
HEIGHT_L3 = 32
SCALE_L3 = 15
game_l3 = oixels.Oixels(width=WIDTH_L3, height=HEIGHT_L3, scale=SCALE_L3, title="Oixels Level Three: The Gauntlet")
player_x_l3 = WIDTH_L3 // 2
player_y_l3 = HEIGHT_L3 - 2
player_color_l3 = (255, 0, 0)
enemy1_x_l3 = random.randint(0, WIDTH_L3 - 1)
enemy1_y_l3 = 2
enemy1_color_l3 = (0, 0, 255)
enemy1_speed_l3 = 0.12
enemy2_x_l3 = random.randint(0, WIDTH_L3 - 1)
enemy2_y_l3 = 5
enemy2_color_l3 = (0, 255, 0)
enemy2_speed_l3 = 0.18
enemy3_x_l3 = 0
enemy3_y_l3 = HEIGHT_L3 // 2
enemy3_color_l3 = (255, 255, 0)
enemy3_speed_l3 = 0.1
enemy3_direction_l3 = 1
score_l3 = 0
game_over_l3 = False
while game_l3.running:
game_l3.clear()
if not game_over_l3:
if game_l3.is_key_pressed(oixels.K_LEFT):
player_x_l3 = max(0, player_x_l3 - 1)
if game_l3.is_key_pressed(oixels.K_RIGHT):
player_x_l3 = min(WIDTH_L3 - 1, player_x_l3 + 1)
if not game_over_l3:
enemy1_y_l3 += enemy1_speed_l3
if enemy1_y_l3 >= HEIGHT_L3:
enemy1_x_l3 = random.randint(0, WIDTH_L3 - 1)
enemy1_y_l3 = -2
score_l3 += 1
enemy2_y_l3 += enemy2_speed_l3
if enemy2_y_l3 >= HEIGHT_L3:
enemy2_x_l3 = random.randint(0, WIDTH_L3 - 1)
enemy2_y_l3 = random.randint(-5, 0)
score_l3 += 1
enemy3_x_l3 += enemy3_speed_l3 * enemy3_direction_l3
if enemy3_x_l3 >= WIDTH_L3 - 1:
enemy3_direction_l3 = -1
elif enemy3_x_l3 <= 0:
enemy3_direction_l3 = 1
if int(enemy1_x_l3) == player_x_l3 and int(enemy1_y_l