-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.py
202 lines (170 loc) · 6.31 KB
/
interactive.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
import pygame
import random
from enums import Direction
from settings import KROK
from interface import ManageInterface
def move(keys, player):
if keys[pygame.K_UP]:
if keys[pygame.K_LEFT]:
player.update(-KROK, -KROK, Direction.GORALEWO)
elif keys[pygame.K_RIGHT]:
player.update(KROK, -KROK, Direction.GORAPRAWO)
else:
player.update(0, -KROK, Direction.GORA)
elif keys[pygame.K_DOWN]:
if keys[pygame.K_LEFT]:
player.update(-KROK, KROK, Direction.DOLLEWO)
elif keys[pygame.K_RIGHT]:
player.update(KROK, KROK, Direction.DOLPRAWO)
else:
player.update(0, KROK, Direction.DOL)
elif keys[pygame.K_LEFT]:
player.update(-KROK, 0, Direction.LEWO)
elif keys[pygame.K_RIGHT]:
player.update(KROK, 0, Direction.PRAWO)
elif keys[pygame.K_d]:
player.hp -= 10
def moveenemies(enemies, player, sprites):
enemies = enemies.sprites()
for enemy in enemies:
moveenemy(enemy, player, sprites)
def moveenemy(enemy, player, sprites):
krok = KROK/2
oldx = enemy.rect.x
oldy = enemy.rect.y
oldd = enemy.direction
x = player.rect.x - enemy.rect.x
y = player.rect.y - enemy.rect.y
eps = 10
if abs(abs(x)-abs(y)) < eps:
if x > 0:
if y < 0:
enemy.update(krok, -krok, Direction.GORAPRAWO)
elif y > 0:
enemy.update(krok, krok, Direction.DOLPRAWO)
else:
enemy.update(krok, 0, Direction.PRAWO)
elif x < 0:
if y < 0:
enemy.update(-krok, -krok, Direction.GORALEWO)
elif y > 0:
enemy.update(-krok, krok, Direction.DOLLEWO)
else:
enemy.update(-krok, 0, Direction.LEWO)
else: # x == 0
if y < 0:
enemy.update(0, -krok, Direction.GORA)
elif y > 0:
enemy.update(0, krok, Direction.DOL)
elif abs(x) > abs(y):
if x > 0:
enemy.update(krok, 0, Direction.PRAWO)
elif x < 0:
enemy.update(-krok, 0, Direction.LEWO)
else:
if y < 0:
enemy.update(0, -krok, Direction.GORA)
elif y > 0:
enemy.update(0, krok, Direction.DOL)
if len(pygame.sprite.spritecollide(enemy, sprites, False)) > 1:
enemy.set_position(oldx, oldy, oldd)
#movecorrection(enemy, sprites)
def movecorrection(enemy, sprites):
rd = random.choice(list(Direction))
krok = KROK//2
oldx = enemy.rect.x
oldy = enemy.rect.y
oldd = enemy.direction
if rd == Direction.GORAPRAWO:
enemy.update(krok, -krok, Direction.GORAPRAWO)
elif rd == Direction.DOLPRAWO:
enemy.update(krok, krok, Direction.DOLPRAWO)
elif rd == Direction.PRAWO:
enemy.update(krok, 0, Direction.PRAWO)
elif rd == Direction.GORALEWO:
enemy.update(-krok, -krok, Direction.GORALEWO)
elif rd == Direction.DOLLEWO:
enemy.update(-krok, krok, Direction.DOLLEWO)
elif rd == Direction.LEWO:
enemy.update(-krok, 0, Direction.LEWO)
elif rd == Direction.GORA:
enemy.update(0, -krok, Direction.GORA)
elif rd == Direction.DOL:
enemy.update(0, krok, Direction.DOL)
if len(pygame.sprite.spritecollide(enemy, sprites, False)) > 1:
enemy.set_position(oldx, oldy, oldd)
def attack(keys, player, sprites, inter):
if not keys[pygame.K_SPACE] or pygame.time.get_ticks() - inter.last_attack < 500:
return
sq = inter.get_actual_square()
if sq.ammo <= 0:
return
inter.get_actual_square().ammo -= 1
sprites.add(player.selectedgun.blast(player, player.direction))
inter.last_attack = pygame.time.get_ticks()
if sq.ammo <= 0:
sq.active = False
def InAreaToAttack(player, enemy) -> bool:
d = enemy.direction
px = player.rect.x
py = player.rect.y
ex = enemy.rect.x
ey = enemy.rect.y
if d == Direction.PRAWO and px > ex and ey <= py <= ey + 40:
return True
if d == Direction.LEWO and px < ex and ey >= py >= ey - 40:
return True
if d == Direction.GORA and py < ey and ex <= px <= ex + 40:
return True
if d == Direction.DOL and py > ey and ex >= px >= ex - 40:
return True
if d != Direction.PRAWO and d != Direction.LEWO and d != Direction.GORA and d != Direction.DOL:
t = random.randrange(1000)
return True if t < 50 else False
return False
def attackenemy(enemies, player, blasts):
enemies = enemies.sprites()
for enemy in enemies:
if InAreaToAttack(player, enemy):
enemy.attack(blasts)
def changeweapon(keys, inter: ManageInterface, player):
what = None
if keys[pygame.K_1]:
what = 0
elif keys[pygame.K_2]:
what = 1
elif keys[pygame.K_3]:
what = 2
elif keys[pygame.K_4]:
what = 3
if what is not None:
inter.select_square(what, player)
def reacttoblast(blasts, characters, inter) -> bool:
intersecdic = pygame.sprite.groupcollide(characters, blasts, False, False)
for character, blastlist in intersecdic.items():
for blast in blastlist:
if character.enemy is not blast.enemy:
if character.enemy:
if character.damage(blast.damage):
inter.addpoints(character.pointsdamage)
else:
inter.addpoints(character.points)
else:
if character.damage(blast.damage):
return True
blast.kill()
return False
def spawnweapon(weapons, activeinits):
rx = random.randrange(100, 900)
ry = random.randrange(100, 500)
init = activeinits[random.randrange(len(activeinits))]
weapons.add(init(rx, ry))
def getweapon(weapons, player, inter):
weapons = weapons.sprites()
for weapon in weapons:
if pygame.sprite.collide_rect(weapon, player):
square = inter.get_square_with_gun(weapon)
if not square.active:
square.active = True
square.ammo += weapon.plusammo
weapon.kill()