Collision v2

This commit is contained in:
Marcin 2019-01-29 21:17:55 +01:00
parent d3ad2c310b
commit bdfad40968
9 changed files with 90 additions and 16 deletions

0
asd.py Normal file
View File

View File

@ -3,7 +3,7 @@ import pygame
from pygame.locals import * from pygame.locals import *
# Runtime settings # Runtime settings
WINDOW_WIDTH = 1280 WINDOW_WIDTH = 720
WINDOW_HEIGHT = 720 WINDOW_HEIGHT = 720
FPS = 30 FPS = 30
WIN_NAME = "Kostschevsky's shooter" WIN_NAME = "Kostschevsky's shooter"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 227 B

After

Width:  |  Height:  |  Size: 162 B

BIN
data/graphics/p1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 B

BIN
data/graphics/p2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 B

45
map_player_collision.txt Normal file
View File

@ -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

27
maps.py
View File

@ -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)

View File

@ -2,14 +2,14 @@
from config import * from config import *
from sprites import * from sprites import *
from colours import * from colours import *
import maps
import events import events
# import modules # import modules
from os import path
import os, sys import os, sys
import pygame import pygame
from pygame.locals import * from pygame.locals import *
# JEChANE
# initialization # initialization
pygame.init() pygame.init()
@ -20,8 +20,8 @@ clock=pygame.time.Clock()
# initialize two players # initialize two players
events.players.append(Player('asd', 400, 100, 10)) events.players.append(Player('p1', 400, 100, 10))
events.players.append(Player('qwe', 400, 500, 10)) events.players.append(Player('p2', 430, 300, 10))
events.p1_group.add(events.players[0]) events.p1_group.add(events.players[0])
events.p2_group.add(events.players[1]) events.p2_group.add(events.players[1])
all_sprites.add(events.players) all_sprites.add(events.players)

View File

@ -1,5 +1,6 @@
# import all files # import all files
from config import * from config import *
import maps
#import events #import events
# import modules # import modules
import os, sys import os, sys
@ -39,6 +40,7 @@ class Player(pygame.sprite.Sprite):
self.friction=-0.25 self.friction=-0.25
self.facing=0 # where the player is looking (0-north, 1-east, 2-south, 3-west) self.facing=0 # where the player is looking (0-north, 1-east, 2-south, 3-west)
self.colliding=False self.colliding=False
self.lastpos=self.rect.center
def moveup(self): def moveup(self):
if self.facing!=0: if self.facing!=0:
@ -71,21 +73,21 @@ class Player(pygame.sprite.Sprite):
def stopmoving(self): def stopmoving(self):
self.acc=vec(0, 0) 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): def move(self):
if self.colliding: if self.colliding or self.wallcollide():
if self.facing==0: self.rect.center=self.lastpos
self.pos.y+=1 self.pos=self.lastpos
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.speed=vec(0, 0)
self.acc=vec(0, 0) self.acc=vec(0, 0)
self.colliding=False self.colliding=False
else: else:
self.lastpos=self.rect.center
self.acc+=self.speed*self.friction self.acc+=self.speed*self.friction
self.speed+=self.acc self.speed+=self.acc
self.pos+=self.speed+0.5*self.acc self.pos+=self.speed+0.5*self.acc