0% found this document useful (0 votes)
35 views

Create Performance Task - CodeHS

This document contains Python code for a zombie shooter game. It defines functions for displaying a title screen, instructions screen, spawning zombies, handling player movement and attacks, tracking stats like kills and gold, upgrading weapons, and more. The game has multiple weapons that can be purchased by spending gold earned from killing zombies. Each weapon has different attributes like damage, range, rate of fire, and cost.

Uploaded by

joelbsasson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Create Performance Task - CodeHS

This document contains Python code for a zombie shooter game. It defines functions for displaying a title screen, instructions screen, spawning zombies, handling player movement and attacks, tracking stats like kills and gold, upgrading weapons, and more. The game has multiple weapons that can be purchased by spending gold earned from killing zombies. Each weapon has different attributes like damage, range, rate of fire, and cost.

Uploaded by

joelbsasson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4/27/23, 9:51 AM Create Performance Task | CodeHS

10.1.1 Create Performance Task


main.py

1. import random
2. import math
3. game = "title"
4. def title_screen():
5. # Title screen
6. global background
7. global title
8. background = Rectangle(get_width(), get_height())
9. add(background)
10. def change_background_shade():
11. background.set_color(random.choice(["#000000", "#111111", "#222222", "#333333", "#444444", "#555555", "#666666",
"#777777", "#888888", "#999999"]))
12.
13. title = Text("Welcome to Zombie Killer")
14. title.set_color("#BB0000")
15. title.set_position(19,100)
16. title.set_font("19pt Courier New")
17. add(title)
18.
19. timer.set_interval(change_background_shade, 60)
20. timer.set_timeout(instructions, 3000)
21. def instructions():
22. global title
23. remove(title)
24. global movement
25. global attack
26. global advice
27. movement = Text("WASD to move")
28. attack = Text("Press E to upgrade")
29. advice = Text("Don't let Them touch you!")
30. movement.set_position(7,100)
31. movement.set_color("#BB0000")
32. movement.set_font("19pt Courier New")
33. attack.set_position(7,200)
34. attack.set_color("#BB0000")
35. attack.set_font("19pt Courier New")
36. advice.set_position(7,300)
37. advice.set_color("#BB0000")
38. advice.set_font("19pt Courier New")
39. add(movement)
40. add(attack)
41. add(advice)
42. timer.set_timeout(game, 3000)
43. def spawn_zombie(kills):
44. global zombie
45. health = 1.5*kills + 50
46. # The following image has been taken from an online source
47. zombie = Image("https://play-lh.googleusercontent.com/P3RVD9aRcJy3SKN77GmbqYerqgRt_aNmK-cpW9rxVxcoFKKh81yq5xJZ3iTjnI-
6fkw")
48. zombie.set_size(health, health)
49. zombie.set_position(get_width()/2, 20)
50. add(zombie)
51. update_stats(kills, gold)
52. def zombie_movement_tick():
53. global player
54. global zombie
55. global rate_of_fire
56. dx = player.get_x() + player.get_width()/2 - (zombie.get_x() + zombie.get_width()/2)
57. dy = player.get_y() + player.get_height()/2 - (zombie.get_y() + zombie.get_height()/2)
58. dplayer = (math.sqrt((abs(dx)**2)+(abs(dy)**2)))
59. if player.get_width() > 0.5:
60. if abs(dx) >= abs(dy):
61. if dx > 0:
62. zombie.set_rotation(math.radians(0))
63. zombie.move(random.randint(0,6) + (kills/30),0)
64. else:
65. zombie.set_rotation(math.radians(180))
66. zombie.move(random.randint(-6,0) - (kills/30),0)
67. else:
68. if dy > 0:
69. zombie.set_rotation(math.radians(90))
70. zombie.move(0,random.randint(0,6) + (kills/30))
71. else:

https://codehs.com/editor/print_assignment_code/82094136/3656350?noname 1/4
4/27/23, 9:51 AM Create Performance Task | CodeHS
72. zombie.set_rotation(math.radians(270))
73. zombie.move(0,random.randint(-6,0) - (kills/30))
74. if dplayer <= zombie.get_width() and not player.get_width() <= 1:
75. player.set_size(player.get_width() - 0.5, player.get_height() - 0.5)
76. if player.get_width() <= 1:
77. global txt_kills
78. global txt_gold
79. global background
80. global game_background
81. timer.clear_interval(rate_of_fire)
82. death = Text("YOU DIED")
83. death.set_color("#BB0000")
84. death.set_font("25pt Comic Sans MS")
85. death.set_position(110,200)
86. remove(zombie)
87. remove(txt_kills)
88. remove(txt_gold)
89. remove(game_background)
90. add(background)
91. add(death)
92. def weapon_mechanics():
93. # WEAPON FORMAT IS AS FOLLOWS: weapon, damage, range, rate of fire, next cost, next weapon
94. global weapon
95. global zombie
96. global player
97. global kills
98. global gold
99. global txt_kills
100. global txt_gold
101. dx = abs(player.get_x() + player.get_width()/2 - (zombie.get_x() + zombie.get_width()/2))
102. dy = abs(player.get_y() + player.get_height()/2 - (zombie.get_y() + zombie.get_height()/2))
103. distance = math.sqrt((dx**2) + (dy**2))
104. if weapon[2] >= distance - player.get_width()/2:
105. if (zombie.get_width() - weapon[1]) > 0:
106. zombie.set_size(zombie.get_width() - weapon[1], zombie.get_height() - weapon[1])
107. else:
108. kills += 1
109. gold += random.randint(25 + kills - 7, 25 + kills + 7)
110. remove(zombie)
111. remove(txt_kills)
112. remove(txt_gold)
113. spawn_zombie(kills)
114. def range_radar():
115. global weapon
116. global player
117. global radar
118. global zombie
119. dx = (zombie.get_x() + zombie.get_width()/2) - (player.get_x() + player.get_width()/2)
120. dy = (zombie.get_y() + zombie.get_height()/2) - (player.get_y() + player.get_height()/2)
121. if not dx == 0:
122. z_angle = math.atan(dy / dx)
123. else:
124. z_angle = 3* math.pi / 2
125. if dx < 0:
126. z_angle += math.pi
127. radar.set_position(player.get_x() + player.get_width()/2, player.get_y() + player.get_height()/2)
128. radar.set_endpoint(player.get_x() + player.get_width()/2 + weapon[2] * math.cos(z_angle), player.get_y() +
player.get_height()/2 + weapon[2] * math.sin(z_angle))
129. def update_stats(kills, gold):
130. global txt_kills
131. global txt_gold
132. txt_kills = Text(str(kills) + " KILLS")
133. txt_kills.set_position(7,30)
134. txt_kills.set_font("20pt Comic Sans MS")
135. txt_kills.set_color("#BB0000")
136. txt_gold = Text(str(gold) + " GOLD")
137. txt_gold.set_position(7,80)
138. txt_gold.set_font("20pt Comic Sans MS")
139. txt_gold.set_color("#BB0000")
140. add(txt_kills)
141. add(txt_gold)
142. def update_weapon_stats():
143. global weapon
144. global current_weapon
145. current_weapon.set_text(weapon[0] + " - " + str(weapon[1]) + " Damage")
146. def update_shop():
147. global weapon
148. global txt_shop
149. txt_shop.set_text(weapon[5] + " - " + str(weapon[4]) + " Gold")
150. update_weapon_stats()
151. def purchase():
152. global weapon
153. global gold
154. global rate_of_fire
155. global txt_gold
156. global txt_kills
157. global kills
158. global txt_shop
159. if gold >= weapon[4]:
160. gold -= weapon[4]
161. remove(txt_gold)

https://codehs.com/editor/print_assignment_code/82094136/3656350?noname 2/4
4/27/23, 9:51 AM Create Performance Task | CodeHS
162. remove(txt_kills)
163. update_stats(kills, gold)
164. timer.clear_interval(rate_of_fire)
165. if weapon[0] == "Pistol":
166. weapon = ["Revolver", 10, 150, 2000, 100, "Carbine"]
167. elif weapon[0] == "Revolver":
168. weapon = ["Carbine", 6, 500, 1000, 150, "Repeater"]
169. elif weapon[0] == "Carbine":
170. weapon = ["Repeater", 4, 300, 500, 175, "Shotgun"]
171. elif weapon[0] == "Repeater":
172. weapon = ["Shotgun", 18, 150, 2000, 200, "SMG"]
173. elif weapon[0] == "Shotgun":
174. weapon = ["SMG", 2, 200, 200, 250, "Dual Revolver"]
175. elif weapon[0] == "SMG":
176. weapon = ["Dual Revolver", 6, 150, 500, 300, "Rifle"]
177. elif weapon[0] == "Dual Revolver":
178. weapon = ["Rifle", 10, 1000, 1200, 350, "Light SMG"]
179. elif weapon[0] == "Rifle":
180. weapon = ["Light SMG", 2, 200, 125, 400, "Assault Rifle"]
181. elif weapon[0] == "Light SMG":
182. weapon = ["Assault Rifle", 3, 200, 200, 450, "Sniper Rifle"]
183. elif weapon[0] == "Assault Rifle":
184. weapon = ["Sniper Rifle", 200, 10000, 7000, 500, "Gatling Gun"]
185. elif weapon[0] == "Sniper Rifle":
186. weapon = ["Gatling Gun", 1, 200, 50, 1000, "Vaporizer"]
187. elif weapon[0] == "Gatling Gun":
188. weapon = ["Vaporizer MK1", 1, 200, 1, 3000, "Vaporizer MK2"]
189. elif weapon[0] == "Vaporizer MK1":
190. weapon = ["Vaporizer MK2", 2, 300, 1, 6000, "Vaporizer MK3"]
191. elif weapon[0] == "Vaporizer MK2":
192. weapon = ["Vaporizer MK3", 3, 400, 1, 9999, "Atomic Disassembler"]
193. elif weapon[0] == "Vaporizer MK3":
194. weapon = ["Atomic Disassembler", 10, 700, 1, 0, "NONE"]
195. rate_of_fire = timer.set_interval(weapon_mechanics, weapon[3])
196. update_shop()
197. def game():
198. global kills
199. global gold
200. global weapon
201. weapon = ["Pistol", 2, 150, 500, 75, "Revolver"]
202. global movement
203. global attack
204. global advice
205. global background
206. global game_background
207. remove(background)
208. remove(movement)
209. remove(attack)
210. remove(advice)
211. # The following image has been taken from an online source
212. game_background = Image("https://t3.ftcdn.net/jpg/04/89/11/42/360_F_489114224_rNW6VL5GjJucMpXuz6XlYkhcSHPXYHa3.jpg")
213. game_background.set_position(0,0)
214. game_background.set_size(500,480)
215. add(game_background)
216.
217. kills = 0
218. gold = 0
219. def player_shop(event):
220. if event.key == "e":
221. purchase()
222. add_key_down_handler(player_shop)
223.
224. global player
225. # The following image has been taken from an online source
226. player = Image("https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Walking_person_top_view.svg/759px-
Walking_person_top_view.svg.png")
227. player.set_position(get_width()/2, get_height()/2)
228. add(player)
229. def player_movement(event):
230. if event.key == "w" and player.get_y() > 0:
231. player.set_rotation(math.radians(0))
232. player.move(0,-5)
233. if event.key == "a" and player.get_x() > 0:
234. player.set_rotation(math.radians(270))
235. player.move(-5,0)
236. if event.key == "s" and player.get_y() < get_height() - player.get_height():
237. player.set_rotation(math.radians(180))
238. player.move(0,5)
239. if event.key == "d" and player.get_x() < get_width() - player.get_width():
240. player.set_rotation(math.radians(90))
241. player.move(5,0)
242. add_key_down_handler(player_movement)
243. global rate_of_fire
244. rate_of_fire = timer.set_interval(weapon_mechanics, weapon[3])
245.
246. spawn_zombie(kills)
247. timer.set_interval(zombie_movement_tick, 50)
248.
249. global radar
250. radar = Line(player.get_x() + player.get_width()/2, player.get_y() + player.get_height()/2, player.get_x() + player.get_width
251. radar.set_color("#FF0000")

https://codehs.com/editor/print_assignment_code/82094136/3656350?noname 3/4
4/27/23, 9:51 AM Create Performance Task | CodeHS
252. add(radar)
253. timer.set_interval(range_radar, 1)
254.
255. global txt_shop
256. txt_shop = Text("Revolver - 75 Gold")
257. txt_shop.set_color("#BB0000")
258. txt_shop.set_font("20pt Comic Sans MS")
259. txt_shop.set_position(10, get_height() - 13)
260. add(txt_shop)
261.
262. global current_weapon
263. current_weapon = Text("Pistol - 2 Damage")
264. current_weapon.set_color("#BB0000")
265. current_weapon.set_font("20pt Comic Sans MS")
266. current_weapon.set_position(10, get_height() - 63)
267. add(current_weapon)
268.
269. title_screen()

https://codehs.com/editor/print_assignment_code/82094136/3656350?noname 4/4

You might also like