diff --git a/asset/asset.svg b/asset/asset.svg index fb2c333..0f8ca51 100644 --- a/asset/asset.svg +++ b/asset/asset.svg @@ -25,9 +25,9 @@ borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" - inkscape:zoom="1.4" - inkscape:cx="-896.36165" - inkscape:cy="178.40018" + inkscape:zoom="3.959798" + inkscape:cx="-1038.4591" + inkscape:cy="251.12477" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="true" @@ -716,5 +716,22 @@ inkscape:connector-curvature="0" inkscape:export-xdpi="96" inkscape:export-ydpi="96" /> + + diff --git a/asset/canon.png b/asset/canon.png new file mode 100644 index 0000000..312e4e6 Binary files /dev/null and b/asset/canon.png differ diff --git a/main.py b/main.py index 2039194..d9af084 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ import pygame from models import Perso + pygame.init() @@ -20,23 +21,25 @@ WHITE=(255,255,255) BLUE=(0,0,255) RED=(255,0,0) -perso1 = Perso('Alice',200,200,pygame.K_o,pygame.K_l,pygame.K_k,pygame.K_m,pygame.K_SPACE,'perso1','projectile1') -perso2 = Perso('Bjorn',600,600,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,pygame.K_SPACE,'perso2','projectile2') +perso = Perso('Alice',200,200,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,pygame.K_SPACE,'perso1','canon.png','projectile1') + -speed = 1 +clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True + keystate = pygame.key.get_pressed() - - perso1.check_keys(keystate,screen_width,screen_height,speed) - perso2.check_keys(keystate,screen_width,screen_height,speed) + perso.check_keys(keystate,screen_width,screen_height) screen.fill(BACKGROUND) - perso1.draw(screen) - perso2.draw(screen) - pygame.display.flip() \ No newline at end of file + perso.draw(screen) + pygame.display.flip() + + + clock.tick(30) + \ No newline at end of file diff --git a/models.py b/models.py index f72161a..f4dd650 100644 --- a/models.py +++ b/models.py @@ -7,6 +7,7 @@ import pygame from time import time import numpy as np from numpy import sqrt +from math import atan, degrees decision_matrix = np.array([[0,1,2],[3,4,5],[6,7,8]]) suffix_matrix = ['_up_left.png', @@ -20,12 +21,12 @@ suffix_matrix = ['_up_left.png', '_down_right.png',] img_path = 'asset/' -cap_speed = 2 +cap_speed = 10 deceleration = 1.1 class Perso(): - def __init__(self,name,posx,posy,key_up,key_down,key_left,key_right,key_fire,texture,texture_proj): + def __init__(self,name,posx,posy,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj): self.name = name self.posx = posx self.posy = posy @@ -36,23 +37,24 @@ class Perso(): self.key_left = key_left self.key_right = key_right self.key_fire = key_fire - self.imgs = [pygame.image.load(img_path+texture+'_up_left.png'), - pygame.image.load(img_path+texture+'_up.png'), - pygame.image.load(img_path+texture+'_up_right.png'), - pygame.image.load(img_path+texture+'_left.png'), - pygame.image.load(img_path+texture+'_down.png'), - pygame.image.load(img_path+texture+'_right.png'), - pygame.image.load(img_path+texture+'_down_left.png'), - pygame.image.load(img_path+texture+'_down.png'), - pygame.image.load(img_path+texture+'_down_right.png'),] + self.imgs = [pygame.image.load(img_path+texture+'_up_left.png').convert_alpha(), + pygame.image.load(img_path+texture+'_up.png').convert_alpha(), + pygame.image.load(img_path+texture+'_up_right.png').convert_alpha(), + pygame.image.load(img_path+texture+'_left.png').convert_alpha(), + pygame.image.load(img_path+texture+'_down.png').convert_alpha(), + pygame.image.load(img_path+texture+'_right.png').convert_alpha(), + pygame.image.load(img_path+texture+'_down_left.png').convert_alpha(), + pygame.image.load(img_path+texture+'_down.png').convert_alpha(), + pygame.image.load(img_path+texture+'_down_right.png').convert_alpha(),] self.img = self.imgs[0] + self.canon = pygame.image.load(img_path+texture_canon).convert_alpha() self.texture_proj = texture_proj self.size = self.img.get_size() self.projectiles = [] self.last_fire = time() - def check_keys(self,keystate,screen_width,screen_height,speed): + def check_keys(self,keystate,screen_width,screen_height): # If an interresting key is pressed if keystate[self.key_left] or keystate[self.key_right] or keystate[self.key_up] or keystate[self.key_down]: @@ -122,8 +124,25 @@ class Perso(): # select img index = decision_matrix[self.direction[1]+1,self.direction[0]+1] self.img = self.imgs[index] - surface.blit(self.img,(self.posx,self.posy)) - #print("Projectiles for {}: {}".format(self.name,len(self.projectiles))) + + #Calculate canon rotation: + x_mouse, y_mouse = pygame.mouse.get_pos() + if x_mouse==self.posx: + x_mouse+=0.1 + degres = degrees(abs(atan((y_mouse-self.posy)/(x_mouse-self.posx)))) + if y_mouse > self.posy: + degres = -degres + if x_mouse < self.posx: + degres = 180-degres + + canon = pygame.transform.rotate(self.canon,degres) + + # Get rects + perso_rect = self.img.get_rect() + canon_rect = canon.get_rect() + + surface.blit(self.img,(self.posx-perso_rect.center[0],self.posy-perso_rect.center[1])) + surface.blit(canon,(self.posx-canon_rect.center[0],self.posy-canon_rect.center[1])) def fire(self,name,texture): if (time()-self.last_fire> 0.2): @@ -142,7 +161,7 @@ class Projectile(): self.direction = direction index = decision_matrix[self.direction[1]+1,self.direction[0]+1] - self.img = pygame.image.load(img_path+texture+suffix_matrix[index]) + self.img = pygame.image.load(img_path+texture+suffix_matrix[index]).convert_alpha() def move(self): self.position = (self.position[0]+self.speed*self.direction[0],self.position[1]+self.speed*self.direction[1])