commit 668eb5be9a640f74db1ff76fb6d646628f4d4b3a Author: Marcin Kostrzewski Date: Mon Jan 21 20:37:58 2019 +0100 Initial Commit diff --git a/__pycache__/colours.cpython-37.pyc b/__pycache__/colours.cpython-37.pyc new file mode 100644 index 0000000..ce10baa Binary files /dev/null and b/__pycache__/colours.cpython-37.pyc differ diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc new file mode 100644 index 0000000..936abe0 Binary files /dev/null and b/__pycache__/config.cpython-37.pyc differ diff --git a/__pycache__/events.cpython-37.pyc b/__pycache__/events.cpython-37.pyc new file mode 100644 index 0000000..2db02b3 Binary files /dev/null and b/__pycache__/events.cpython-37.pyc differ diff --git a/__pycache__/mygame.cpython-37.pyc b/__pycache__/mygame.cpython-37.pyc new file mode 100644 index 0000000..1e3c2e7 Binary files /dev/null and b/__pycache__/mygame.cpython-37.pyc differ diff --git a/__pycache__/sprites.cpython-37.pyc b/__pycache__/sprites.cpython-37.pyc new file mode 100644 index 0000000..435c9dc Binary files /dev/null and b/__pycache__/sprites.cpython-37.pyc differ diff --git a/colours.py b/colours.py new file mode 100644 index 0000000..8e5694c --- /dev/null +++ b/colours.py @@ -0,0 +1,6 @@ +# colours variables + +WHITE=(245, 245, 245) +BLACK=(10, 10, 10) +RED=(245, 0, 0) +BLUE=(0, 0, 245) \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..8725d97 --- /dev/null +++ b/config.py @@ -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 + diff --git a/data/graphics/a.txt b/data/graphics/a.txt new file mode 100644 index 0000000..e69de29 diff --git a/data/graphics/asd.png b/data/graphics/asd.png new file mode 100644 index 0000000..f3cb7d1 Binary files /dev/null and b/data/graphics/asd.png differ diff --git a/data/graphics/qwe.png b/data/graphics/qwe.png new file mode 100644 index 0000000..31c606d Binary files /dev/null and b/data/graphics/qwe.png differ diff --git a/events.py b/events.py new file mode 100644 index 0000000..c5bb83d --- /dev/null +++ b/events.py @@ -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) + + \ No newline at end of file diff --git a/maps.py b/maps.py new file mode 100644 index 0000000..e69de29 diff --git a/mygame.py b/mygame.py new file mode 100644 index 0000000..d67f581 --- /dev/null +++ b/mygame.py @@ -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() \ No newline at end of file diff --git a/sprites.py b/sprites.py new file mode 100644 index 0000000..cdf30a0 --- /dev/null +++ b/sprites.py @@ -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') + diff --git a/test.py b/test.py new file mode 100644 index 0000000..1fd3e41 --- /dev/null +++ b/test.py @@ -0,0 +1,4 @@ +import os, sys +name='a' +img_path=os.path.join('data\\graphics', name+'.txt') +print(img_path) \ No newline at end of file