-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobjects.py
83 lines (71 loc) · 3.02 KB
/
objects.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
'''
objects.py
Provides an abstraction layer over pygames Surfaces, and in addition to
rectangles, provides other polygons with collision
'''
import pygame,math,os
from globalvalues import GlobalObjects,Collison
pygame.init()
def vadd(x,y):
return [x[0]+y[0],x[1]+y[1]]
def vsub(x,y):
return [x[0]-y[0],x[1]-y[1]]
def vdot(x,y):
return x[0]*y[0]+x[1]*y[1]
class Object:
def __init__(self, texture, ifcollides=True):
self.surface = pygame.image.load(texture)
# using a mask texture lets artists use more of their alpha channels
self.collides=ifcollides
self.mask = pygame.mask.from_surface(self.surface)
#directions should probably be done opposite the way pygame does them,
# just for the sake of our sanity
# so x >0 = right ; y >0 = up
self.moving=False
self.rfootfoward=False
self.acceleration=[0,0]
self.velocity=[0,0]
self.position = [0, 0]
self.lastdraw=pygame.time.get_ticks()
def angle(self):
return math.degrees(math.atan2(self.velocity[0],self.velocity[1]))
def draw_on(self,surface,at=(0, 0)):
self.lastdraw=pygame.time.get_ticks()
surface.blit(self.surface,at)
self.position = at
def collide(self, otherobj):
if self.collides and otherobj.collides:
"""
Test if the sprites are colliding and
resolve the collision in this case.
"""
offset = [int(x) for x in vsub(otherobj.position,self.position)]
overlap = self.mask.overlap_area(otherobj.mask,offset)
if overlap == 0:
return
"""Calculate collision normal"""
normx = (self.mask.overlap_area(otherobj.mask,(offset[0]+1,offset[1])) -
self.mask.overlap_area(otherobj.mask,(offset[0]-1,offset[1])))
normy = (self.mask.overlap_area(otherobj.mask,(offset[0],offset[1]+1)) -
self.mask.overlap_area(otherobj.mask,(offset[0],offset[1]-1)))
if normx == 0 and normy == 0:
"""One sprite is inside another"""
return
normv = [normx,normy]
diffvel = vsub(otherobj.velocity,self.velocity)
energy = vdot(diffvel,normv)/(2*vdot(normv,normv))
if energy > 0:
"""Can scale up to 2*J here to get bouncy collisions"""
energy *= 1.9
self.velocity=vadd(self.velocity,[normx*energy,normy*energy])
return
"""Separate the sprites"""
c1 = -overlap/vdot(n,n)
c2 = -c1/2
self.position=vadd(self.position,[c2*normx,c2*normy])
#class Character(Object):
Glenda=Object('assets'+os.sep+'characters'+os.sep+'Glenda.png')
Konqi=Object('assets'+os.sep+'characters'+os.sep+'Konqi.png')
Beastie=Object('assets'+os.sep+'characters'+os.sep+'Beastie.png')
Schilli=Object('assets'+os.sep+'characters'+os.sep+'Schilli.png')
GlobalObjects.playercharacters = {1:Glenda,2:Konqi,3:Beastie,4:Schilli}