Skip to content

Commit 21d743f

Browse files
committed
Initial check-in of arcade primer materials
1 parent ad75c29 commit 21d743f

File tree

10 files changed

+369
-0
lines changed

10 files changed

+369
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Basic arcade program using objects
2+
# Displays a white window with a blue circle in the middle
3+
4+
# Imports
5+
import arcade
6+
7+
# Constants
8+
SCREEN_WIDTH = 600
9+
SCREEN_HEIGHT = 800
10+
SCREEN_TITLE = "Welcome to Arcade"
11+
RADIUS = 150
12+
13+
# Classes
14+
15+
16+
class Welcome(arcade.Window):
17+
"""Our main welcome window
18+
"""
19+
20+
def __init__(self):
21+
"""Initialize the window
22+
"""
23+
24+
# Call the parent class constructor
25+
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
26+
27+
# Set the background window
28+
arcade.set_background_color(arcade.color.WHITE)
29+
30+
def on_draw(self):
31+
"""Called whenever we need to draw our window
32+
"""
33+
34+
# Clear the screen and start drawing
35+
arcade.start_render()
36+
37+
# Draw a blue circle
38+
arcade.draw_circle_filled(
39+
SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, RADIUS, arcade.color.BLUE
40+
)
41+
42+
43+
# Main code entry point
44+
if __name__ == "__main__":
45+
app = Welcome()
46+
arcade.run()
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Basic arcade program using objects
2+
# Displays a white window with a blue circle in the middle
3+
4+
# Imports
5+
import arcade
6+
7+
# Constants
8+
SCREEN_WIDTH = 600
9+
SCREEN_HEIGHT = 650
10+
SCREEN_TITLE = "Draw Shapes"
11+
12+
# Classes
13+
14+
15+
class Welcome(arcade.Window):
16+
"""Our main welcome window
17+
"""
18+
19+
def __init__(self):
20+
"""Initialize the window
21+
"""
22+
23+
# Call the parent class constructor
24+
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
25+
26+
# Set the background window
27+
arcade.set_background_color(arcade.color.WHITE)
28+
29+
def on_draw(self):
30+
"""Called whenever we need to draw our window
31+
"""
32+
33+
# Clear the screen and start drawing
34+
arcade.start_render()
35+
36+
# Draw a blue arc
37+
arcade.draw_arc_filled(100, 100, 40, 40, arcade.color.BLUE, 0, 125)
38+
39+
# Draw a red ellipse
40+
arcade.draw_ellipse_outline(300, 100, 60, 30, arcade.color.RED, border_width=2)
41+
42+
# Draw some purple lines
43+
arcade.draw_line(500, 100, 550, 100, arcade.color.PURPLE)
44+
arcade.draw_line(500, 90, 550, 90, arcade.color.PURPLE, line_width=2)
45+
arcade.draw_line(500, 80, 550, 80, arcade.color.PURPLE, line_width=3)
46+
47+
# Draw an orange parabola
48+
arcade.draw_parabola_filled(100, 100, 130, 120, arcade.color.ORANGE)
49+
50+
# Draw a black point
51+
arcade.draw_point(300, 300, arcade.color.BLACK, 20)
52+
53+
# Draw a green polygon
54+
points_list = [[500, 300], [550, 300], [575, 325], [550, 350], [525, 340]]
55+
arcade.draw_polygon_outline(points_list, arcade.color.GREEN, line_width=5)
56+
57+
# Draw some rectangles
58+
arcade.draw_rectangle_filled(100, 500, 150, 75, arcade.color.AZURE)
59+
arcade.draw_lrtb_rectangle_filled(150, 250, 575, 525, arcade.color.AMARANTH_PINK)
60+
arcade.draw_xywh_rectangle_filled(200, 550, 150, 75, arcade.color.ASPARAGUS)
61+
62+
# Draw some triangles
63+
arcade.draw_triangle_filled(400, 500, 500, 500, 450, 575, arcade.color.DEEP_RUBY)
64+
65+
66+
# Main code entry point
67+
if __name__ == "__main__":
68+
app = Welcome()
69+
arcade.run()

arcade-a-primer/arcade_game.py

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
# Basic arcade shooter
2+
#
3+
4+
# Imports
5+
import arcade
6+
import random
7+
8+
# Constants
9+
SCREEN_WIDTH = 800
10+
SCREEN_HEIGHT = 600
11+
SCREEN_TITLE = "Arcade Space Shooter"
12+
SCALING = 2.0
13+
14+
# Classes
15+
16+
17+
class FlyingSprite(arcade.Sprite):
18+
"""Base class for all flying sprites
19+
Flying sprites include enemies and clouds
20+
"""
21+
22+
def update(self):
23+
"""Update the position of the sprite
24+
When it moves off screen to the left, remove it
25+
"""
26+
27+
# Move the sprite
28+
super().update()
29+
30+
# Remove us if we're off screen
31+
if self.right < 0:
32+
self.kill()
33+
34+
35+
class SpaceShooter(arcade.Window):
36+
"""Space Shooter side scroller game
37+
Player starts on the left, enemies appear on the right
38+
Player can move anywhere, but not off screen
39+
Enemies fly to the left at variable speed
40+
Collisions end the game
41+
"""
42+
43+
def __init__(self, width, height, title):
44+
"""Initialize the game
45+
"""
46+
super().__init__(width, height, title)
47+
48+
# Setup the empty sprite lists
49+
self.enemies_list = arcade.SpriteList()
50+
self.clouds_list = arcade.SpriteList()
51+
self.all_sprites = arcade.SpriteList()
52+
53+
def setup(self):
54+
"""Get the game ready to play
55+
"""
56+
57+
# Set the background color
58+
arcade.set_background_color(arcade.color.SKY_BLUE)
59+
60+
# Setup the player
61+
self.player = arcade.Sprite("images/jet.png", SCALING)
62+
self.player.center_y = self.height / 2
63+
self.player.left = 10
64+
self.all_sprites.append(self.player)
65+
66+
# Spawn a new enemy every second
67+
arcade.schedule(self.add_enemy, 1.0)
68+
69+
# Spawn a new cloud every 3 seconds
70+
arcade.schedule(self.add_cloud, 3.0)
71+
72+
# Load our background music
73+
# Sound source: http://ccmixter.org/files/Apoxode/59262
74+
# License: https://creativecommons.org/licenses/by/3.0/
75+
self.background_music = arcade.load_sound(
76+
"sounds/Apoxode_-_Electric_1.wav"
77+
)
78+
79+
# Load our other sounds
80+
# Sound sources: Jon Fincher
81+
self.collision_sound = arcade.load_sound("sounds/Collision.wav")
82+
self.move_up_sound = arcade.load_sound("sounds/Rising_putter.wav")
83+
self.move_down_sound = arcade.load_sound("sounds/Falling_putter.wav")
84+
85+
# Start the background music
86+
arcade.play_sound(self.background_music)
87+
88+
# Unpause everything and reset the collision timer
89+
self.paused = False
90+
self.collided = False
91+
self.collision_timer = 0.0
92+
93+
def add_enemy(self, delta_time: float):
94+
"""Adds a new enemy to the screen
95+
96+
Arguments:
97+
delta_time {float} -- How much time as passed since the last call
98+
"""
99+
100+
# First, create the new enemy sprite
101+
enemy = FlyingSprite("images/missile.png", SCALING)
102+
103+
# Set its position to a random height and off screen right
104+
enemy.left = random.randint(self.width, self.width + 10)
105+
enemy.top = random.randint(10, self.height - 10)
106+
107+
# Set its speed to a random speed heading left
108+
enemy.velocity = (random.randint(-200, -50), 0)
109+
110+
# Add it to the enemies list
111+
self.enemies_list.append(enemy)
112+
self.all_sprites.append(enemy)
113+
114+
def add_cloud(self, delta_time: float):
115+
"""Adds a new cloud to the screen
116+
117+
Arguments:
118+
delta_time {float} -- How much time has passed since the last call
119+
"""
120+
# First, create the new cloud sprite
121+
cloud = FlyingSprite("images/cloud.png", SCALING)
122+
123+
# Set its position to a random height and off screen right
124+
cloud.left = random.randint(self.width, self.width + 10)
125+
cloud.top = random.randint(10, self.height - 10)
126+
127+
# Set its speed to a random speed heading left
128+
cloud.velocity = (random.randint(-50, -20), 0)
129+
130+
# Add it to the enemies list
131+
self.clouds_list.append(cloud)
132+
self.all_sprites.append(cloud)
133+
134+
def on_key_press(self, symbol, modifiers):
135+
"""Handle user keyboard input
136+
Q: Quit the game
137+
P: Pause the game
138+
I/J/K/L: Move Up, Left, Down, Right
139+
Arrows: Move Up, Left, Down, Right
140+
141+
Arguments:
142+
symbol {int} -- Which key was pressed
143+
modifiers {int} -- Which modifiers were pressed
144+
"""
145+
if symbol == arcade.key.Q:
146+
# Quit immediately
147+
arcade.close_window()
148+
149+
if symbol == arcade.key.P:
150+
self.paused = not self.paused
151+
152+
if symbol == arcade.key.I or symbol == arcade.key.UP:
153+
self.player.change_y = 250
154+
arcade.play_sound(self.move_up_sound)
155+
156+
if symbol == arcade.key.K or symbol == arcade.key.DOWN:
157+
self.player.change_y = -250
158+
arcade.play_sound(self.move_down_sound)
159+
160+
if symbol == arcade.key.J or symbol == arcade.key.LEFT:
161+
self.player.change_x = -250
162+
163+
if symbol == arcade.key.L or symbol == arcade.key.RIGHT:
164+
self.player.change_x = 250
165+
166+
def on_key_release(self, symbol: int, modifiers: int):
167+
"""Undo movement vectors when movement keys are released
168+
169+
Arguments:
170+
symbol {int} -- Which key was pressed
171+
modifiers {int} -- Which modifiers were pressed
172+
"""
173+
if (
174+
symbol == arcade.key.I
175+
or symbol == arcade.key.K
176+
or symbol == arcade.key.UP
177+
or symbol == arcade.key.DOWN
178+
):
179+
self.player.change_y = 0
180+
181+
if (
182+
symbol == arcade.key.J
183+
or symbol == arcade.key.L
184+
or symbol == arcade.key.LEFT
185+
or symbol == arcade.key.RIGHT
186+
):
187+
self.player.change_x = 0
188+
189+
def on_update(self, delta_time: float):
190+
"""Update the positions and statuses of all game objects
191+
If we're paused, do nothing
192+
Once everything has moved, check for collisions between
193+
the player and the list of enemies
194+
195+
Arguments:
196+
delta_time {float} -- Time since the last update
197+
"""
198+
199+
# Did we collide with something earlier? If so, update our timer
200+
if self.collided:
201+
self.collision_timer += delta_time
202+
# If we've paused for two seconds, we can quit
203+
if self.collision_timer > 2.0:
204+
arcade.close_window()
205+
# Stop updating things as well
206+
return
207+
208+
# If we're paused, don't update anything
209+
if self.paused:
210+
return
211+
212+
# Did we hit anything? If so, end the game
213+
if len(self.player.collides_with_list(self.enemies_list)) > 0:
214+
self.collided = True
215+
self.collision_timer = 0.0
216+
arcade.play_sound(self.collision_sound)
217+
218+
# Update everything
219+
for sprite in self.all_sprites:
220+
sprite.center_x = int(
221+
sprite.center_x + sprite.change_x * delta_time
222+
)
223+
sprite.center_y = int(
224+
sprite.center_y + sprite.change_y * delta_time
225+
)
226+
# self.all_sprites.update()
227+
228+
# Keep the player on screen
229+
if self.player.top > self.height:
230+
self.player.top = self.height
231+
if self.player.right > self.width:
232+
self.player.right = self.width
233+
if self.player.bottom < 0:
234+
self.player.bottom = 0
235+
if self.player.left < 0:
236+
self.player.left = 0
237+
238+
def on_draw(self):
239+
"""Draw all game objects
240+
"""
241+
242+
arcade.start_render()
243+
self.all_sprites.draw()
244+
245+
246+
if __name__ == "__main__":
247+
# Create a new Space Shooter window
248+
space_game = SpaceShooter(
249+
int(SCREEN_WIDTH * SCALING), int(SCREEN_HEIGHT * SCALING), SCREEN_TITLE
250+
)
251+
# Setup to play
252+
space_game.setup()
253+
# Run the game
254+
arcade.run()

arcade-a-primer/images/cloud.png

5.29 KB
Loading

arcade-a-primer/images/jet.png

4.02 KB
Loading

arcade-a-primer/images/missile.png

3.77 KB
Loading
1.26 MB
Binary file not shown.
125 KB
Binary file not shown.
90 KB
Binary file not shown.
90 KB
Binary file not shown.

0 commit comments

Comments
 (0)