Netoyage et ebauche de collisions

This commit is contained in:
grisel-davy 2020-04-14 19:07:57 -04:00
parent 489d6b6446
commit bf22a5e62d
2 changed files with 47 additions and 51 deletions

11
main.py
View file

@ -5,13 +5,14 @@
import pygame import pygame
from models import Perso, Carte from models import Game, Perso, Carte
pygame.init() pygame.init()
screen_width = 1200 screen_width = 1200
screen_height = 800 screen_height = 800
center_screen = (int(screen_width/2),int(screen_height/2))
screen = pygame.display.set_mode((screen_width, screen_height)) screen = pygame.display.set_mode((screen_width, screen_height))
done = False done = False
@ -21,8 +22,9 @@ WHITE=(255,255,255)
BLUE=(0,0,255) BLUE=(0,0,255)
RED=(255,0,0) RED=(255,0,0)
perso = Perso('Alice',int(screen_width/2),int(screen_height/2),pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,'perso1_right.png','canon.png','projectile1_right.png')
carte = Carte('map_1.png') 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')
game = Game(carte,perso)
clock = pygame.time.Clock() clock = pygame.time.Clock()
@ -34,12 +36,11 @@ while not done:
keystate = pygame.key.get_pressed() + pygame.mouse.get_pressed() keystate = pygame.key.get_pressed() + pygame.mouse.get_pressed()
perso.check_keys(keystate,screen_width,screen_height) perso.check_keys(keystate,screen_width,screen_height,carte)
screen.fill(BACKGROUND) screen.fill(BACKGROUND)
carte.draw(screen,(0,0),perso) game.draw(screen)
perso.draw(screen)
pygame.display.flip() pygame.display.flip()

View file

@ -5,8 +5,6 @@
import pygame import pygame
from time import time from time import time
import numpy as np
from numpy import sqrt
from math import atan, degrees,radians, cos, sin from math import atan, degrees,radians, cos, sin
img_path = 'asset/' img_path = 'asset/'
@ -14,31 +12,50 @@ img_path = 'asset/'
cap_speed = 10 cap_speed = 10
deceleration = 1.1 deceleration = 1.1
class Game():
def __init__(self,carte,perso):
self.carte = carte
self.perso = perso
def draw(self,surface):
self.carte.draw(surface,self.perso)
self.perso.draw(surface)
class Carte(): class Carte():
def __init__(self,texture): def __init__(self,texture):
self.img = pygame.image.load('maps/'+texture).convert_alpha() self.img = pygame.image.load('maps/'+texture).convert_alpha()
self.rect = self.img.get_rect()
self.mask = pygame.mask.from_surface(self.img)
def draw(self,surface,position,perso): def draw(self,surface,perso):
surface.blit(self.img,(-perso.startx-perso.posx,-perso.starty-perso.posy)) surface.blit(self.img,(perso.posx_screen-perso.posx,perso.posy_screen-perso.posy))
class Perso(): class Perso():
def __init__(self,name,posx,posy,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj): def __init__(self,name,posx,posy,center_screen,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj):
self.name = name self.name = name
self.startx = posx
self.posx = posx self.posx = posx
self.starty = posy
self.posy = posy self.posy = posy
self.posx_screen = center_screen[0]
self.posy_screen = center_screen[1]
self.speed = [0,0] self.speed = [0,0]
self.direction = [0,-1]
self.key_up = key_up self.key_up = key_up
self.key_down = key_down self.key_down = key_down
self.key_left = key_left self.key_left = key_left
self.key_right = key_right self.key_right = key_right
self.key_fire = key_fire self.key_fire = key_fire
self.img = pygame.image.load(img_path+texture).convert_alpha() self.img = pygame.image.load(img_path+texture).convert_alpha()
self.perso_rect = self.img.get_rect()
self.mask = pygame.mask.from_surface(self.img)
print(self.mask)
self.canon = pygame.image.load(img_path+texture_canon).convert_alpha() self.canon = pygame.image.load(img_path+texture_canon).convert_alpha()
self.canon_rect = self.canon.get_rect()
self.degres_perso = 0 self.degres_perso = 0
self.degres_canon = 0 self.degres_canon = 0
self.texture_proj = texture_proj self.texture_proj = texture_proj
@ -47,7 +64,7 @@ class Perso():
self.projectiles = [] self.projectiles = []
self.last_fire = time() self.last_fire = time()
def check_keys(self,keystate,screen_width,screen_height): def check_keys(self,keystate,screen_width,screen_height,carte):
# If an interresting key is pressed # 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]: if keystate[self.key_left] or keystate[self.key_right] or keystate[self.key_up] or keystate[self.key_down]:
@ -64,18 +81,6 @@ class Perso():
if keystate[self.key_down]: if keystate[self.key_down]:
self.speed[1] = cap_speed self.speed[1] = cap_speed
if not keystate[self.key_left] and not keystate[self.key_right]:
self.direction[0] = 0
if not keystate[self.key_up] and not keystate[self.key_down]:
self.direction[1] = 0
# Cap the speed
if abs(self.speed[0])>cap_speed:
self.speed[0] = self.speed[0]/abs(self.speed[0])*cap_speed
if abs(self.speed[1])>cap_speed:
self.speed[1] = self.speed[1]/abs(self.speed[1])*cap_speed
# Begin the deceleration # Begin the deceleration
self.speed[0] = int(self.speed[0]/deceleration) self.speed[0] = int(self.speed[0]/deceleration)
self.speed[1] = int(self.speed[1]/deceleration) self.speed[1] = int(self.speed[1]/deceleration)
@ -84,17 +89,9 @@ class Perso():
self.posx = self.posx+self.speed[0] self.posx = self.posx+self.speed[0]
self.posy = self.posy+self.speed[1] self.posy = self.posy+self.speed[1]
# Select the direction based on the closest cardinals+bissectrices vectors if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):#,pygame.sprite.collide_mask):
if self.speed != [0,0]: self.posx = self.posx-self.speed[0]
direction=[] self.posy = self.posy-self.speed[1]
min_dist = 1000
for a in [-1,0,1]:
for b in [-1,0,1]:
dist = sqrt((self.speed[0]-a)**2+(self.speed[1]-b)**2)
if dist < min_dist:
min_dist = dist
direction = [a,b]
self.direction = direction
if keystate[self.key_fire]: if keystate[self.key_fire]:
@ -115,36 +112,34 @@ class Perso():
del self.projectiles[k] del self.projectiles[k]
#Calculate player rotation: #Calculate player rotation:
if self.direction[0]!=0: if self.speed[0]!=0:
self.degres_perso = -1*degrees(atan(self.direction[1]/self.direction[0])) self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0]))
if self.direction[0] < 0: if self.speed[0] < 0:
self.degres_perso = 180+self.degres_perso self.degres_perso = 180+self.degres_perso
else: else:
self.degres_perso = ((self.direction[1]>0)*2-1)*-90 self.degres_perso = ((self.speed[1]>0)*2-1)*-90
perso = pygame.transform.rotate(self.img,self.degres_perso) perso = pygame.transform.rotate(self.img,self.degres_perso)
#Calculate canon rotation: #Calculate canon rotation:
x_mouse, y_mouse = pygame.mouse.get_pos() x_mouse, y_mouse = pygame.mouse.get_pos()
if x_mouse==self.startx: if x_mouse==self.posx_screen:
x_mouse+=0.1 x_mouse+=0.1
self.degres_canon = -1*degrees(atan((y_mouse-self.starty)/(x_mouse-self.startx))) self.degres_canon = -1*degrees(atan((y_mouse-self.posy_screen)/(x_mouse-self.posx_screen)))
if x_mouse < self.startx: if x_mouse < self.posx_screen:
self.degres_canon = 180+self.degres_canon self.degres_canon = 180+self.degres_canon
canon = pygame.transform.rotate(self.canon,self.degres_canon) canon = pygame.transform.rotate(self.canon,self.degres_canon)
# Get rects # Get rects
perso_rect = self.img.get_rect() self.perso_rect = self.img.get_rect()
canon_rect = canon.get_rect() self.canon_rect = canon.get_rect()
# Blits # Blits
surface.blit(perso,(self.startx-perso_rect.center[0],self.starty-perso_rect.center[1])) surface.blit(perso,(self.posx_screen-self.perso_rect.center[0],self.posy_screen-self.perso_rect.center[1]))
surface.blit(canon,(self.startx-canon_rect.center[0],self.starty-canon_rect.center[1])) surface.blit(canon,(self.posx_screen-self.canon_rect.center[0],self.posy_screen-self.canon_rect.center[1]))
#pygame.draw.circle(surface, (200,0,0), (self.posx,self.posy), 10)
def fire(self,name,texture): def fire(self,name,texture):
if (time()-self.last_fire> 0.2): if (time()-self.last_fire> 0.2):
new_proj = Projectile(name,self.texture_proj,(self.startx,self.starty),20,self.degres_canon) new_proj = Projectile(name,self.texture_proj,(self.posx_screen,self.posy_screen),20,self.degres_canon)
self.projectiles.append(new_proj) self.projectiles.append(new_proj)
self.last_fire = time() self.last_fire = time()