88 lines
No EOL
2.7 KiB
Python
88 lines
No EOL
2.7 KiB
Python
# -*- coding: UTF-8 -*-
|
|
|
|
### Tangled Mind
|
|
### Author: Arthur 'Grizzly' Grisel-Davy
|
|
|
|
import pygame
|
|
from time import time
|
|
|
|
class Perso():
|
|
|
|
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_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))
|
|
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) |