perfect collisions perso/map and solve #5 with collisions bullet/map

This commit is contained in:
grisel-davy 2020-04-21 00:51:51 -04:00
parent 244359fb9c
commit 3a100d925b
5 changed files with 74 additions and 38 deletions

View file

@ -14,7 +14,7 @@
viewBox="0 0 210 297" viewBox="0 0 210 297"
version="1.1" version="1.1"
id="svg8" id="svg8"
inkscape:version="0.92.4 5da689c313, 2019-01-14" inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)"
sodipodi:docname="asset.svg"> sodipodi:docname="asset.svg">
<defs <defs
id="defs2" /> id="defs2" />
@ -25,9 +25,9 @@
borderopacity="1.0" borderopacity="1.0"
inkscape:pageopacity="0.0" inkscape:pageopacity="0.0"
inkscape:pageshadow="2" inkscape:pageshadow="2"
inkscape:zoom="0.24748737" inkscape:zoom="20.055905"
inkscape:cx="-3244.5066" inkscape:cx="-1045.8998"
inkscape:cy="371.0213" inkscape:cy="197.95561"
inkscape:document-units="px" inkscape:document-units="px"
inkscape:current-layer="layer1" inkscape:current-layer="layer1"
showgrid="true" showgrid="true"
@ -73,24 +73,6 @@
y="238.79167" y="238.79167"
inkscape:export-xdpi="96" inkscape:export-xdpi="96"
inkscape:export-ydpi="96" /> inkscape:export-ydpi="96" />
<rect
style="opacity:1;fill:#0000b9;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1.22474444;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect4525"
width="7.9374938"
height="7.9375"
x="-280.45831"
y="240.11458"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96" />
<rect
style="opacity:1;fill:#ff6600;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect4529"
width="2.6458333"
height="1.3229166"
x="-277.8125"
y="238.79167"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96" />
<rect <rect
inkscape:export-ydpi="96" inkscape:export-ydpi="96"
inkscape:export-xdpi="96" inkscape:export-xdpi="96"
@ -752,5 +734,52 @@
inkscape:export-filename="/home/grizzly/Documents/Python/tangledmind/maps/map_1.png" inkscape:export-filename="/home/grizzly/Documents/Python/tangledmind/maps/map_1.png"
inkscape:export-xdpi="94.092171" inkscape:export-xdpi="94.092171"
inkscape:export-ydpi="94.092171" /> inkscape:export-ydpi="94.092171" />
<path
style="fill:none;stroke:#000000;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m -267.22916,244.08333 h -9.26042 l 9.26041,-9.26041"
id="path4613"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m -276.48958,237.46875 v 6.61458 l -7.9375,-7.9375"
id="path4615"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m -285.75,244.08333 h 9.26042 l -6.61459,6.61459"
id="path4617"
inkscape:connector-curvature="0" />
<g
id="g4628"
transform="rotate(90,-276.48959,244.08333)"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96">
<path
id="path4621"
transform="scale(0.26458333)"
d="m -1056.1582,912.51953 a 15.000079,14.999977 0 0 0 -3.8418,10 15.000079,14.999977 0 0 0 15,15 15.000079,14.999977 0 0 0 15,-15 15.000079,14.999977 0 0 0 -3.8438,-10 z"
style="opacity:1;fill:#4d4d4d;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:7.55905533;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
inkscape:connector-curvature="0" />
<rect
inkscape:export-ydpi="96"
inkscape:export-xdpi="96"
y="238.2625"
x="-277.8125"
height="3.1750042"
width="2.6458333"
id="rect4529"
style="opacity:1;fill:#ff6600;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1.54919434;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
</g>
<path
style="fill:none;stroke:#000000;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m -276.48958,252.02083 v -7.9375 l 7.9375,7.9375"
id="path4619"
inkscape:connector-curvature="0" />
<circle
style="opacity:1;fill:#4d4d4d;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:2;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="path4632"
cx="-280.15704"
cy="243.85907"
r="3.96875" />
</g> </g>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 45 KiB

BIN
asset/perso.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 819 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

After

Width:  |  Height:  |  Size: 938 B

View file

@ -23,7 +23,7 @@ BLUE=(0,0,255)
RED=(255,0,0) RED=(255,0,0)
carte = Carte('map_1.png') carte = Carte('map_1.png')
perso = Perso('Alice',660,1570,center_screen,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,'perso1_right.png','canon.png','projectile1_right.png') perso = Perso('Alice',660,1570,center_screen,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,'perso.png','canon.png','projectile1_right.png')
game = Game(carte,perso) game = Game(carte,perso)
clock = pygame.time.Clock() clock = pygame.time.Clock()

View file

@ -26,7 +26,7 @@ class Game():
# draw the projectiles and remove them if needed # draw the projectiles and remove them if needed
to_remove = [] to_remove = []
for k,proj in enumerate(self.perso.projectiles): for k,proj in enumerate(self.perso.projectiles):
if not proj.is_out(surface): if not proj.is_out(self.carte):
proj.move() proj.move()
proj.draw(surface, self.perso.get_draw_offset()) proj.draw(surface, self.perso.get_draw_offset())
else: else:
@ -108,29 +108,32 @@ class Perso():
# Update the position on x axis # Update the position on x axis
self.posx = self.posx+self.speed[0] self.posx = self.posx+self.speed[0]
# If new position cause to enter a wall, rollback to last position (i.e. don't move)
if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])): if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):
self.posx = self.posx-self.speed[0] self.posx = self.posx-self.speed[0]
self.speed[0] = 0
# Update the position on y axis # Update the position on y axis
self.posy = self.posy+self.speed[1] self.posy = self.posy+self.speed[1]
if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])): if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):
self.posy = self.posy-self.speed[1] self.posy = self.posy-self.speed[1]
self.speed[1] = 0
if keystate[self.key_fire]: if keystate[self.key_fire]:
self.fire('fireball','asset/projectile') self.fire('bullet','asset/projectile')
def draw(self,surface): def draw(self,surface):
#Calculate player rotation: #Calculate player rotation:
if self.speed != [0,0]: # if self.speed != [0,0]:
if self.speed[0]!=0: # if self.speed[0]!=0:
self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0])) # self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0]))
if self.speed[0] < 0: # if self.speed[0] < 0:
self.degres_perso = 180+self.degres_perso # self.degres_perso = 180+self.degres_perso
else: # else:
self.degres_perso = ((self.speed[1]>0)*2-1)*-90 # self.degres_perso = ((self.speed[1]>0)*2-1)*-90
self.perso = pygame.transform.rotate(self.img_perso,self.degres_perso) # self.perso = pygame.transform.rotate(self.img_perso,self.degres_perso)
#Calculate canon rotation: #Calculate canon rotation:
x_mouse, y_mouse = pygame.mouse.get_pos() x_mouse, y_mouse = pygame.mouse.get_pos()
@ -172,17 +175,21 @@ class Projectile(pygame.sprite.Sprite):
self.direction = direction = [cos(radians(angle)),-sin(radians(angle))] self.direction = direction = [cos(radians(angle)),-sin(radians(angle))]
self.img = pygame.transform.rotate(pygame.image.load(img_path+texture).convert_alpha(),angle) self.img = pygame.transform.rotate(pygame.image.load(img_path+texture).convert_alpha(),angle)
self.rect = self.img.get_rect() self.rect = self.img.get_rect()
self.mask = pygame.mask.from_surface(self.img)
def move(self): def move(self):
self.deplacement = (round(self.deplacement[0]+self.speed*self.direction[0]),round(self.deplacement[1]+self.speed*self.direction[1])) self.deplacement = (round(self.deplacement[0]+self.speed*self.direction[0]),round(self.deplacement[1]+self.speed*self.direction[1]))
def draw(self,surface,offset): def draw(self,surface,offset):
surface.blit(self.img,(offset[0]+self.pos_init[0]+self.deplacement[0]-self.rect[0],offset[1]+self.pos_init[1]+self.deplacement[1]-self.rect[1])) surface.blit(self.img,(offset[0]+self.pos_init[0]+self.deplacement[0]-self.rect[0],offset[1]+self.pos_init[1]+self.deplacement[1]-self.rect[1]))
def is_out(self,surface): def is_out(self,carte):
if (self.deplacement[0]+surface.get_width()/2<0-self.img.get_width() or abs_pos = (self.pos_init[0]+self.deplacement[0],self.pos_init[1]+self.deplacement[1])
self.deplacement[1]+surface.get_height()/2<0-self.img.get_height() or if carte.mask.overlap(self.mask, (abs_pos[0]-self.rect.center[0],abs_pos[1]-self.rect.center[1])):
self.deplacement[0]+surface.get_width()/2>surface.get_width()+self.img.get_width() or # if (self.deplacement[0]+surface.get_width()/2<0-self.img.get_width() or
self.deplacement[1]+surface.get_height()/2>surface.get_height()+self.img.get_height()): # self.deplacement[1]+surface.get_height()/2<0-self.img.get_height() or
# self.deplacement[0]+surface.get_width()/2>surface.get_width()+self.img.get_width() or
# self.deplacement[1]+surface.get_height()/2>surface.get_height()+self.img.get_height()):
return True return True
else: else:
return False return False