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

master
grisel-davy 4 years ago
parent 244359fb9c
commit 3a100d925b

@ -14,7 +14,7 @@
viewBox="0 0 210 297"
version="1.1"
id="svg8"
inkscape:version="0.92.4 5da689c313, 2019-01-14"
inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)"
sodipodi:docname="asset.svg">
<defs
id="defs2" />
@ -25,9 +25,9 @@
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.24748737"
inkscape:cx="-3244.5066"
inkscape:cy="371.0213"
inkscape:zoom="20.055905"
inkscape:cx="-1045.8998"
inkscape:cy="197.95561"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="true"
@ -73,24 +73,6 @@
y="238.79167"
inkscape:export-xdpi="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
inkscape:export-ydpi="96"
inkscape:export-xdpi="96"
@ -752,5 +734,52 @@
inkscape:export-filename="/home/grizzly/Documents/Python/tangledmind/maps/map_1.png"
inkscape:export-xdpi="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>
</svg>

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 45 KiB

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

@ -23,7 +23,7 @@ BLUE=(0,0,255)
RED=(255,0,0)
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)
clock = pygame.time.Clock()

@ -26,7 +26,7 @@ class Game():
# draw the projectiles and remove them if needed
to_remove = []
for k,proj in enumerate(self.perso.projectiles):
if not proj.is_out(surface):
if not proj.is_out(self.carte):
proj.move()
proj.draw(surface, self.perso.get_draw_offset())
else:
@ -108,29 +108,32 @@ class Perso():
# Update the position on x axis
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])):
self.posx = self.posx-self.speed[0]
self.speed[0] = 0
# Update the position on y axis
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])):
self.posy = self.posy-self.speed[1]
self.speed[1] = 0
if keystate[self.key_fire]:
self.fire('fireball','asset/projectile')
self.fire('bullet','asset/projectile')
def draw(self,surface):
#Calculate player rotation:
if self.speed != [0,0]:
if self.speed[0]!=0:
self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0]))
if self.speed[0] < 0:
self.degres_perso = 180+self.degres_perso
else:
self.degres_perso = ((self.speed[1]>0)*2-1)*-90
self.perso = pygame.transform.rotate(self.img_perso,self.degres_perso)
# if self.speed != [0,0]:
# if self.speed[0]!=0:
# self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0]))
# if self.speed[0] < 0:
# self.degres_perso = 180+self.degres_perso
# else:
# self.degres_perso = ((self.speed[1]>0)*2-1)*-90
# self.perso = pygame.transform.rotate(self.img_perso,self.degres_perso)
#Calculate canon rotation:
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.img = pygame.transform.rotate(pygame.image.load(img_path+texture).convert_alpha(),angle)
self.rect = self.img.get_rect()
self.mask = pygame.mask.from_surface(self.img)
def move(self):
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):
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):
if (self.deplacement[0]+surface.get_width()/2<0-self.img.get_width() or
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()):
def is_out(self,carte):
abs_pos = (self.pos_init[0]+self.deplacement[0],self.pos_init[1]+self.deplacement[1])
if carte.mask.overlap(self.mask, (abs_pos[0]-self.rect.center[0],abs_pos[1]-self.rect.center[1])):
# if (self.deplacement[0]+surface.get_width()/2<0-self.img.get_width() or
# 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
else:
return False
Loading…
Cancel
Save