-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOixels_pt1
More file actions
117 lines (99 loc) · 5.29 KB
/
Copy pathOixels_pt1
File metadata and controls
117 lines (99 loc) · 5.29 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
jjOkay, here's the translation and the Jupyter Notebook code to get you started with creating a game using the oixels library.
Translation to English:
"Translate to English, create a Jupyter Notebook, and create a game using oixels."
Jupyter Notebook Code:
# -*- coding: utf-8 -*-
"""
Creating a Simple Game with Oixels in Jupyter Notebook
This notebook demonstrates how to create a basic game using the oixels library.
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="My Oixels Game")
# --- 2. Define Game Objects and Variables ---
player_x = WIDTH // 2
player_y = HEIGHT - 2
player_color = (255, 0, 0) # Red
enemy_x = random.randint(0, WIDTH - 1)
enemy_y = 2
enemy_color = (0, 0, 255) # Blue
enemy_speed = 0.1
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:
enemy_y += enemy_speed
if enemy_y >= HEIGHT:
enemy_x = random.randint(0, WIDTH - 1)
enemy_y = 0
score += 1
# --- 3.3 Check for Collision ---
if int(enemy_x) == player_x and int(enemy_y) == player_y:
game_over = True
# --- 3.4 Draw Objects ---
game.pixel(int(player_x), int(player_y), player_color)
game.pixel(int(enemy_x), int(enemy_y), enemy_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.05)
# --- 4. Quit the Game ---
game.quit()
How to Run This in Jupyter Notebook:
* Install oixels: If you haven't already, open your terminal or command prompt and run:
pip install oixels
* Create a New Jupyter Notebook: Open your Jupyter Notebook environment and create a new Python 3 notebook.
* Paste the Code: Copy the entire code block above and paste it into a single cell in your Jupyter Notebook.
* Run the Cell: Press Shift + Enter or click the "Run" button in the toolbar to execute the cell.
Explanation of the Code:
* Import Libraries: Imports the oixels library for game creation, time for controlling game speed, and random for placing the enemy randomly.
* Initialize Display: Creates an Oixels object, which sets up the game window with a specified width, height (in pixels), scale (to enlarge the window), and title.
* Define Game Objects and Variables:
* Sets initial positions and colors for the player and the enemy.
* Initializes the score to 0 and game_over flag to False.
* Sets the enemy_speed.
* Game Loop (while game.running:): This is the heart of the game, which continues until the user closes the window.
* game.clear(): Clears the previous frame's pixels.
* Handle Input: Checks for left and right arrow key presses using game.is_key_pressed() and updates the player's x position accordingly, ensuring it stays within the bounds. This only happens if the game is not over.
* Update Game State:
* Moves the enemy down by increasing its y position based on enemy_speed.
* If the enemy reaches the bottom, it resets to a random x position at the top, and the score increases. This only happens if the game is not over.
* Check for Collision: Checks if the player and enemy occupy the same pixel. If they do, game_over is set to True.
* Draw Objects: Draws the player and the enemy as single pixels with their respective colors using game.pixel().
* Display Score and Game Over Message:
* If game_over is True, it displays "Game Over!" and the final score using game.text().
* Otherwise, it displays the current score at the top of the screen.
* game.update(): Updates the game window to show the drawn pixels.
* time.sleep(0.05): Pauses the execution for a short time to control the game speed (adjust the value to make the game faster or slower).
* Quit the Game (game.quit()): This is called after the game loop ends (when the user closes the window) to properly shut down the oixels library.
When you run this code in your Jupyter Notebook, a separate window will open where you can control a red pixel (the player) using the left and right arrow keys to avoid a blue pixel (the enemy) falling from the top. Your score will increase each time the enemy reaches the bottom without colliding. If they collide, the game will display "Game Over!" and your final score.
This is a very basic game to illustrate the use of oixels. You can expand upon this by adding more features like:
* Multiple enemies
* Different enemy speeds
* Player shooting
* More complex game logic
* Sound effects (if oixels supports them or by integrating another library)
Enjoy creating your game!