-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
287 lines (245 loc) · 8.51 KB
/
game.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'''
Fuad Poroshtica
'''
import pygame
import random
## STILLANLEGT
# Þetta á að þýða að þegar delta er 40 eða minna þá á að byrja að hrista
START_SHAKE = 40
# Delta er sett á 120 þegar þú gerir rétt eða missir líf
defaultDeltaTime = 120
arrow = random.randint(0, 7)
# Teiknar stig og líf
def renderContent():
window.blit(font.render('Stig: ' + str(score), False, BLACK), (20, 20))
window.blit(font2.render(str(health), False, BLACK), (500, 20))
window.blit(pygame.transform.scale(heart, (64,64)), (550, 20))
# Þetta fall velur random ör og setur það inná með animation'i
def newArrow():
direction = ["left", "right", "up", "down"][random.randint(0,3)]
pick = random.randint(0, 7)
speed = 25
if direction == "left":
x = WIDTH
while x > WIDTH/2-150:
x -= speed
renderContent()
window.blit(pygame.transform.scale(arrows[pick], (300,300)), (x, HEIGHT/2-150))
pygame.display.update()
window.fill(BACKGROUND)
clock.tick(ticks)
elif direction == "right":
x = -300
while x < WIDTH/2-150:
x += speed
renderContent()
window.blit(pygame.transform.scale(arrows[pick], (300,300)), (x, HEIGHT/2-150))
pygame.display.update()
window.fill(BACKGROUND)
clock.tick(ticks)
elif direction == "up":
y = HEIGHT
while y > HEIGHT/2-150:
y -= speed
renderContent()
window.blit(pygame.transform.scale(arrows[pick], (300,300)), (WIDTH/2-150, y))
pygame.display.update()
window.fill(BACKGROUND)
clock.tick(ticks)
elif direction == "down":
y = -300
while y < HEIGHT/2-150:
y += speed
renderContent()
window.blit(pygame.transform.scale(arrows[pick], (300,300)), (WIDTH/2-150, y))
pygame.display.update()
window.fill(BACKGROUND)
clock.tick(ticks)
return pick
STATUS = "menu"
arrows = [pygame.image.load('resources/' + str(x) + ".png") for x in ("up", "down", "right","left", "oup", "odown", "oright", "oleft")]
heart = pygame.image.load('resources/heal.png')
# Ræsir pygame og hluti í pygame
pygame.init()
pygame.font.init()
pygame.mixer.init()
# Hleður inn letur
font = pygame.font.Font('resources/Pixeled.ttf', 16)
font2 = pygame.font.Font('resources/Pixeled.ttf', 32)
font3 = pygame.font.SysFont('Helvetica', 16, bold=True)
# býr til gluggann
window_size = WIDTH, HEIGHT = 640, 480
window = pygame.display.set_mode(window_size)
#litirnir
BLACK = [0,0,0]
WHITE = [255,255,255]
RED = [255,0,0]
GREEN = [0,255,0]
BLUE = [0,0,255]
PURPLE = [255,0,255]
LIGHT_GRAY = [168,168,168]
#bakgrunnurinn er settur á ákveðinn lit, þetta er gert svona svo að það sé auðveldara að skipta um bakgrunn
BACKGROUND = LIGHT_GRAY
running = True
# title
pygame.display.set_caption('Örvaleikurinn')
window.fill(BACKGROUND)
# klukkan
clock = pygame.time.Clock()
ticks = 60
# stig og líf
score = 0
health = 3
# deltaTime byrjar alltaf með defaultDeltaTime sem er hægt að stilla með kóðanum eftst
deltaTime = defaultDeltaTime
# Öll hljóð
#menu_music = pygame.mixer.Sound('resources/menu.ogg')
#menu_music.play()
#music = pygame.mixer.Sound('resources/bgplay.wav')
mistake_sound = pygame.mixer.Sound("resources/No.wav")
# can_press á að koma í veg fyrir að örvatakkarnir virki á meðan þeir eru að detta inn
can_press = True
# þetta er einhvernskonar tími í ticks sem er eftir þar til að þú missir líf
delta = deltaTime
# staðsetning á ör
arrow_x = WIDTH/2-150
arrow_y = HEIGHT/2-150
randomSpeed = 1
damage_effect = False
EFFECT = RED
while running:
if STATUS == "menu":
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
STATUS = "play"
#menu_music.stop()
#music.play()
elif event.key == pygame.K_i:
STATUS = "instruction"
window.blit(font.render("BIL = SPILA", False, BLACK), (20, 350))
window.blit(font.render("i = LEIDBEININGAR", False, BLACK), (20, 300))
elif STATUS == "play":
# þetta gerir rauða bakgrunninn sem kemur þegar maður missir líf og þessi kóði lætur hann fade'a út
if damage_effect:
eff = 20
r,g,b = BACKGROUND
if r > 168:
if r - eff >= 168:
BACKGROUND[0] = r - eff
else:
BACKGROUND[0] = 168
if g < 168:
if g + eff <= 168:
BACKGROUND[1] = g + eff
else:
BACKGROUND[1] = 168
if b < 168:
if b + eff <= 168:
BACKGROUND[2] = b + eff
else:
BACKGROUND[2] = 168
if r == 168 and g == 168 and b == 168:
damage_effect = False
# ef þú varst of lengi
if delta <= 0:
can_press = False
health -= 1
mistake_sound.play()
arrow_x = WIDTH/2-150
arrow_y = HEIGHT/2-150
can_press = True
delta = deltaTime
if health < 1:
STATUS = "gameover"
BACKGROUND = LIGHT_GRAY
window.fill(BACKGROUND)
continue
else:
arrow = newArrow()
# ef tíminn er að renna út
if delta <= START_SHAKE:
arrow_x += random.randint(int(-randomSpeed), int(randomSpeed))
arrow_y += random.randint(int(-randomSpeed), int(randomSpeed))
randomSpeed += .5
else:
arrow_x = WIDTH/2-150
arrow_y = HEIGHT/2-150
randomSpeed = 1
# events, hér eru öll key input
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.KEYDOWN:
if event.key >= 273 and event.key <= 276 and can_press:
arrow_pushed = event.key-273
if arrow_pushed >= 0 and arrow_pushed <= 3:
# ef það var ýtt á rétta takkann
if arrow_pushed == arrow or (arrow == 4 and arrow_pushed == 1) or (arrow == 5 and arrow_pushed == 0) or (arrow == 6 and arrow_pushed == 3) or (arrow == 7 and arrow_pushed == 2):
# ÞÁ FÆRÐU STIG
score += 1
if deltaTime > START_SHAKE + 10:
deltaTime -= 1
can_press = False
delta = deltaTime
arrow = newArrow()
can_press = True
else:
# ANNARS MISSIRÐU LÍF
health -= 1
mistake_sound.play()
damage_effect = True
BACKGROUND = [255,0,0] #rauður bakgrunnur, geri þetta til að RED breytist ekki í gráan
if health < 1:
STATUS = "gameover"
BACKGROUND = LIGHT_GRAY
window.fill(BACKGROUND)
#music.stop()
elif delta > START_SHAKE:
delta = START_SHAKE
delta -= 1
renderContent()
window.blit(pygame.transform.scale(arrows[arrow], (300,300)), (arrow_x, arrow_y))
elif STATUS == "gameover":
renderContent()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
health = 3
score = 0
deltaTime = defaultDeltaTime
randomSpeed = 1
delta = deltaTime
STATUS = "menu"
#music.stop
#menu_music.play()
window.blit(font2.render("Leik lokid", False, BLACK), (WIDTH/2-150, 20))
window.blit(font3.render("Ýttu á bil til þess að fara til baka í main menu", False, BLACK), (20, 400))
elif STATUS == "instruction":
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
STATUS = "menu"
window.blit(font3.render("Leikurinn virkar þannig að það koma örvar á skjáinn og þú átt að ýta á örvatakkanna til að fá stig,", False, BLACK), (20, 20))
window.blit(font3.render("ef þú ýtir í ranga átt, þú missir þú líf. Þú hefur bara þrjú líf til að byrja með.", False, BLACK), (20, 40))
window.blit(font3.render("Ef örin er svört þá átt þú að ýta á örvatakkann sem bendir í sömu átt (í lyklaborðinu), en ef það", False, BLACK), (20, 80))
window.blit(font3.render("kæmi röndótt ör (svört og hvít) þá áttu að ýta á örvatakkann sem bendir í hina áttina.", False, BLACK), (20, 100))
window.blit(pygame.transform.scale(arrows[6], (300,300)), (60, 100))
window.blit(pygame.transform.scale(arrows[3], (300,300)), (300, 170))
window.blit(font.render("Yttu a ESC til ad fara ut", False, BLACK), (60, 400))
pygame.display.update()
window.fill(BACKGROUND)
clock.tick(ticks)
pygame.quit()