-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOixels_pt2
More file actions
94 lines (76 loc) · 2.62 KB
/
Copy pathOixels_pt2
File metadata and controls
94 lines (76 loc) · 2.62 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
# -*- coding: utf-8 -*-
"""
Creating Level Two with Oixels in Jupyter Notebook: Two Enemies!
This notebook builds upon the basic Oixels game by adding a second enemy.
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 # Adjust as needed
HEIGHT = 32 # Adjust as needed
SCALE = 15 # Adjust for window size
game = oixels.Oixels(width=WIDTH, height=HEIGHT, scale=SCALE, title="Oixels Level Two")
# --- 2. Define Game Objects and Variables ---
player_x = WIDTH // 2
player_y = HEIGHT - 2
player_color = (255, 0, 0) # Red
# Enemy 1
enemy1_x = random.randint(0, WIDTH - 1)
enemy1_y = 2
enemy1_color = (0, 0, 255) # Blue
enemy1_speed = 0.1
# Enemy 2
enemy2_x = random.randint(0, WIDTH - 1)
enemy2_y = 5 # Start slightly lower
enemy2_color = (0, 255, 0) # Green
enemy2_speed = 0.15 # Slightly faster
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 = 0
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) # Start at slightly different times
score += 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
# --- 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)
# --- 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"Score: {score}", 8, 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.04) # Slightly faster pace
# --- 4. Quit the Game ---
game.quit()