From 7062491b66ed4b9fa2a84e48a2768c8f06ef60e5 Mon Sep 17 00:00:00 2001 From: Arthur 'Grizzly' Grisel-Davy Date: Sun, 29 Mar 2020 17:17:04 -0400 Subject: [PATCH] Tirer des missiles --- asset/missile.png | Bin 0 -> 355 bytes main.py | 10 +++++----- models.py | 48 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 asset/missile.png diff --git a/asset/missile.png b/asset/missile.png new file mode 100644 index 0000000000000000000000000000000000000000..7400f32c65a091378fff91b60682dd3cb4deddb3 GIT binary patch literal 355 zcmeAS@N?(olHy`uVBq!ia0vp^Ahsa~8<2e5Z*dt&u@pObhHwBu4M$1`kk47*5n0T@ zz;^_M8K-LVNdpBXOI#yLg7ec#$`gxH8OqDc^)mCai<1)zQuXqS(r3T3kpe1u;OXKR zV$u6{>OtNn1Ch4Rg`qJiKla`&G zzdkoGX z#oI?#z3F|-E|9P^J*PQT)iX)S!SJ~K5B7SI5Vgl_I_W!RyA}2c%rjM!TcdQq>vAC@ z*8*M!-GC2E!fKPsrbt|qi&(`hU2t6ZUFnG!gDcsp2RB~d9n0$f>OiXYIg6QVYuKKC wUzox0@sQ_*f}=+oX0_?D)m-R!bNdx*KfBA6&^_%IK)*70y85}Sb4q9e06>I_82|tP literal 0 HcmV?d00001 diff --git a/main.py b/main.py index 92b5016..d9d19b1 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ import pygame -from models import perso +from models import Perso pygame.init() @@ -20,8 +20,8 @@ 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,'asset/perso1') -perso2 = perso('Bjorn',600,600,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,'asset/perso2') +perso1 = Perso('Alice',200,200,pygame.K_o,pygame.K_l,pygame.K_k,pygame.K_m,pygame.K_SPACE,'asset/perso1') +perso2 = Perso('Bjorn',600,600,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,pygame.K_SPACE,'asset/perso2') speed = 1 @@ -32,8 +32,8 @@ while not done: keystate = pygame.key.get_pressed() - perso1.check_move(keystate,screen_width,screen_height,speed) - perso2.check_move(keystate,screen_width,screen_height,speed) + perso1.check_keys(keystate,screen_width,screen_height,speed) + perso2.check_keys(keystate,screen_width,screen_height,speed) screen.fill(BACKGROUND) diff --git a/models.py b/models.py index 30984ab..019a487 100644 --- a/models.py +++ b/models.py @@ -4,45 +4,85 @@ ### Author: Arthur 'Grizzly' Grisel-Davy import pygame +from time import time -class perso(): +class Perso(): - def __init__(self,name,posx,posy,key_up,key_down,key_left,key_right,texture): + def __init__(self,name,posx,posy,key_up,key_down,key_left,key_right,key_fire,texture): self.name = name self.posx = posx self.posy = posy + self.direction = (0,-1) self.key_up = key_up self.key_down = key_down self.key_left = key_left self.key_right = key_right + self.key_fire = key_fire self.img_up = pygame.image.load(texture+'_up.png') self.img_down = pygame.image.load(texture+'_down.png') self.img_right = pygame.image.load(texture+'_right.png') self.img_left = pygame.image.load(texture+'_left.png') self.img = self.img_up self.size = self.img.get_size() + self.projectiles = [] + self.last_fire = time() - def check_move(self,keystate,screen_width,screen_height,speed): + def check_keys(self,keystate,screen_width,screen_height,speed): if keystate[self.key_left]: self.posx -= speed if self.posx <0: self.posx=0 self.img = self.img_left + self.direction = (-1,0) if keystate[self.key_right]: self.posx += speed if self.posx+self.size[0] > screen_width: self.posx = screen_width-self.size[0] self.img = self.img_right + self.direction = (1,0) if keystate[self.key_up]: self.posy -= speed if self.posy < 0: self.posy = 0 self.img = self.img_up + self.direction = (0,-1) if keystate[self.key_down]: self.posy += speed if self.posy+self.size[1] > screen_height: self.posy = screen_height-self.size[1] self.img = self.img_down + self.direction = (0,1) + + if keystate[self.key_fire]: + self.fire('asset/missile') + def draw(self,surface): - surface.blit(self.img,(self.posx,self.posy)) \ No newline at end of file + surface.blit(self.img,(self.posx,self.posy)) + for proj in self.projectiles: + proj.move() + proj.draw(surface) + + def fire(self,name): + print(time()) + if (time()-self.last_fire> 0.2): + new_proj = Projectile(name,2,(self.posx,self.posy),self.direction) + self.projectiles.append(new_proj) + self.last_fire = time() + + + +class Projectile(): + + def __init__(self,name,speed,position,direction): + self.name = name + self.img = pygame.image.load(name+'.png') + self.speed = speed + self.position = position + self.direction = direction + + def move(self): + self.position = (self.position[0]+self.speed*self.direction[0],self.position[1]+self.speed*self.direction[1]) + + def draw(self,surface): + surface.blit(self.img,self.position) \ No newline at end of file