Initial Commit

This commit is contained in:
Marcin Kostrzewski 2019-01-21 20:37:58 +01:00
commit 668eb5be9a
15 changed files with 239 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

6
colours.py Normal file
View File

@ -0,0 +1,6 @@
# colours variables
WHITE=(245, 245, 245)
BLACK=(10, 10, 10)
RED=(245, 0, 0)
BLUE=(0, 0, 245)

26
config.py Normal file
View File

@ -0,0 +1,26 @@
# Config variables
import pygame
from pygame.locals import *
# Runtime settings
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
FPS = 30
WIN_NAME = "Kostschevsky's shooter"
# Controls
# player 1
P1_UP=pygame.K_w
P1_DOWN=pygame.K_s
P1_RIGHT=pygame.K_d
P1_LEFT=pygame.K_a
P1_SHOOT=pygame.K_z
# player 2
P2_UP=pygame.K_UP
P2_DOWN=pygame.K_DOWN
P2_RIGHT=pygame.K_RIGHT
P2_LEFT=pygame.K_LEFT
P2_SHOOT=pygame.K_RCTRL

0
data/graphics/a.txt Normal file
View File

BIN
data/graphics/asd.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 761 B

BIN
data/graphics/qwe.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

69
events.py Normal file
View File

@ -0,0 +1,69 @@
import os, sys
import pygame
from pygame.locals import *
from sprites import *
running=True
players=[]
def events():
global running
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False
def collision_check(p):
# collision between players
if players[0].rect.colliderect(players[1].rect):
if p==1:
players[0].colliding=True
direction=players[1].facing
players[0].facing=(direction+2)%4
return True
elif p==0:
players[1].colliding=True
direction=players[0].facing
players[1].facing=(direction+2)%4
return True
else:
players[0].colliding=True
players[1].colliding=True
else:
return False
def player1_input(keys):
if not (keys[P1_UP] or keys[P1_DOWN] or keys[P1_RIGHT] or keys[P1_LEFT]):
players[0].stopmoving()
elif not collision_check(0):
if keys[P1_RIGHT]:
players[0].moveright()
if keys[P1_LEFT]:
players[0].moveleft()
if keys[P1_UP]:
players[0].moveup()
if keys[P1_DOWN]:
players[0].movedown()
def player2_input(keys):
if not (keys[P2_UP] or keys[P2_DOWN] or keys[P2_RIGHT] or keys[P2_LEFT]):
players[1].stopmoving()
elif not collision_check(1):
if keys[P2_RIGHT]:
players[1].moveright()
if keys[P2_LEFT]:
players[1].moveleft()
if keys[P2_UP]:
players[1].moveup()
if keys[P2_DOWN]:
players[1].movedown()
def event_handler():
events()
keys=pygame.key.get_pressed()
collision_check(-1)
player1_input(keys)
player2_input(keys)

0
maps.py Normal file
View File

41
mygame.py Normal file
View File

@ -0,0 +1,41 @@
# import all files
from config import *
from sprites import *
from colours import *
import events
# import modules
import os, sys
import pygame
from pygame.locals import *
# initialization
pygame.init()
pygame.mixer.init()
screen=pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption(WIN_NAME)
clock=pygame.time.Clock()
# initialize two players
events.players.append(Player('asd', 50, 50, 5))
events.players.append(Player('qwe', 100, 100, 5))
all_sprites.add(events.players)
events.running=True
# game loop
while events.running:
clock.tick(FPS)
# events
pygame.event.pump()
events.event_handler()
# update
all_sprites.update()
screen.fill(BLACK)
# draw
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
sys.exit()

93
sprites.py Normal file
View File

@ -0,0 +1,93 @@
# import all files
from config import *
import events
# import modules
import os, sys
import pygame
from pygame.locals import *
# resource handler
def load_img(name):
img_path=os.path.join('data/graphics', name)
image=pygame.image.load(img_path).convert()
colorkey=image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
def load_sound(name):
sound_path=os.path.join('data\\sounds')
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')
self.rect.center=(s_pos_x, s_pos_y)
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
def moveup(self):
self.facing=0
self.acc.y=-1.5
self.acc.x=0
def movedown(self):
self.facing=2
self.acc.y=1.5
self.acc.x=0
def moveright(self):
self.facing=1
self.acc.x=1.5
self.acc.y=0
def moveleft(self):
self.facing=3
self.acc.x=-1.5
self.acc.y=0
def stopmoving(self):
self.acc=vec(0, 0)
def move(self):
if self.colliding:
if self.facing==0:
self.pos.y+=1
elif self.facing==1:
self.pos.x-=1
elif self.facing==2:
self.pos.y-=1
elif self.facing==3:
self.pos.x+=1
self.rect.center=self.pos
self.speed=vec(0, 0)
self.acc=vec(0, 0)
self.colliding=False
else:
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):
self.move()
class Bullet(pygame.sprite.Sprite):
def __init__(self, fname):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect=load_img(fname + '.png')

4
test.py Normal file
View File

@ -0,0 +1,4 @@
import os, sys
name='a'
img_path=os.path.join('data\\graphics', name+'.txt')
print(img_path)