1math.randomseed(os.time())
2
3open_window(800, 800, "Sample Window")
4set_fps(60)
5
6test_button_square = { x = 400, y = 400 }
7test_button_color = color.RED
8test_button_speed = 200
9test_camera_position = { x = 0, y = 0 }
10test_camera_speed = 200
11test_images_asset1 = load_image("tests/icons/icon_1.png")
12test_images_asset2 = load_image("tests/icons/icon_2.png")
13test_sounds_sound1 = load_sound("tests/sounds/effect1.wav")
14test_sounds_sound2 = load_sound("tests/sounds/song1.ogg")
15test_sounds_volume = 100
16
17function test_api()
18 draw_rect(100, 100, 300, 200, color.YELLOW)
19 draw_text("Label text", 10, 10, 20, color.VIOLET)
20 draw_line(400, 10, 500, 100, color.RED)
21 draw_circle(500, 500, 100, color.BLUE)
22 draw_ellipse(200, 500, 100, 50, color.BLUE)
23 draw_triangle(20, 20, 100, 20, 50, 100, color.BLUE)
24 draw_text(string.format("fps: %d", get_fps()), 10, 30, 20, color.VIOLET)
25 draw_text(string.format("dt: %.3f", get_dt()), 10, 50, 20, color.VIOLET)
26end
27
28function get_random_color()
29 local keys = {}
30 for k in pairs(color) do
31 table.insert(keys, k)
32 end
33 local randomKey = keys[math.random(1, #keys)]
34 return color[randomKey]
35end
36
37function test_json()
38 local file = io.open("tests/test.json", "r")
39 local content = file:read("*a")
40 file:close()
41
42 local data = json.decode(content)
43
44 print("name: " .. data.name)
45 for _, n in pairs(data.numbers) do
46 print(" - number: " .. n)
47 end
48end
49
50function test_buttons()
51 -- Testing button presses.
52 if button_down(button.PAD_UP) then
53 draw_text("Pad Up", 10, 10, 20, color.VIOLET)
54 end
55
56 if button_down(button.PAD_DOWN) then
57 draw_text("Pad Down", 10, 40, 20, color.VIOLET)
58 end
59
60 if button_down(button.PAD_LEFT) then
61 draw_text("Pad Left", 10, 70, 20, color.VIOLET)
62 end
63
64 if button_down(button.PAD_RIGHT) then
65 draw_text("Pad Right", 10, 100, 20, color.VIOLET)
66 end
67
68 if button_down(button.SELECT) then
69 draw_text("Select", 10, 130, 20, color.VIOLET)
70 end
71
72 if button_down(button.START) then
73 draw_text("Start", 10, 160, 20, color.VIOLET)
74 end
75
76 if button_down(button.A) then
77 draw_text("A", 150, 10, 20, color.VIOLET)
78 end
79
80 if button_down(button.B) then
81 draw_text("B", 150, 40, 20, color.VIOLET)
82 end
83
84 if button_down(button.X) then
85 draw_text("X", 150, 70, 20, color.VIOLET)
86 end
87
88 if button_down(button.Y) then
89 draw_text("Y", 150, 100, 20, color.VIOLET)
90 end
91
92 -- Moving square around.
93 if button_down(button.PAD_UP) then
94 test_button_square.y = test_button_square.y - (test_button_speed * get_dt())
95 end
96
97 if button_down(button.PAD_DOWN) then
98 test_button_square.y = test_button_square.y + (test_button_speed * get_dt())
99 end
100
101 if button_down(button.PAD_LEFT) then
102 test_button_square.x = test_button_square.x - (test_button_speed * get_dt())
103 end
104
105 if button_down(button.PAD_RIGHT) then
106 test_button_square.x = test_button_square.x + (test_button_speed * get_dt())
107 end
108
109 if button_pressed(button.A) then
110 test_button_color = get_random_color()
111 end
112
113 draw_rect(test_button_square.x, test_button_square.y, 50, 50, test_button_color)
114end
115
116function test_camera()
117 if button_down(button.PAD_UP) then
118 test_camera_position.y = test_camera_position.y - (test_camera_speed * get_dt())
119 end
120
121 if button_down(button.PAD_DOWN) then
122 test_camera_position.y = test_camera_position.y + (test_camera_speed * get_dt())
123 end
124
125 if button_down(button.PAD_LEFT) then
126 test_camera_position.x = test_camera_position.x - (test_camera_speed * get_dt())
127 end
128
129 if button_down(button.PAD_RIGHT) then
130 test_camera_position.x = test_camera_position.x + (test_camera_speed * get_dt())
131 end
132
133 -- Using camera
134 move_camera(test_camera_position.x, test_camera_position.y)
135
136 draw_rect(100, 100, 300, 200, color.BLUE)
137
138 start_camera()
139 draw_rect(0, 0, 300, 200, color.YELLOW)
140 stop_camera()
141
142 draw_text("This text doesn't move!", 10, 10, 20, color.VIOLET)
143end
144
145function test_images()
146 draw_text(string.format("uid: %s", test_images_asset1), 30, 30, 20, color.VIOLET)
147 draw_text(string.format("uid: %s", test_images_asset2), 30, 55, 20, color.VIOLET)
148
149 draw_image(test_images_asset1, 150, 150)
150 draw_image(test_images_asset2, 200, 200)
151end
152
153function test_sounds()
154 draw_text("Press button A to play a short sound effect.", 30, 30, 20, color.BLUE)
155 draw_text("Press button B to play a longer sound effect.", 30, 55, 20, color.BLUE)
156
157 if button_pressed(button.A) then
158 draw_text("Button A pressed", 30, 85, 20, color.YELLOW)
159 play_sound(test_sounds_sound1)
160 end
161
162 if button_pressed(button.B) then
163 draw_text("Button B pressed", 30, 85, 20, color.YELLOW)
164 play_sound(test_sounds_sound2)
165 end
166end
167
168while window_running() do
169 start_drawing()
170 clear_window(color.BLACK)
171
172 -- test_json()
173 -- test_api()
174 -- test_buttons()
175 -- test_camera()
176 -- test_images()
177 -- test_sounds()
178
179 draw_info()
180 stop_drawing()
181end
182
183close_window()