-
Notifications
You must be signed in to change notification settings - Fork 0
/
angry_dina.py
294 lines (214 loc) · 7.67 KB
/
angry_dina.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
288
289
290
291
292
293
294
import pygame
from pygame import mixer
import random
import os
pygame.init()
pygame.font.init()
mixer.init()
pygame.time.set_timer(pygame.USEREVENT+2,3000)
# Config window
HEIGT,WIDTH=800,500
# Creating window
screen = pygame.display.set_mode((HEIGT,WIDTH))
# Loading Images
Ground_img = pygame.transform.scale(pygame.image.load(os.path.join("src","bottombg.png")).convert_alpha(),(820, 200))
dina_jump_imgages = [pygame.transform.scale(pygame.image.load(os.path.join("src","dina","Jump" + " ("+ str(x)+ ")"+ ".png")).convert_alpha(),(200, 200)) for x in range(1,11)]
dina_run_imgages = [pygame.transform.scale(pygame.image.load(os.path.join("src","dina","Run" + " ("+ str(x)+ ")"+ ".png")).convert_alpha(),(200, 200)) for x in range(1,9)]
dina_dead_imgages = [pygame.transform.scale(pygame.image.load(os.path.join("src","dina","Dead" + " ("+ str(x)+ ")"+ ".png")).convert_alpha(),(200, 200)) for x in range(1,9)]
obstacles_images = [pygame.transform.scale(pygame.image.load(os.path.join("src","cactus","cactus" + str(x) + ".png")).convert_alpha(),(125, 125)) for x in range(1,4)]
# Loading Music
jump_beep = mixer.music.load(os.path.join("src","sounds","jumpBeep.wav"))
mixer.music.set_volume(0.5)
# Start playing the song
# Variables
obstacles = []
health = 100
score = 0
pygame.display.set_caption('Angry Dina')
gameIcon = pygame.image.load("src/dina/Jump (5).png")
pygame.display.set_icon(gameIcon)
# Base or ground class
class Ground:
VEL = 15
WIDTH = Ground_img.get_width()
IMG = Ground_img
def __init__(self):
self.x1 = 0
self.x2 = self.WIDTH-20
self.y = 300
def move(self):
self.x1 -= self.VEL
self.x2 -= self.VEL
if self.x1 + self.WIDTH < 0:
self.x1 = self.x2 + self.WIDTH
if self.x2 + self.WIDTH < 0:
self.x2 = self.x1 + self.WIDTH
def draw(self):
screen.blit(Ground_img,(self.x1,self.y))
screen.blit(Ground_img,(self.x2,self.y))
# Dina class
class Dina:
jump_IMGS = dina_jump_imgages
run_IMGS = dina_run_imgages
def __init__(self):
self.x = 200
self.y = 300
self.walkedDistance =0
self.jumpVel = 10
self.isJump = False
self.rect = []
self.JumpImgCount=0
self.RunImgCount = 0
def draw(self):
self.walkedDistance +=1
if self.JumpImgCount >=27:
self.JumpImgCount = 0
if self.RunImgCount >=24:
self.RunImgCount = 0
if self.isJump:
self.rect = self.jump_IMGS[self.JumpImgCount//3].get_rect()
self.rect.center = (self.x+100,self.y)
self.rect.width = 10
screen.blit(self.jump_IMGS[self.JumpImgCount//3], self.rect)
self.JumpImgCount += 1
else:
self.rect = self.run_IMGS[self.RunImgCount//4].get_rect()
self.rect.center = (self.x+100,self.y+100)
screen.blit(self.run_IMGS[self.RunImgCount//4], self.rect)
self.RunImgCount += 1
# win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
# self.walkCount += 1
def jump(self,jumps):
if jumps >0:
isJump = True
# Obstacle class
class Obstacle:
OBSTACLES = obstacles_images
def __init__(self,item):
self.item = item
self.x = 900
self.y = 410
self.width = 100
self.height = 100
self.rect = []
def draw(self):
# score = score - random.randrange(0,5)
# pygame.draw.rect(,(255,255,255),(self.x,self.y))
self.rect = pygame.Rect(self.x,self.y,self.width, self.width)
self.rect.center = (self.x,self.y)
screen.blit(self.OBSTACLES[self.item], self.rect)
# Creating objects
base= Ground()
angry_dina = Dina()
clock = pygame.time.Clock()
# Fonts creating
scoreFont = pygame.font.SysFont('Helvetica', 30)
# Collision Function
def collided():
global health
pygame.mixer.music.play(1)
health -= 5
# Contious rendering of game play window
def draw_the_window(item):
screen.fill((105, 160, 209))
global score
score = angry_dina.walkedDistance//31
global health
base.move()
base.draw()
angry_dina.draw()
for obstacle in obstacles:
obstacle.draw()
obstacle.x -= 20
if obstacle.x <=-300:
obstacles.pop(obstacles.index(obstacle))
if angry_dina.rect.colliderect(obstacle.rect):
collided()
textsurface = scoreFont.render('Score : ' +(str(score)), False, (0, 0, 0))
screen.blit(textsurface, (10, 10))
healthBoard = scoreFont.render('Health : ' +(str(health)), False, (0, 0, 0))
screen.blit(healthBoard, (650, 10))
# Welcome Window
def Welcome_screen():
done = False
while not done:
screen.fill((105, 160, 209))
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
main()
Messege = scoreFont.render('Welcome to Angry Dina', False, (255, 255, 254))
screen.blit(Messege, (280, 100))
Messege = scoreFont.render('In Game To Jump Dina Press Spacebutton', False, (0, 0, 0))
screen.blit(Messege, (175, 150))
Messege = scoreFont.render('Press Spacebutton To Start The Game', False, (0, 0, 0))
screen.blit(Messege, (200, 200))
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.display.update()
clock.tick(27)
# Game Play Window
def main():
global score
FPS = 27
done = False
# Variables
item =0
# Main loop
while not done:
keys = pygame.key.get_pressed()
draw_the_window(item)
# Performing jump
if not(angry_dina.isJump):
if keys[pygame.K_SPACE]:
angry_dina.isJump = True
pygame.mixer.music.play(1)
else:
if angry_dina.jumpVel >= -10:
neg = 1
if angry_dina.jumpVel < 0:
neg = -1
angry_dina.y -= (angry_dina.jumpVel **2 )*0.5 * neg
angry_dina.jumpVel -= 1
else:
angry_dina.isJump = False
angry_dina.jumpVel = 10
# Check Health
if health < 1:
for obstacle in obstacles:
obstacles.pop(obstacles.index(obstacle))
game_over()
for event in pygame.event.get():
if event.type == pygame.USEREVENT+2:
item = random.randrange(0,3)
obstacles.append(Obstacle(item))
if event.type == pygame.USEREVENT+2:
if FPS < 50:
FPS += 0.5
if event.type == pygame.QUIT:
score = 0
done=True
pygame.display.update()
clock.tick(FPS)
# Game Over Screen
def game_over():
global score
done = False
global health
while not done:
health = 100
screen.fill((105, 160, 209))
keys = pygame.key.get_pressed()
Messege = scoreFont.render('Your Score is : ' +(str(score)), False, (0, 0, 0))
screen.blit(Messege, (280, 100))
Messege = scoreFont.render('Press Spacebutton to Play Again', False, (0, 0, 0))
screen.blit(Messege, (200, 150))
for event in pygame.event.get():
if event.type == pygame.QUIT or keys[pygame.K_SPACE]:
angry_dina.walkedDistance = 0
done = True
done = True
pygame.display.update()
clock.tick(27)
if __name__ == '__main__':
Welcome_screen()