Fix projectiles in new camera system
This commit is contained in:
parent
33822302ca
commit
c7a2d52b79
2 changed files with 16 additions and 12 deletions
1
main.py
1
main.py
|
@ -23,6 +23,7 @@ BLUE=(0,0,255)
|
||||||
RED=(255,0,0)
|
RED=(255,0,0)
|
||||||
|
|
||||||
carte = Carte('map_1.png')
|
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')
|
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)
|
camera = Camera(660,1570,screen_width,screen_height,0.3)
|
||||||
game = Game(carte,perso,camera)
|
game = Game(carte,perso,camera)
|
||||||
|
|
27
models.py
27
models.py
|
@ -32,7 +32,7 @@ class Game():
|
||||||
for k,proj in enumerate(self.perso.projectiles):
|
for k,proj in enumerate(self.perso.projectiles):
|
||||||
if not proj.is_out(self.carte):
|
if not proj.is_out(self.carte):
|
||||||
proj.move()
|
proj.move()
|
||||||
#proj.draw(surface,camera)
|
proj.draw(surface,self.camera)
|
||||||
else:
|
else:
|
||||||
to_remove.append(k)
|
to_remove.append(k)
|
||||||
if to_remove != []:
|
if to_remove != []:
|
||||||
|
@ -145,7 +145,6 @@ class Perso():
|
||||||
|
|
||||||
# perso in the box:
|
# perso in the box:
|
||||||
if camera.box.collidepoint(posx_screen,posy_screen):
|
if camera.box.collidepoint(posx_screen,posy_screen):
|
||||||
print("IN THE BOX")
|
|
||||||
|
|
||||||
# X AXIS
|
# X AXIS
|
||||||
temp_pos = self.posx
|
temp_pos = self.posx
|
||||||
|
@ -176,7 +175,6 @@ class Perso():
|
||||||
|
|
||||||
# perso not in the box
|
# perso not in the box
|
||||||
else:
|
else:
|
||||||
print("NOT IN THE BOX")
|
|
||||||
# X AXIS
|
# X AXIS
|
||||||
temp_pos = self.posx
|
temp_pos = self.posx
|
||||||
self.posx = self.posx+self.speed[0]
|
self.posx = self.posx+self.speed[0]
|
||||||
|
@ -222,7 +220,6 @@ class Perso():
|
||||||
self.perso_rect = self.perso.get_rect()
|
self.perso_rect = self.perso.get_rect()
|
||||||
self.canon_rect = self.canon.get_rect()
|
self.canon_rect = self.canon.get_rect()
|
||||||
|
|
||||||
|
|
||||||
# blit player and canon
|
# 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.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]))
|
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.projectiles.append(new_proj)
|
||||||
self.last_fire = time()
|
self.last_fire = time()
|
||||||
|
|
||||||
|
|
||||||
class Projectile(pygame.sprite.Sprite):
|
class Projectile(pygame.sprite.Sprite):
|
||||||
|
|
||||||
def __init__(self,name,texture,position,speed,angle):
|
def __init__(self,name,texture,position,speed,angle):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.speed = speed
|
self.speed = speed
|
||||||
self.pos_init = position
|
self.posx = position[0]
|
||||||
self.deplacement = [0,0]
|
self.posy = position[1]
|
||||||
|
#self.deplacement = [0,0]
|
||||||
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)
|
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.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):
|
def draw(self,surface,camera):
|
||||||
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]))
|
# 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):
|
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, (self.posx-self.rect.center[0],self.posy-self.rect.center[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
|
# 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[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[0]+surface.get_width()/2>surface.get_width()+self.img.get_width() or
|
||||||
|
|
Loading…
Reference in a new issue