-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
197 lines (156 loc) · 4.93 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
## Pygame library
import pygame
import objects
## Initialize pygame
pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()
pygame.font.init()
textFont = pygame.font.SysFont('Comic Sans MS',30)
WarriorWin = textFont.render('Warrior Wins',False,(0,0,0))
GoblinWin = textFont.render('Goblin Wins',False,(0,0,0))
screenWidth = 600
screenHeight = 600
## Create 500x500 window
win = pygame.display.set_mode((screenHeight,screenWidth))
clock = pygame.time.Clock()
## Caption Window
pygame.display.set_caption("Warrior vs Goblin")
hitSound = pygame.mixer.Sound("hit.wav")
projectileSound = pygame.mixer.Sound("bullet.wav")
music = pygame.mixer.music.load("music2.mp3")
pygame.mixer.music.play(-1)
## instance of warrior
warrior = objects.character(0,490,64,64,objects.walkRightWarrior,objects.walkLeftWarrior,objects.charWarrior)
goblin = objects.character(490,490,64,64,objects.walkRightGoblin,objects.walkLeftGoblin,objects.charGoblin)
def redrawWindow():
## Fill screen after movement
win.blit(objects.background,(0,0))
##Create Character and display to screen
warrior.draw(win)
goblin.draw(win)
for beam in beams:
beam.draw(win)
for spit in acid:
spit.draw(win)
if goblin.health == 0:
win.blit(WarriorWin,(0,0))
if warrior.health == 0:
win.blit(GoblinWin,(420,0))
pygame.display.update()
## Loop till close
run = True
beams = []
acid = []
while run:
##FPS set to 27
clock.tick(27)
## If quit is pressed
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
## To make sure beam is within bounds
for beam in beams:
if beam.y - beam.radius < goblin.hitbox[1] + goblin.hitbox[3] and beam.y + beam.radius > goblin.hitbox[1]:
if beam.x + beam.radius > goblin.hitbox[0] and beam.x - beam.radius < goblin.hitbox[0]:
beams.pop(beams.index(beam))
hitSound.play()
goblin.hit()
if beam.x < 600 and beam.x > 0:
beam.x += beam.vel
else:
beams.pop(beams.index(beam))
for spit in acid:
if spit.y - spit.radius < warrior.hitbox[1] + warrior.hitbox[3] and spit.y + spit.radius > warrior.hitbox[1]:
if spit.x + spit.radius > warrior.hitbox[0] and spit.x - spit.radius < warrior.hitbox[0]:
acid.pop(acid.index(spit))
hitSound.play()
warrior.hit()
if spit.x < 600 and spit.x > 0:
spit.x += spit.vel
else:
acid.pop(acid.index(spit))
##Get pressed key
keys = pygame.key.get_pressed()
if keys[pygame.K_RSHIFT]:
projectileSound.play()
if warrior.right:
facing = 1
if warrior.left:
facing = -1
if warrior.right == True or warrior.left == True:
if len(beams) < 2:
beams.append(objects.projectile(round(warrior.x + warrior.width//2),round(warrior.y + warrior.height//2),6,(255,0,0),facing))
## Move character in a ceratin direction with velocity
## Boundaries are also set for character depending on velocity width and screen
if(keys[pygame.K_LEFT]) and warrior.x > warrior.vel:
warrior.x -= warrior.vel
warrior.left = True
warrior.right = False
warrior.standing = False
elif(keys[pygame.K_RIGHT]) and warrior.x < screenWidth - warrior.width - warrior.vel:
warrior.x += warrior.vel
warrior.right = True
warrior.left = False
warrior.standing = False
else:
warrior.standing = True
warrior.walkCount = 0
if warrior.isJump == False:
if(keys[pygame.K_UP]) and warrior.y > warrior.jump:
warrior.isJump = True
warrior.right = False
warrior.left = False
warrior.walkCount = 0
else:
if warrior.jump >= -10:
jumpMultiplier = 1
if warrior.jump < 0:
jumpMultiplier = -1
warrior.y -= (warrior.jump**2)*0.5*jumpMultiplier
warrior.jump -= 1
else:
warrior.isJump = False
warrior.jump = 10
## Movement for Goblin
if keys[pygame.K_e]:
projectileSound.play()
if goblin.right:
goblinFacing = 1
if goblin.left:
goblinFacing = -1
if goblin.right == True or goblin.left == True:
if len(acid) < 2:
acid.append(objects.projectile(round(goblin.x + goblin.width//2),round(goblin.y + goblin.height//2),6,(34,139,34),goblinFacing))
## Move character in a ceratin direction with velocity
## Boundaries are also set for character depending on velocity width and screen
if(keys[pygame.K_a]) and goblin.x > goblin.vel:
goblin.x -= goblin.vel
goblin.left = True
goblin.right = False
goblin.standing = False
elif(keys[pygame.K_d]) and goblin.x < screenWidth - goblin.width - goblin.vel:
goblin.x += goblin.vel
goblin.right = True
goblin.left = False
goblin.standing = False
else:
goblin.standing = True
goblin.walkCount = 0
if goblin.isJump == False:
if(keys[pygame.K_w]) and goblin.y > goblin.jump:
goblin.isJump = True
goblin.right = False
goblin.left = False
goblin.walkCount = 0
else:
if goblin.jump >= -10:
jumpMultiplier = 1
if goblin.jump < 0:
jumpMultiplier = -1
goblin.y -= (goblin.jump**2)*0.5*jumpMultiplier
goblin.jump -= 1
else:
goblin.isJump = False
goblin.jump = 10
redrawWindow()
pygame.quit()