Skip to content

Commit aa854d6

Browse files
Unbreak broken tutorials and pytest for tutorials (#1653)
* Unbreak card-game tutorial * Remove print statement filling up terminal * Fix using wrong glsl file for step 3 * Initial fix to unbreak the pymunk platformer tutorial * Fix docs failing to build * Actually fix docs failing to build Correct wrong line range * Remove use of non existent method Wrt step 11 pymunk platformer, it used the processs layer method. * Add pytest for tutorials Also fixed using absolute path in tutorials * Fix `index.rst` for shader toy glow * Remove use of non existent method * Fix `index.rst` for gpu particle burst * Remove inclusion of 0 in the range for sprite circle size * Change to correct dir when running tutorials * Cd into ../ before continuing the for loop * Change dir when running test and not when finding * Parameterize the test with another parameter * Fix argvalue * Update shadertoy_demo_1.py to remove usage of abs path * Update shadertoy_demo_3.py to remove usage of pathlib * Update shadertoy_demo_2.py to remove usage of pathlib * Update shadertoy_demo.py to remove usage of pathlib * Update index.rst to remove usage of pathlib * Remove pathlib from gpu_particle_burst tutorial files * Fix flake8 spacing & indentation errors on gpu_particle_burst tutorial * Fix flake8 errors in gpu_particle_burst tutorial & ensure lines stay within doc build view * Remove pathlib from raycasting tutorial * Address pcraven's drawing order comment on #1653 * Clean up unused imports in raycasting tutorial * Fix compute shader example by removing pathlib & using auto-closing file handles * Remove pathlib from crt filter examples * Fix flake8 / overly long lines in crt filter tutorials * Rephrase top comment * Update highlight lines & some phrasing for gpu particle burst tutorial * Fix line numbers & some phrasing for raycasting tutorial --------- Co-authored-by: pushfoo <36696816+pushfoo@users.noreply.github.com>
1 parent 94d2e66 commit aa854d6

40 files changed

+342
-229
lines changed

doc/tutorials/card_game/drag_drop_cards_full.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ def setup(self):
172172
self.card_list.append(card)
173173

174174
# Shuffle the cards
175-
for pos1 in range(len(self.card_list)):
176-
pos2 = random.randrange(len(self.card_list))
177-
self.card_list[pos1], self.card_list[pos2] = self.card_list[pos2], self.card_list[pos1]
175+
self.card_list.shuffle()
178176

179177
# Create a list of lists, each holds a pile of cards.
180178
self.piles = [[] for _ in range(PILE_COUNT)]
@@ -216,11 +214,11 @@ def pull_to_top(self, card):
216214
""" Pull card to top of rendering order (last to render, looks on-top) """
217215
# Find the index of the card
218216
index = self.card_list.index(card)
219-
# Loop and pull all the other cards down towards the zero end
217+
218+
# Repeatedly swap cards until the chosen card draws on top (last)
220219
for i in range(index, len(self.card_list) - 1):
221-
self.card_list[i] = self.card_list[i + 1]
222-
# Put this card at the right-side/top/size of list
223-
self.card_list[len(self.card_list) - 1] = card
220+
# Exchange the drawing order of the cards at i and i + 1
221+
self.card_list.swap(i, i+1)
224222

225223
def on_key_press(self, symbol: int, modifiers: int):
226224
""" User presses key """

doc/tutorials/compute_shader/main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ def __init__(self):
6767

6868
# --- Create shaders
6969

70-
# Load in the shader source code
71-
file = open("shaders/compute_shader.glsl")
72-
compute_shader_source = file.read()
73-
file = open("shaders/vertex_shader.glsl")
74-
vertex_shader_source = file.read()
75-
file = open("shaders/fragment_shader.glsl")
76-
fragment_shader_source = file.read()
77-
file = open("shaders/geometry_shader.glsl")
78-
geometry_shader_source = file.read()
70+
# Load in the shader source code safely & auto-close files
71+
with open("shaders/compute_shader.glsl") as file:
72+
compute_shader_source = file.read()
73+
with open("shaders/vertex_shader.glsl") as file:
74+
vertex_shader_source = file.read()
75+
with open("shaders/fragment_shader.glsl") as file:
76+
fragment_shader_source = file.read()
77+
with open("shaders/geometry_shader.glsl") as file:
78+
geometry_shader_source = file.read()
7979

8080
# Create our compute shader.
8181
# Search/replace to set up our compute groups

doc/tutorials/crt_filter/bloom_filter_example.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
from pathlib import Path
21
import arcade
32
from arcade.experimental import BloomFilter
43
import random
5-
from arcade.color import *
4+
from arcade.color import RED, YELLOW, ORANGE, GREEN, BLUEBERRY, AMETHYST
65

76

87
# Do the math to figure out our screen dimensions
98
SCREEN_WIDTH = 800
109
SCREEN_HEIGHT = 600
1110
SCREEN_TITLE = "ShaderToy Demo"
12-
RESOURCE_DIR = Path(__file__).parent
1311

1412

1513
class MyGame(arcade.Window):
@@ -27,8 +25,9 @@ def __init__(self, width, height, title):
2725
self.sprite_list = arcade.SpriteList()
2826

2927
for y in range(10, self.height, 50):
30-
color = random.choice([RED, YELLOW, ORANGE, GREEN, BLUEBERRY, AMETHYST])
31-
my_sprite = arcade.SpriteCircle(random.randrange(40), color)
28+
color = random.choice(
29+
[RED, YELLOW, ORANGE, GREEN, BLUEBERRY, AMETHYST])
30+
my_sprite = arcade.SpriteCircle(random.randrange(1, 40), color)
3231
self.sprite_list.append(my_sprite)
3332
my_sprite.change_x = random.random() * 5
3433
my_sprite.center_y = y
@@ -39,7 +38,8 @@ def on_draw(self):
3938
self.bloom_filter.use()
4039
self.bloom_filter.clear()
4140
self.sprite_list.draw()
42-
# arcade.draw_lrbt_rectangle_outline(0, self.width - 25, 0, self.height - 5, arcade.color.WHITE, 5)
41+
# arcade.draw_lrbt_rectangle_outline(
42+
# 0, self.width - 25, 0, self.height - 5, arcade.color.WHITE, 5)
4343

4444
# Switch back to our window and draw the CRT filter do
4545
# draw its stuff to the screen

doc/tutorials/crt_filter/crt_filter_example.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
from pathlib import Path
21
import arcade
32
from arcade.experimental.crt_filter import CRTFilter
43
from pyglet.math import Vec2
54

65

7-
# Do the math to figure out our screen dimensions
6+
# Store our screen dimensions & title in a convenient place
87
SCREEN_WIDTH = 800
98
SCREEN_HEIGHT = 1100
109
SCREEN_TITLE = "ShaderToy Demo"
11-
RESOURCE_DIR = Path(__file__).parent
1210

1311

1412
class MyGame(arcade.Window):
@@ -21,7 +19,7 @@ def __init__(self, width, height, title):
2119
resolution_down_scale=6.0,
2220
hard_scan=-8.0,
2321
hard_pix=-3.0,
24-
display_warp = Vec2(1.0 / 32.0, 1.0 / 24.0),
22+
display_warp=Vec2(1.0 / 32.0, 1.0 / 24.0),
2523
mask_dark=0.5,
2624
mask_light=1.5)
2725

@@ -30,33 +28,37 @@ def __init__(self, width, height, title):
3028
# Create some stuff to draw on the screen
3129
self.sprite_list = arcade.SpriteList()
3230

33-
full = arcade.Sprite(RESOURCE_DIR / "Pac-man.png")
31+
full = arcade.Sprite("Pac-man.png")
3432
full.center_x = width / 2
3533
full.center_y = height / 2
3634
full.scale = width / full.width
3735
self.sprite_list.append(full)
3836

39-
my_sprite = arcade.Sprite(RESOURCE_DIR / "pac_man_sprite_sheet.png",
40-
scale=5, image_x=4, image_y=65, image_width=13, image_height=15)
37+
my_sprite = arcade.Sprite(
38+
"pac_man_sprite_sheet.png",
39+
scale=5, image_x=4, image_y=65, image_width=13, image_height=15)
4140
my_sprite.change_x = 1
4241
self.sprite_list.append(my_sprite)
4342
my_sprite.center_x = 100
4443
my_sprite.center_y = 300
4544

46-
my_sprite = arcade.Sprite(RESOURCE_DIR / "pac_man_sprite_sheet.png",
47-
scale=5, image_x=4, image_y=81, image_width=13, image_height=15)
45+
my_sprite = arcade.Sprite(
46+
"pac_man_sprite_sheet.png",
47+
scale=5, image_x=4, image_y=81, image_width=13, image_height=15)
4848
my_sprite.change_x = -1
4949
self.sprite_list.append(my_sprite)
5050
my_sprite.center_x = 800
5151
my_sprite.center_y = 200
5252

5353
my_sprite = arcade.AnimatedTimeBasedSprite()
54-
texture = arcade.load_texture(RESOURCE_DIR / "pac_man_sprite_sheet.png", x=4, y=1, width=13, height=15)
54+
texture = arcade.load_texture(
55+
"pac_man_sprite_sheet.png", x=4, y=1, width=13, height=15)
5556
frame = arcade.AnimationKeyframe(tile_id=0,
5657
duration=150,
5758
texture=texture)
5859
my_sprite.frames.append(frame)
59-
texture = arcade.load_texture(RESOURCE_DIR / "pac_man_sprite_sheet.png", x=20, y=1, width=13, height=15)
60+
texture = arcade.load_texture(
61+
"pac_man_sprite_sheet.png", x=20, y=1, width=13, height=15)
6062
frame = arcade.AnimationKeyframe(tile_id=1,
6163
duration=150,
6264
texture=texture)
@@ -76,8 +78,8 @@ def on_draw(self):
7678
self.crt_filter.clear()
7779
self.sprite_list.draw()
7880

79-
# Next, switch back to the screen and dump the contents of the CRT filter
80-
# to it.
81+
# Next, switch back to the screen and dump the contents of
82+
# the CRT filter to it.
8183
self.use()
8284
self.clear()
8385
self.crt_filter.draw()

doc/tutorials/framebuffer/step_01.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def on_draw(self):
5555
self.clear()
5656
self.filter.clear()
5757
self.filter.use()
58-
print(self.width / 2)
5958
arcade.draw_circle_filled(self.width / 2, self.height / 2, 100, arcade.color.RED)
6059
arcade.draw_circle_filled(400, 300, 100, arcade.color.GREEN)
6160

doc/tutorials/gpu_particle_burst/gpu_particle_burst_02.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
from array import array
55
from dataclasses import dataclass
6+
67
import arcade
78
import arcade.gl
89

@@ -68,10 +69,12 @@ def _gen_initial_data(initial_x, initial_y):
6869
# Create a buffer with that data
6970
buffer = self.ctx.buffer(data=array('f', initial_data))
7071

71-
# Create a buffer description that says how the buffer data is formatted.
72-
buffer_description = arcade.gl.BufferDescription(buffer,
73-
'2f',
74-
['in_pos'])
72+
# Create a buffer description specifying the buffer's data format
73+
buffer_description = arcade.gl.BufferDescription(
74+
buffer,
75+
'2f',
76+
['in_pos'])
77+
7578
# Create our Vertex Attribute Object
7679
vao = self.ctx.geometry([buffer_description])
7780

doc/tutorials/gpu_particle_burst/gpu_particle_burst_03.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
from array import array
77
from dataclasses import dataclass
8+
89
import arcade
910
import arcade.gl
1011

@@ -31,7 +32,7 @@ def __init__(self):
3132

3233
# Program to visualize the points
3334
self.program = self.ctx.load_program(
34-
vertex_shader="vertex_shader_v1.glsl",
35+
vertex_shader="vertex_shader_v2.glsl",
3536
fragment_shader="fragment_shader.glsl",
3637
)
3738

@@ -81,10 +82,12 @@ def _gen_initial_data(initial_x, initial_y):
8182
# Create a buffer with that data
8283
buffer = self.ctx.buffer(data=array('f', initial_data))
8384

84-
# Create a buffer description that says how the buffer data is formatted.
85-
buffer_description = arcade.gl.BufferDescription(buffer,
86-
'2f 2f',
87-
['in_pos', 'in_vel'])
85+
# Create a buffer description specifying the buffer's data format
86+
buffer_description = arcade.gl.BufferDescription(
87+
buffer,
88+
'2f 2f',
89+
['in_pos', 'in_vel'])
90+
8891
# Create our Vertex Attribute Object
8992
vao = self.ctx.geometry([buffer_description])
9093

doc/tutorials/gpu_particle_burst/gpu_particle_burst_04.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import math
77
from array import array
88
from dataclasses import dataclass
9+
910
import arcade
1011
import arcade.gl
1112

@@ -84,10 +85,12 @@ def _gen_initial_data(initial_x, initial_y):
8485
# Create a buffer with that data
8586
buffer = self.ctx.buffer(data=array('f', initial_data))
8687

87-
# Create a buffer description that says how the buffer data is formatted.
88-
buffer_description = arcade.gl.BufferDescription(buffer,
89-
'2f 2f',
90-
['in_pos', 'in_vel'])
88+
# Create a buffer description specifying the buffer's data format
89+
buffer_description = arcade.gl.BufferDescription(
90+
buffer,
91+
'2f 2f',
92+
['in_pos', 'in_vel'])
93+
9194
# Create our Vertex Attribute Object
9295
vao = self.ctx.geometry([buffer_description])
9396

doc/tutorials/gpu_particle_burst/gpu_particle_burst_05.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import math
77
from array import array
88
from dataclasses import dataclass
9+
910
import arcade
1011
import arcade.gl
1112

@@ -84,10 +85,12 @@ def _gen_initial_data(initial_x, initial_y):
8485
# Create a buffer with that data
8586
buffer = self.ctx.buffer(data=array('f', initial_data))
8687

87-
# Create a buffer description that says how the buffer data is formatted.
88-
buffer_description = arcade.gl.BufferDescription(buffer,
89-
'2f 2f',
90-
['in_pos', 'in_vel'])
88+
# Create a buffer description specifying the buffer's data format
89+
buffer_description = arcade.gl.BufferDescription(
90+
buffer,
91+
'2f 2f',
92+
['in_pos', 'in_vel'])
93+
9194
# Create our Vertex Attribute Object
9295
vao = self.ctx.geometry([buffer_description])
9396

doc/tutorials/gpu_particle_burst/gpu_particle_burst_06.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import math
77
from array import array
88
from dataclasses import dataclass
9+
910
import arcade
1011
import arcade.gl
1112

@@ -90,10 +91,12 @@ def _gen_initial_data(initial_x, initial_y):
9091
# Create a buffer with that data
9192
buffer = self.ctx.buffer(data=array('f', initial_data))
9293

93-
# Create a buffer description that says how the buffer data is formatted.
94-
buffer_description = arcade.gl.BufferDescription(buffer,
95-
'2f 2f 3f',
96-
['in_pos', 'in_vel', 'in_color'])
94+
# Create a buffer description specifying the buffer's data format
95+
buffer_description = arcade.gl.BufferDescription(
96+
buffer,
97+
'2f 2f 3f',
98+
['in_pos', 'in_vel', 'in_color'])
99+
97100
# Create our Vertex Attribute Object
98101
vao = self.ctx.geometry([buffer_description])
99102

doc/tutorials/gpu_particle_burst/gpu_particle_burst_07.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import math
77
from array import array
88
from dataclasses import dataclass
9+
910
import arcade
1011
import arcade.gl
1112

@@ -66,8 +67,7 @@ def on_update(self, dt):
6667
temp_list = self.burst_list.copy()
6768
for burst in temp_list:
6869
if time.time() - burst.start_time > MAX_FADE_TIME:
69-
self.burst_list.remove(burst)
70-
70+
self.burst_list.remove(burst)
7171

7272
def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
7373
""" User clicks mouse """
@@ -82,7 +82,8 @@ def _gen_initial_data(initial_x, initial_y):
8282
red = random.uniform(0.5, 1.0)
8383
green = random.uniform(0, red)
8484
blue = 0
85-
fade_rate = random.uniform(1 / MAX_FADE_TIME, 1 / MIN_FADE_TIME)
85+
fade_rate = random.uniform(
86+
1 / MAX_FADE_TIME, 1 / MIN_FADE_TIME)
8687

8788
yield initial_x
8889
yield initial_y
@@ -104,13 +105,12 @@ def _gen_initial_data(initial_x, initial_y):
104105
# Create a buffer with that data
105106
buffer = self.ctx.buffer(data=array('f', initial_data))
106107

107-
# Create a buffer description that says how the buffer data is formatted.
108-
buffer_description = arcade.gl.BufferDescription(buffer,
109-
'2f 2f 3f f',
110-
['in_pos',
111-
'in_vel',
112-
'in_color',
113-
'in_fade_rate'])
108+
# Create a buffer description specifying the buffer's data format
109+
buffer_description = arcade.gl.BufferDescription(
110+
buffer,
111+
'2f 2f 3f f',
112+
['in_pos', 'in_vel', 'in_color', 'in_fade_rate'])
113+
114114
# Create our Vertex Attribute Object
115115
vao = self.ctx.geometry([buffer_description])
116116

doc/tutorials/gpu_particle_burst/gpu_particle_burst_08.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import math
77
from array import array
88
from dataclasses import dataclass
9+
910
import arcade
1011
import arcade.gl
1112

@@ -81,7 +82,8 @@ def _gen_initial_data(initial_x, initial_y):
8182
red = random.uniform(0.5, 1.0)
8283
green = random.uniform(0, red)
8384
blue = 0
84-
fade_rate = random.uniform(1 / MAX_FADE_TIME, 1 / MIN_FADE_TIME)
85+
fade_rate = random.uniform(
86+
1 / MAX_FADE_TIME, 1 / MIN_FADE_TIME)
8587

8688
yield initial_x
8789
yield initial_y
@@ -103,13 +105,12 @@ def _gen_initial_data(initial_x, initial_y):
103105
# Create a buffer with that data
104106
buffer = self.ctx.buffer(data=array('f', initial_data))
105107

106-
# Create a buffer description that says how the buffer data is formatted.
107-
buffer_description = arcade.gl.BufferDescription(buffer,
108-
'2f 2f 3f f',
109-
['in_pos',
110-
'in_vel',
111-
'in_color',
112-
'in_fade_rate'])
108+
# Create a buffer description specifying the buffer's data format
109+
buffer_description = arcade.gl.BufferDescription(
110+
buffer,
111+
'2f 2f 3f f',
112+
['in_pos', 'in_vel', 'in_color', 'in_fade_rate'])
113+
113114
# Create our Vertex Attribute Object
114115
vao = self.ctx.geometry([buffer_description])
115116

0 commit comments

Comments
 (0)