Repository navigation
Expand file tree
/
Copy pathBullet.py
More file actions
126 lines (95 loc) · 4.09 KB
/
Copy pathBullet.py
File metadata and controls
126 lines (95 loc) · 4.09 KB
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
import pygame, sys
from pygame.locals import *
import Config as cf
from Util import *
bulletSrc = "res/Bullet/bullet.png"
class Bullet:
def __init__(self, id, mainMap, bulletPool):
self.id = id
self.mainMap = mainMap
self.bulletPool = bulletPool
self.surfaceOrigin = pygame.image.load(bulletSrc)
self.surfaceOrigin = pygame.transform.scale(self.surfaceOrigin, (cf.BULLET_WIDTH, cf.BULLET_HEIGHT))
self.pos = Point(0, 0)
self.isFree = True
# State
self.countDieTime = 0
def update(self, surface):
if self.countDieTime > cf.BULLET_TIME_TO_DIE * cf.FPS:
pos = (self.pos.x + self.surface.get_width() / 2, self.pos.y + self.surface.get_height() / 2)
self.bulletPool.hero.explode.start(pos)
self.free()
if (not self.isFree):
self.updatePosition()
self.countDieTime += 1
surface.blit(self.surface, self.pos.getValue())
def free(self):
self.isFree = True
cf.explosionSound.play()
def run(self, pos, vector, angle):
posX = pos.x - self.surfaceOrigin.get_width() / 2
posY = pos.y - self.surfaceOrigin.get_height() / 2
# self.pos = Point(posX, posY)
self.vector = normalize(vector)
self.angle = angle
self.countDieTime = 0
self.isFree = False
self.surface = pygame.transform.rotate(self.surfaceOrigin, -self.angle)
self.rect = self.surface.get_rect(center = self.surfaceOrigin.get_rect().center)
self.pos = Point(posX + self.rect.left, posY + self.rect.top)
def updatePosition(self):
oldPos = (self.pos.x + self.surface.get_width() / 2, self.pos.y + self.surface.get_width() / 2)
deltaX = self.vector.x * cf.BULLET_SPEED / cf.FPS
deltaY = self.vector.y * cf.BULLET_SPEED / cf.FPS
newPos = (oldPos[0] + deltaX, oldPos[1] + deltaY)
newDir, state, angle = self.mainMap.checkCollisionByFor(oldPos[0], oldPos[1], newPos[0], newPos[1], self.vector)
if state:
# pos = (self.pos.x + self.surface.get_width() / 2, self.pos.y + self.surface.get_height() / 2)
# self.bulletPool.hero.explode.start(pos)
self.vector = newDir
return
# self.free()
state = self.bulletPool.checkCollisionOponent(oldPos[0], oldPos[1], newPos[0], newPos[1])
if state:
pos = (self.pos.x + self.surface.get_width() / 2, self.pos.y + self.surface.get_height() / 2)
self.bulletPool.hero.explode.start(pos)
self.free()
print("Hited Oponent id: " + str(self.bulletPool.oponent.id))
self.bulletPool.oponent.die()
self.bulletPool.hero.addScore(1)
self.pos.addDelta(deltaX, deltaY)
# def draw(self, surface):
class BulletPool:
def __init__(self, amount, mainMap, hero):
self.amount = amount
self.mainMap = mainMap
self.hero = hero
self.bullets = []
for i in range(amount):
bullet = Bullet(i, self.mainMap, self)
self.bullets.append(bullet)
# run bullet from "pos" with "vector" direction
def shoot(self, pos, vector, angle):
for bullet in self.bullets:
if (bullet.isFree):
bullet.run(pos, vector, angle)
return
def update(self, surface):
for bullet in self.bullets:
if (not bullet.isFree):
bullet.update(surface)
def setOponent(self, oponent):
self.oponent = oponent
def checkCollisionOponent(self, oldX, oldY, x, y):
deltaX = (x - oldX) / cf.DETECT_SLICE
deltaY = (y - oldY) / cf.DETECT_SLICE
startX = x
startY = y
oponentPos = self.oponent.rect
for i in range(cf.DETECT_SLICE):
state = oponentPos.left <= x <= oponentPos.left + cf.HERO_WIDTH and oponentPos.top <= y <= oponentPos.top + cf.HERO_HEIGHT
if state:
return True
startX -= deltaX
startY -= deltaY
return False