From bdfad409685c6169989f41d67160e257647eef74 Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 29 Jan 2019 21:17:55 +0100 Subject: [PATCH] Collision v2 --- asd.py | 0 config.py | 2 +- data/graphics/bullet.png | Bin 227 -> 162 bytes data/graphics/p1.png | Bin 0 -> 228 bytes data/graphics/p2.png | Bin 0 -> 227 bytes map_player_collision.txt | 45 +++++++++++++++++++++++++++++++++++++++ maps.py | 27 +++++++++++++++++++++++ mygame.py | 8 +++---- sprites.py | 24 +++++++++++---------- 9 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 asd.py create mode 100644 data/graphics/p1.png create mode 100644 data/graphics/p2.png create mode 100644 map_player_collision.txt diff --git a/asd.py b/asd.py new file mode 100644 index 0000000..e69de29 diff --git a/config.py b/config.py index a0d5fb3..e093e84 100644 --- a/config.py +++ b/config.py @@ -3,7 +3,7 @@ import pygame from pygame.locals import * # Runtime settings -WINDOW_WIDTH = 1280 +WINDOW_WIDTH = 720 WINDOW_HEIGHT = 720 FPS = 30 WIN_NAME = "Kostschevsky's shooter" diff --git a/data/graphics/bullet.png b/data/graphics/bullet.png index 6e6b50d33023e6f9ea7990b9c32bcdfe9a09bdd4..80b77dd331b4a52551a35e538c9f62092667f0d2 100644 GIT binary patch literal 162 zcmeAS@N?(olHy`uVBq!ia0vp^tRT$61SFYwH*Nw_oCO|{#S9GGLLkg|>2BR0pkRon zi(?4K%;Z1+|DE~IzWLw&|7SE#oL&)gAV&V+hu^POzn6|T{PZU|Y3G?EAHLr{zxchR zgoMMB`}saRXJTUhJ?hzW21o+g0e^Yw_Zu1%8)}I&FzCf8y?PsRs1RsAgQu&X%Q~lo FCIC8pKc4^q literal 227 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc1SFYWcSQjy&H|6fVg?31We{epSZZGe6l5>) z^mS!_%qS{i&iJe6+I*mpWQl7;iF1B#Zfaf$gL6@8Vo7R>LV0FMhJw4NZ$Nk>pEyua ztfz}(h{fr*QylpYC~&Y$@BRP()3+-wUfHa+yTa30lCG#c6f{2K!jthoD*lsu!Kpt9 zGYptd&Usmzq_}L8mTswCQPi}o?^{ye2BR0px`u5 z7sn8f&g4J;|JySwZb&+MN>n@Skf^Imi@QsU@Rg%30jxJ0+T1qVRDGGTU{U{kyM=|F z7mseLIjYsvHP5cL>CE1$ueaLy=ND+VGexnTcz%BV#GiZi>`9s3nkI63>gnkdeohO! zmLlDocIARk&0@EHDch_s35K&;C0s9EG6JIJ6IU0z@pCVZ)L6-LkzrP#v%wlk2?>Un Xx!UVrPR;ZOx{$%s)z4*}Q$iB}7L!^| literal 0 HcmV?d00001 diff --git a/data/graphics/p2.png b/data/graphics/p2.png new file mode 100644 index 0000000000000000000000000000000000000000..2c48ebf51a89e272f3d4b5d4043bb22fecb76f73 GIT binary patch literal 227 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jloCO|{#S9GGLLkg|>2BR0px{(b z7sn8f&g4J;|JySw78G7ypys*GJbrC{MC3w7f=O$SRIj|i)d&TCo`V@F2?-1g YQ_iYK*Yh_=0v*WU>FVdQ&MBb@02w%5MF0Q* literal 0 HcmV?d00001 diff --git a/map_player_collision.txt b/map_player_collision.txt new file mode 100644 index 0000000..e864d45 --- /dev/null +++ b/map_player_collision.txt @@ -0,0 +1,45 @@ +111111111111111111111111111111111111 +100000000001111111111111000000000001 +111111100000011111111100000000000001 +101001000000011111110000000000000001 +100000000000001110000000000000000001 +100000000000000110000000000000000001 +100000000000000110000000000100000001 +100000010000000110000000001111000001 +100000111000000110000000001111100001 +100001110000000111000000011111000001 +100011100000000000000000001111000001 +100001000000000000000000000011000001 +100000000000000000000000000011100001 +100000000000000011100000000001000001 +100000000000000001100000000000000001 +100000000000000000110000000000000001 +100000000010000000110000000000000001 +100000000111100000110000000000000001 +100000000011110000011000000000000001 +100000000000100000011000000000000001 +100000000000000000011000000000000001 +100000000000000000011100000000000001 +100000000000000000001100000000000001 +110000000000000000001111101000000001 +111000000000000000001111111100000001 +110000000000000000111111111000000001 +100000010000000001111111110000000001 +100000111000000001111111111100000001 +110000010000100011111111111110000001 +111000000001110111111111111100000001 +111110000000111111111111111100000001 +111111000000111100000011111100000001 +111111000000010000000000011000000001 +111111100100000000000000000000000001 +111111111110000000000000000000000001 +111111111111111111111111111111111111 + + + + + + + + + diff --git a/maps.py b/maps.py index e69de29..4a919f0 100644 --- a/maps.py +++ b/maps.py @@ -0,0 +1,27 @@ +from os import path +import pygame + +walls=pygame.sprite.Group() + +class Hitbox(pygame.sprite.Sprite): + def __init__(self, x, y): + self.groups=walls + pygame.sprite.Sprite.__init__(self, self.groups) + self.image=pygame.Surface((20,20)) + self.rect=self.image.get_rect() + self.x=x + self.y=y + self.rect.x=x*20 + self.rect.y=y*20 + +game_folder=path.dirname(__file__) +map_pdata=[] + +with open(path.join(game_folder, 'map_player_collision.txt')) as f: + for line in f: + map_pdata.append(line) + +for row, tiles in enumerate(map_pdata): + for col, tile in enumerate(tiles): + if tile=='1': + Hitbox(col, row) diff --git a/mygame.py b/mygame.py index ff25448..b918b04 100644 --- a/mygame.py +++ b/mygame.py @@ -2,14 +2,14 @@ from config import * from sprites import * from colours import * +import maps import events # import modules +from os import path import os, sys import pygame from pygame.locals import * -# JEChANE - # initialization pygame.init() @@ -20,8 +20,8 @@ clock=pygame.time.Clock() # initialize two players -events.players.append(Player('asd', 400, 100, 10)) -events.players.append(Player('qwe', 400, 500, 10)) +events.players.append(Player('p1', 400, 100, 10)) +events.players.append(Player('p2', 430, 300, 10)) events.p1_group.add(events.players[0]) events.p2_group.add(events.players[1]) all_sprites.add(events.players) diff --git a/sprites.py b/sprites.py index 8df3689..d1f1f9d 100644 --- a/sprites.py +++ b/sprites.py @@ -1,5 +1,6 @@ # import all files from config import * +import maps #import events # import modules import os, sys @@ -39,6 +40,7 @@ class Player(pygame.sprite.Sprite): self.friction=-0.25 self.facing=0 # where the player is looking (0-north, 1-east, 2-south, 3-west) self.colliding=False + self.lastpos=self.rect.center def moveup(self): if self.facing!=0: @@ -71,28 +73,28 @@ class Player(pygame.sprite.Sprite): def stopmoving(self): self.acc=vec(0, 0) + def wallcollide(self): + for wall in maps.walls: + if self.rect.colliderect(wall.rect): + return True + return False + 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 + if self.colliding or self.wallcollide(): + self.rect.center=self.lastpos + self.pos=self.lastpos self.speed=vec(0, 0) self.acc=vec(0, 0) self.colliding=False else: + self.lastpos=self.rect.center 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() + self.move() class Bullet(pygame.sprite.Sprite): def __init__(self, fname, x, y, direction):