Python-project/sprites.py

162 lines
4.6 KiB
Python
Raw Normal View History

2019-01-21 20:37:58 +01:00
# import all files
from config import *
2019-01-29 21:17:55 +01:00
import maps
2019-01-21 22:55:58 +01:00
#import events
2019-01-21 20:37:58 +01:00
# import modules
import os, sys
import pygame
from pygame.locals import *
# resource handler
2019-01-30 23:35:43 +01:00
screen=pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
2019-01-21 20:37:58 +01:00
def load_img(name):
2019-02-07 14:26:48 +01:00
img_path=os.path.join('data\graphics', name)
2019-01-21 20:37:58 +01:00
image=pygame.image.load(img_path).convert()
colorkey=image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
2019-01-31 09:17:12 +01:00
def load_img_noalpha(name):
2019-02-07 14:26:48 +01:00
img_path=os.path.join('data\graphics', name)
2019-01-31 09:17:12 +01:00
image=pygame.image.load(img_path).convert()
2019-01-31 19:23:25 +01:00
return image
2019-01-31 09:17:12 +01:00
2019-01-21 20:37:58 +01:00
def load_sound(name):
2019-02-07 14:26:48 +01:00
sound_path=os.path.join('data\sounds')
2019-01-21 20:37:58 +01:00
sound=pygame.mixer.Sound(sound_path)
return sound
# sprite groups
all_sprites=pygame.sprite.Group()
# sprites classes
vec=pygame.math.Vector2
class Player(pygame.sprite.Sprite):
def __init__(self, fname, s_pos_x, s_pos_y, speed):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect=load_img(fname +'.png')
2019-01-30 20:21:08 +01:00
self.rect.center=vec(s_pos_x, s_pos_y)
2019-01-21 20:37:58 +01:00
self.pos=vec(s_pos_x, s_pos_y)
self.speed=vec(0, 0)
self.acc=vec(0, 0)
self.friction=-0.25
self.facing=0 # where the player is looking (0-north, 1-east, 2-south, 3-west)
self.colliding=False
2019-01-30 20:21:08 +01:00
self.lastpos=(0,0)
self.hp=PLAYER_HP
self.alive=True
self.hp_visible=False
2019-01-22 00:38:30 +01:00
2019-01-21 20:37:58 +01:00
def moveup(self):
2019-01-21 20:08:18 +01:00
if self.facing!=0:
self.image=pygame.transform.rotate(self.image, (self.facing*90))
2019-01-21 20:37:58 +01:00
self.facing=0
self.acc.y=-1.5
self.acc.x=0
def movedown(self):
2019-01-21 20:08:18 +01:00
if self.facing!=2:
2019-01-21 21:56:43 +01:00
self.image=pygame.transform.rotate(self.image, (self.facing*90)-180)
2019-01-21 20:37:58 +01:00
self.facing=2
self.acc.y=1.5
self.acc.x=0
def moveright(self):
2019-01-21 20:08:18 +01:00
if self.facing!=1:
2019-01-21 21:56:43 +01:00
self.image=pygame.transform.rotate(self.image, (self.facing*90)-90)
2019-01-21 20:37:58 +01:00
self.facing=1
self.acc.x=1.5
self.acc.y=0
def moveleft(self):
2019-01-21 20:08:18 +01:00
if self.facing!=3:
2019-01-21 21:56:43 +01:00
self.image=pygame.transform.rotate(self.image, (self.facing*90)-270)
2019-01-21 20:37:58 +01:00
self.facing=3
self.acc.x=-1.5
self.acc.y=0
2019-01-21 21:56:43 +01:00
2019-01-21 20:37:58 +01:00
def stopmoving(self):
self.acc=vec(0, 0)
2019-01-29 21:17:55 +01:00
def wallcollide(self):
for wall in maps.walls:
if self.rect.colliderect(wall.rect):
return True
return False
2019-01-30 20:21:08 +01:00
def gothit(self):
self.hp_visible=True
if self.hp>0:
self.hp-=BULLET_DMG
2019-01-30 23:35:43 +01:00
if self.hp<=0:
2019-01-30 20:21:08 +01:00
self.alive=False
2019-01-29 21:17:55 +01:00
2019-01-21 20:37:58 +01:00
def move(self):
2019-01-29 21:17:55 +01:00
if self.colliding or self.wallcollide():
self.rect.center=self.lastpos
2019-01-30 20:21:08 +01:00
self.pos=vec(self.lastpos[0], self.lastpos[1]) ## tuple to vector
2019-01-21 20:37:58 +01:00
self.speed=vec(0, 0)
self.acc=vec(0, 0)
self.colliding=False
else:
2019-01-29 21:17:55 +01:00
self.lastpos=self.rect.center
2019-01-21 20:37:58 +01:00
self.acc+=self.speed*self.friction
self.speed+=self.acc
self.pos+=self.speed+0.5*self.acc
self.rect.center=self.pos
def update(self):
2019-01-29 21:17:55 +01:00
self.move()
2019-01-31 19:23:25 +01:00
def delete(self):
self.kill()
2019-01-21 20:37:58 +01:00
class Bullet(pygame.sprite.Sprite):
2019-01-21 22:55:58 +01:00
def __init__(self, fname, x, y, direction):
2019-01-21 20:37:58 +01:00
pygame.sprite.Sprite.__init__(self)
self.image, self.rect=load_img(fname + '.png')
2019-01-30 23:35:43 +01:00
self.rect.center=(x, y)
2019-01-21 22:55:58 +01:00
self.speed=vec(0, 0)
self.direction=direction
def shoot(self):
if self.direction==0:
self.speed.y=-BULLET_SPEED
elif self.direction==1:
2019-01-22 00:38:30 +01:00
self.speed.x=BULLET_SPEED
2019-01-21 22:55:58 +01:00
elif self.direction==2:
self.speed.y=BULLET_SPEED
elif self.direction==3:
self.speed.x=-BULLET_SPEED
self.rect.center+=self.speed
def update(self):
self.shoot()
2019-01-30 20:21:08 +01:00
if not pygame.sprite.spritecollideany(self, maps.bwalls, collided = None)==None:
2019-01-21 23:43:13 +01:00
self.kill()
2019-01-31 19:23:25 +01:00
def delete(self):
self.kill()
2019-01-31 09:17:12 +01:00
class HPBar(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.Surface((150, 30))
self.image.fill((87,156,135))
self.rect=self.image.get_rect()
self.rect.center=((x, y))
self.width=150
def gothit(self):
self.width-=int(150*(BULLET_DMG/100))
2019-01-31 19:23:25 +01:00
self.width=max([self.width, 0])
2019-01-31 09:17:12 +01:00
self.image=pygame.transform.scale(self.image, (self.width, 30))
def update(self):
2019-01-31 18:35:47 +01:00
self.gothit
def delete(self):
self.kill()