Fix projectiles in new camera system

master
grisel-davy 4 years ago
parent 33822302ca
commit c7a2d52b79

@ -23,6 +23,7 @@ BLUE=(0,0,255)
RED=(255,0,0)
carte = Carte('map_1.png')
# click = 323
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')
camera = Camera(660,1570,screen_width,screen_height,0.3)
game = Game(carte,perso,camera)

@ -32,7 +32,7 @@ class Game():
for k,proj in enumerate(self.perso.projectiles):
if not proj.is_out(self.carte):
proj.move()
#proj.draw(surface,camera)
proj.draw(surface,self.camera)
else:
to_remove.append(k)
if to_remove != []:
@ -145,7 +145,6 @@ class Perso():
# perso in the box:
if camera.box.collidepoint(posx_screen,posy_screen):
print("IN THE BOX")
# X AXIS
temp_pos = self.posx
@ -176,7 +175,6 @@ class Perso():
# perso not in the box
else:
print("NOT IN THE BOX")
# X AXIS
temp_pos = self.posx
self.posx = self.posx+self.speed[0]
@ -222,7 +220,6 @@ class Perso():
self.perso_rect = self.perso.get_rect()
self.canon_rect = self.canon.get_rect()
# blit player and canon
surface.blit(self.perso,(posx_screen-self.perso_rect.center[0],posy_screen-self.perso_rect.center[1]))
surface.blit(self.canon,(posx_screen-self.canon_rect.center[0],posy_screen-self.canon_rect.center[1]))
@ -235,28 +232,34 @@ class Perso():
self.projectiles.append(new_proj)
self.last_fire = time()
class Projectile(pygame.sprite.Sprite):
def __init__(self,name,texture,position,speed,angle):
self.name = name
self.speed = speed
self.pos_init = position
self.deplacement = [0,0]
self.posx = position[0]
self.posy = position[1]
#self.deplacement = [0,0]
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]))
self.posx = round(self.posx+self.speed*self.direction[0])
self.posy = round(self.posy+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):
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 draw(self,surface,camera):
# Get position on screen
offsetx,offsety = camera.get_offset()
posx_screen = self.posx-offsetx
posy_screen = self.posy-offsety
surface.blit(self.img,(posx_screen-self.rect[0],posy_screen-self.rect[1]))
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 carte.mask.overlap(self.mask, (self.posx-self.rect.center[0],self.posy-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

Loading…
Cancel
Save