Framerate cap and rotating canon

This commit is contained in:
grisel-davy 2020-04-11 00:17:49 -04:00
parent 1dd0e781d8
commit dd103744d6
4 changed files with 66 additions and 27 deletions

View file

@ -25,9 +25,9 @@
borderopacity="1.0" borderopacity="1.0"
inkscape:pageopacity="0.0" inkscape:pageopacity="0.0"
inkscape:pageshadow="2" inkscape:pageshadow="2"
inkscape:zoom="1.4" inkscape:zoom="3.959798"
inkscape:cx="-896.36165" inkscape:cx="-1038.4591"
inkscape:cy="178.40018" inkscape:cy="251.12477"
inkscape:document-units="px" inkscape:document-units="px"
inkscape:current-layer="layer1" inkscape:current-layer="layer1"
showgrid="true" showgrid="true"
@ -716,5 +716,22 @@
inkscape:connector-curvature="0" inkscape:connector-curvature="0"
inkscape:export-xdpi="96" inkscape:export-xdpi="96"
inkscape:export-ydpi="96" /> inkscape:export-ydpi="96" />
<rect
style="opacity:1;fill:#333333;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:0.38599998;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect101"
width="2.6458335"
height="3.9687505"
x="-277.8125"
y="221.59375"
inkscape:export-xdpi="32.161598"
inkscape:export-ydpi="32.161598" />
<path
style="fill:#333333;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M -277.28333,221.59375 V 220.8 c 0,0 0.51485,0.26458 0.79375,0.26458 0.27889,0 0.79375,-0.26458 0.79375,-0.26458 v 0.79375 h -1.5875"
id="path103"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccsccc"
inkscape:export-xdpi="32.161598"
inkscape:export-ydpi="32.161598" />
</g> </g>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 42 KiB

BIN
asset/canon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 265 B

21
main.py
View file

@ -6,6 +6,7 @@
import pygame import pygame
from models import Perso from models import Perso
pygame.init() pygame.init()
@ -20,23 +21,25 @@ WHITE=(255,255,255)
BLUE=(0,0,255) BLUE=(0,0,255)
RED=(255,0,0) 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') perso = Perso('Alice',200,200,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,pygame.K_SPACE,'perso1','canon.png','projectile1')
perso2 = Perso('Bjorn',600,600,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,pygame.K_SPACE,'perso2','projectile2')
speed = 1
clock = pygame.time.Clock()
while not done: while not done:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
done = True done = True
keystate = pygame.key.get_pressed() keystate = pygame.key.get_pressed()
perso.check_keys(keystate,screen_width,screen_height)
perso1.check_keys(keystate,screen_width,screen_height,speed)
perso2.check_keys(keystate,screen_width,screen_height,speed)
screen.fill(BACKGROUND) screen.fill(BACKGROUND)
perso1.draw(screen) perso.draw(screen)
perso2.draw(screen) pygame.display.flip()
pygame.display.flip()
clock.tick(30)

View file

@ -7,6 +7,7 @@ import pygame
from time import time from time import time
import numpy as np import numpy as np
from numpy import sqrt from numpy import sqrt
from math import atan, degrees
decision_matrix = np.array([[0,1,2],[3,4,5],[6,7,8]]) decision_matrix = np.array([[0,1,2],[3,4,5],[6,7,8]])
suffix_matrix = ['_up_left.png', suffix_matrix = ['_up_left.png',
@ -20,12 +21,12 @@ suffix_matrix = ['_up_left.png',
'_down_right.png',] '_down_right.png',]
img_path = 'asset/' img_path = 'asset/'
cap_speed = 2 cap_speed = 10
deceleration = 1.1 deceleration = 1.1
class Perso(): 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.name = name
self.posx = posx self.posx = posx
self.posy = posy self.posy = posy
@ -36,23 +37,24 @@ class Perso():
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.imgs = [pygame.image.load(img_path+texture+'_up_left.png'), self.imgs = [pygame.image.load(img_path+texture+'_up_left.png').convert_alpha(),
pygame.image.load(img_path+texture+'_up.png'), pygame.image.load(img_path+texture+'_up.png').convert_alpha(),
pygame.image.load(img_path+texture+'_up_right.png'), pygame.image.load(img_path+texture+'_up_right.png').convert_alpha(),
pygame.image.load(img_path+texture+'_left.png'), pygame.image.load(img_path+texture+'_left.png').convert_alpha(),
pygame.image.load(img_path+texture+'_down.png'), pygame.image.load(img_path+texture+'_down.png').convert_alpha(),
pygame.image.load(img_path+texture+'_right.png'), pygame.image.load(img_path+texture+'_right.png').convert_alpha(),
pygame.image.load(img_path+texture+'_down_left.png'), pygame.image.load(img_path+texture+'_down_left.png').convert_alpha(),
pygame.image.load(img_path+texture+'_down.png'), pygame.image.load(img_path+texture+'_down.png').convert_alpha(),
pygame.image.load(img_path+texture+'_down_right.png'),] pygame.image.load(img_path+texture+'_down_right.png').convert_alpha(),]
self.img = self.imgs[0] self.img = self.imgs[0]
self.canon = pygame.image.load(img_path+texture_canon).convert_alpha()
self.texture_proj = texture_proj self.texture_proj = texture_proj
self.size = self.img.get_size() self.size = self.img.get_size()
self.projectiles = [] self.projectiles = []
self.last_fire = time() 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 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]:
@ -122,8 +124,25 @@ class Perso():
# select img # select img
index = decision_matrix[self.direction[1]+1,self.direction[0]+1] index = decision_matrix[self.direction[1]+1,self.direction[0]+1]
self.img = self.imgs[index] 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): def fire(self,name,texture):
if (time()-self.last_fire> 0.2): if (time()-self.last_fire> 0.2):
@ -142,7 +161,7 @@ class Projectile():
self.direction = direction self.direction = direction
index = decision_matrix[self.direction[1]+1,self.direction[0]+1] 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): def move(self):
self.position = (self.position[0]+self.speed*self.direction[0],self.position[1]+self.speed*self.direction[1]) self.position = (self.position[0]+self.speed*self.direction[0],self.position[1]+self.speed*self.direction[1])