-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDestructableObject.py
68 lines (54 loc) · 2.52 KB
/
DestructableObject.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
import turtle as tl
import pygame
import os
class DestructableObject(object):
nextId= 0
bounds = [0.0,0.0]
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (0, 0, 0)
def __init__(self,screen,image, attackDmg=1, damage=0, health=1,startPosition=(0.0,0.0),movespeed=20,name = "Destructable-"):
self.isDisposed = False
self.font = pygame.font.SysFont('Arial', 12, True, False)
self.name = name
self.title = self.font.render(self.name,True, DestructableObject.WHITE)
self.damage = damage
self.health = health
self.currentPos = startPosition
if isinstance(image, str):
self.image = pygame.image.load(image)
else: self.image =image
self.image_size = self.image.get_rect().size
self.movespeed = movespeed;
self.attackDmg = attackDmg
DestructableObject.bounds = pygame.display.get_window_size()
DestructableObject.screen = screen
self.prevPos = self.currentPos
self.offset =(0,0)
def dispose(self):
self.isDisposed = True
def checkHealth(self):
if self.health <= 0:
self.dispose()
def takeDamage(self,damage = 1):
if self.health - damage < 0:
self.health = 0
else:
self.health-=damage
self.checkHealth();
def CheckOutOfBounds(self,axis):
self.offset = self.getoffset()
switch = {
"xmax": lambda:(self.currentPos[0] > DestructableObject.bounds[0] - self.image_size[0]),
"xmin": lambda:(self.currentPos[0] < 1),
"ymax": lambda:(self.currentPos[1] > DestructableObject.bounds[1] - self.image_size[1]),
"ymin": lambda:(self.currentPos[1] < 1)
}
res =switch.get(axis, "Only xmin..max,ymax.. allowed")
return res()
def getoffset(self):
return (self.currentPos[0] - self.prevPos[0] , self.currentPos[1] - self.prevPos[1] )
def draw(self):
# DestructableObject.screen.blit(self.title,(self.currentPos[0]-13,self.currentPos[1]-15)) #print name & id -35px behind object
DestructableObject.screen.blit(self.image,self.currentPos)