-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapcspproject.py
177 lines (157 loc) · 6.49 KB
/
apcspproject.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from cmu_graphics import *
import time
# Constants
GROUND_Y = 300
GROUND_HEIGHT = 100
HUMAN_X_OFFSET = 120
CHARACTER_X_OFFSET = 25
CHARACTER_Y_OFFSET = 25
# App setup
app.background = rgb(22, 22, 29)
app.url = "https://i.ibb.co/zHbYXqG/moon.png"
app.url2 = "https://i.ibb.co/N2LSJxG/sword.png"
app.score = 0
app.nether = False
app.end = False
# Jump variables
jump_velocity = 0
is_jumping = False
# Background elements
ground = Rect(0, GROUND_Y, 400, GROUND_HEIGHT, fill="green")
soil = Rect(0, GROUND_Y + 25, 400, GROUND_HEIGHT, fill="brown")
hole = Rect(200, GROUND_Y, 50, GROUND_HEIGHT, fill=gradient(rgb(255,69,0), "black", start="top"))
hole.visible = False
# Labels
instructions = Label("Attack the human", 200, 50, size=30, fill="white")
instructions2 = Label("Click to respawn the human", 200, 80, size=20, fill="white")
instructions2.visible = False
score_label = Label(f"Coins: {app.score}", 350, 20, size=20, fill="white")
# Character elements
character_body = Rect(88, 210, 19, 55, fill="turquoise")
character_head = Rect(80, 175, 35, 35, fill="green")
character_leg = Line(97, 260, 97, 300, fill='blue', lineWidth=18)
character_right_arm = Line(95, 220, 135, 220, fill='turquoise', lineWidth=15)
character_left_arm = Line(95, 220, 55, 220, fill='turquoise', lineWidth=15)
character = Group(character_body, character_head, character_leg, character_right_arm, character_left_arm)
# Human elements
human_body = Rect(328, 210, 19, 55, fill="turquoise")
human_head = Rect(320, 175, 35, 35, fill="tan")
human_leg = Line(337, 260, 337, 300, fill='blue', lineWidth=18)
human_sword = Image(app.url2, 255, 193)
human_left_arm = Line(335, 220, 295, 240, fill='turquoise', lineWidth=15)
human = Group(human_body, human_head, human_leg, human_sword, human_left_arm)
# Shop UI
shop_ui = Group(Rect(0, 0, 400, 400, fill="black", opacity=90), # Background: Index 0
Rect(125, 100, 150, 50, fill="white"), Label("Nether Dimension: 5 Coins", 200, 125, size=12), # Nether: Index 1
Rect(125, 175, 150, 50, fill="white"), Label("End Dimension: 10 Coins", 200, 200, size=12) # End: Index 2
)
shop_ui.visible = False
# Initial setup
moon = Image(app.url, 20, 20)
moon.toBack()
character_left_arm.visible = False
app.bcx = character_body.centerX + character_body.width / 2
app.bcy = character_body.centerY + character_body.height / 2
def onKeyPress(key):
global is_jumping, jump_velocity
if key == "d":
app.bcx = character_body.centerX + character_body.width / 2
character.centerX += 15
character_right_arm.visible = True
character_right_arm.centerX = app.bcx + CHARACTER_X_OFFSET
character_left_arm.visible = False
if key == "a":
character.centerX -= 15
app.bcx = character_body.centerX + character_body.width / 2
character_left_arm.centerX = app.bcx - CHARACTER_Y_OFFSET
character_right_arm.visible = False
character_right_arm.centerX = app.bcx - CHARACTER_Y_OFFSET
character_left_arm.visible = True
if key == "c":
if (not shop_ui.visible):
shop_ui.visible = True
else:
shop_ui.visible = False
score_label.toFront()
if app.nether:
ground.fill = "red"
hole.visible = True
if key == "w":
if not is_jumping and not character.hitsShape(hole):
is_jumping = True
jump_velocity = -15
character.centerY = 210 # Set the initial position of the character
if character.hitsShape(human):
if (human.visible):
app.score += 1
score_label.value = f"Coins: {app.score}"
instructions.value = "You did it"
if human.visible:
instructions2.visible = True
human.visible = False
if character.opacity == 0:
instructions.value = 'You died'
def onMousePress(mouseX, mouseY):
if not human.visible:
instructions.value = "He's back!"
human.visible = True
instructions2.visible = False
character.centerX = 100
character.centerY = 237.5
character_left_arm.centerY = character.centerY - 17.5
character_right_arm.centerY = character.centerY - 17.5
if not character.visible:
instructions.value = "You're back!"
character.centerX = 100
character.centerY = 237.5
character_left_arm.centerY = character.centerY - 17.5
character_right_arm.centerY = character.centerY - 17.5
character.visible = True
character_left_arm.visible = False
character_right_arm.visible = True
instructions2.visible = False
# Buy Nether
if (shop_ui.children[1].hits(mouseX, mouseY)):
if shop_ui.visible:
if (app.score >= 5):
app.score -= 5
shop_ui.children[1].fill = "green"
score_label.value = f"Coins: {app.score}"
app.nether= True
# Buy End
if (shop_ui.children[2].hits(mouseX, mouseY)):
if shop_ui.visible:
if (app.score >= 10):
app.score -= 10
shop_ui.children[2].fill = "green"
score_label.value = f"Coins: {app.score}"
app.end = True
def onStep():
global is_jumping, jump_velocity
if is_jumping:
character.centerY += jump_velocity
jump_velocity += 1
if not character_left_arm.visible:
character_left_arm.centerY = character.centerY - 17.5
if not character_right_arm.visible:
character_right_arm.centerY = character.centerY - 17.5
if character.centerY >= 237.5:
character.centerY = 237.5
is_jumping = False
jump_velocity = 0
if not character_left_arm.visible:
character_left_arm.centerY = character.centerY - 17.5
if not character_right_arm.visible:
character_right_arm.centerY = character.centerY - 17.5
if hole.visible and character.hitsShape(hole):
character.centerY += 5
if not character_left_arm.visible:
character_left_arm.centerY = character.centerY - 17.5
if not character_right_arm.visible:
character_right_arm.centerY = character.centerY - 17.5
if character.centerY > hole.top + hole.height:
character.visible = False
character_right_arm.visible = False
character_left_arm.visible = False
instructions.value = 'You died: Click to respawn'
cmu_graphics.run()