GameBox 0.1.3__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {gamebox-0.1.3 → gamebox-0.3.0}/PKG-INFO +2 -2
- {gamebox-0.1.3 → gamebox-0.3.0}/README.md +1 -1
- {gamebox-0.1.3 → gamebox-0.3.0}/pyproject.toml +1 -1
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/__init__.py +1 -1
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/_game.py +2 -1
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/basics/_net.py +2 -1
- gamebox-0.3.0/src/GameBox/helpers/_collisions.py +110 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/helpers/_input.py +6 -1
- gamebox-0.3.0/src/GameBox/player/_playerPhysics.py +84 -0
- gamebox-0.3.0/src/GameBox/tilemap/_Editor.py +36 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/tilemap/_collisionDef.py +7 -0
- gamebox-0.3.0/src/GameBox/tilemap/_editorBrushes.py +166 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/tilemap/_tilemap.py +29 -10
- {gamebox-0.1.3 → gamebox-0.3.0}/tests/basicScreen.py +12 -7
- gamebox-0.3.0/tests/testMap.json +1 -0
- gamebox-0.1.3/src/GameBox/player/_playerPhysics.py +0 -192
- gamebox-0.1.3/tests/testMap.json +0 -4
- {gamebox-0.1.3 → gamebox-0.3.0}/.gitignore +0 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/LICENSE +0 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/basics/__init__.py +0 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/basics/_shapes.py +0 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/basics/cammera.py +0 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/basics/utils.py +0 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/player/_player.py +0 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/src/GameBox/player/_playerControler.py +0 -0
- {gamebox-0.1.3 → gamebox-0.3.0}/tests/levelTiles.png +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: GameBox
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: GameBox is a beginner-friendly Python package built on top of pygame, designed to make 2D game development faster and easier. It provides ready-to-use modules, utilities, and abstractions that let new developers create polished games without needing advanced coding knowledge—while still offering the flexibility for experienced coders to customize and extend.
|
|
5
5
|
Author-email: Sam Fertig <sfertig007@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -14,7 +14,7 @@ GameBox is a lightweight 2D game framework built on pygame.
|
|
|
14
14
|
This is an early technical preview of a full engine rewrite.
|
|
15
15
|
APIs may change frequently.
|
|
16
16
|
|
|
17
|
-
github:
|
|
17
|
+
github: https://github.com/sfertig/GameBox
|
|
18
18
|
|
|
19
19
|
Versioning
|
|
20
20
|
GameBox follows a modified semantic versioning scheme.
|
|
@@ -2,7 +2,7 @@ GameBox is a lightweight 2D game framework built on pygame.
|
|
|
2
2
|
This is an early technical preview of a full engine rewrite.
|
|
3
3
|
APIs may change frequently.
|
|
4
4
|
|
|
5
|
-
github:
|
|
5
|
+
github: https://github.com/sfertig/GameBox
|
|
6
6
|
|
|
7
7
|
Versioning
|
|
8
8
|
GameBox follows a modified semantic versioning scheme.
|
|
@@ -14,12 +14,13 @@ class _global_:
|
|
|
14
14
|
#--game and cam
|
|
15
15
|
self.game: object = None
|
|
16
16
|
self.cam: object = None
|
|
17
|
+
self.event: pygame.event = None
|
|
17
18
|
#-collisions
|
|
18
19
|
self.collisions: list[pygame.Rect] = []
|
|
19
20
|
|
|
20
21
|
#objects
|
|
21
22
|
self.player = self._player()
|
|
22
|
-
self.tilemap
|
|
23
|
+
self.tilemap = []
|
|
23
24
|
|
|
24
25
|
class _player:
|
|
25
26
|
def __init__(self):
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from ..basics._net import Global
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def CheckCollisions(x, y, vx, vy, dim, sample, obj):
|
|
8
|
+
x, y = x + vx, y + vy
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
#basic object collisions
|
|
12
|
+
x, y, vx, vy = _mainCollisionLogic(Global.collisions, x, y, vx, vy, dim)
|
|
13
|
+
x, y, vx, vy = _checkTilemapCollisions(x, y, vx, vy, dim, sample, obj)
|
|
14
|
+
|
|
15
|
+
return x, y, vx, vy
|
|
16
|
+
|
|
17
|
+
def _mainCollisionLogic(collisions, x, y, vx, vy, dim):
|
|
18
|
+
new_rect = pygame.Rect((x, y), dim)
|
|
19
|
+
for collision in collisions:
|
|
20
|
+
#pygame.draw.rect(Global.screen, (255, 0, 0), collision, 1)
|
|
21
|
+
if collision.colliderect(new_rect):
|
|
22
|
+
if vx > 0:
|
|
23
|
+
x = collision.left - dim[0]
|
|
24
|
+
elif vx < 0:
|
|
25
|
+
x = collision.right
|
|
26
|
+
vx = 0
|
|
27
|
+
|
|
28
|
+
# Y-axis collisions
|
|
29
|
+
new_rect = pygame.Rect((x, y), dim)
|
|
30
|
+
for collision in collisions:
|
|
31
|
+
#pygame.draw.rect(Global.screen, (255, 0, 0), collision, 1)
|
|
32
|
+
if collision.colliderect(new_rect):
|
|
33
|
+
if vy > 0: # falling
|
|
34
|
+
y = collision.top - dim[1]
|
|
35
|
+
vy = 0
|
|
36
|
+
elif vy < 0: # jumping
|
|
37
|
+
y = collision.bottom
|
|
38
|
+
vy = 0
|
|
39
|
+
|
|
40
|
+
return x, y, vx, vy
|
|
41
|
+
|
|
42
|
+
def _checkTilemapCollisions(x, y, vx, vy, dim, sample, obj):
|
|
43
|
+
if len(Global.tilemap) == 0:
|
|
44
|
+
return x, y, vx, vy
|
|
45
|
+
|
|
46
|
+
for tilemap in Global.tilemap:
|
|
47
|
+
|
|
48
|
+
#get player reletive tilemap pos
|
|
49
|
+
prect = pygame.Rect(x, y, dim[0], dim[1])
|
|
50
|
+
prx, pry = prect.center
|
|
51
|
+
if Global.cam.follow != obj:
|
|
52
|
+
if Global.cam.x != 0 or Global.cam.y != 0:
|
|
53
|
+
px = int((prx) - Global.cam.x)
|
|
54
|
+
py = int((pry) - Global.cam.y)
|
|
55
|
+
px = int(px // tilemap.tileDim[0])
|
|
56
|
+
py = int(py // tilemap.tileDim[1])
|
|
57
|
+
else:
|
|
58
|
+
px = int((x) / tilemap.tileDim[0])
|
|
59
|
+
py = int((y ) / tilemap.tileDim[1])
|
|
60
|
+
else:
|
|
61
|
+
if Global.cam.x != 0 or Global.cam.y != 0:
|
|
62
|
+
px = int(((prx + Global.cam.x)) // tilemap.tileDim[0])
|
|
63
|
+
py = int(((pry + Global.cam.y)) // tilemap.tileDim[1])
|
|
64
|
+
else:
|
|
65
|
+
px = int((x) / tilemap.tileDim[0])
|
|
66
|
+
py = int((y) / tilemap.tileDim[1])
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
#check if player is on tilemap
|
|
70
|
+
if px < 0 or px >= tilemap.mapDim[0] or py < 0 or py >= tilemap.mapDim[1]:
|
|
71
|
+
return x, y, vx, vy
|
|
72
|
+
#get collision rects around player
|
|
73
|
+
collisions: list[pygame.Rect] = []
|
|
74
|
+
for tx in range(px - sample, px + sample):
|
|
75
|
+
for ty in range(py - sample, py + sample):
|
|
76
|
+
nx = int(px + tx)
|
|
77
|
+
ny = int(py + ty)
|
|
78
|
+
#if tile is on map
|
|
79
|
+
if nx < 0 or nx >= tilemap.mapDim[0] or ny < 0 or ny >= tilemap.mapDim[1]:
|
|
80
|
+
continue
|
|
81
|
+
#if tile has defined collision shape
|
|
82
|
+
tile = str(tilemap.map[ny][nx])
|
|
83
|
+
if tile not in tilemap.collisionDict: continue
|
|
84
|
+
|
|
85
|
+
#get collision shape
|
|
86
|
+
rectshape = tilemap.collisionDict[str(tile)]
|
|
87
|
+
rect = getattr(tilemap.collisionShapes, rectshape).copy()
|
|
88
|
+
|
|
89
|
+
# Position rect correctly in the world
|
|
90
|
+
if Global.cam.follow != obj:
|
|
91
|
+
if Global.cam.x != 0 or Global.cam.y != 0:
|
|
92
|
+
rect.x = ((nx * tilemap.tileDim[0])) - Global.cam.x
|
|
93
|
+
rect.y = ((ny * tilemap.tileDim[1])) - Global.cam.y
|
|
94
|
+
else:
|
|
95
|
+
rect.x += (nx * tilemap.tileDim[0]) - Global.cam.x
|
|
96
|
+
rect.y += (ny * tilemap.tileDim[1]) - Global.cam.y
|
|
97
|
+
else:
|
|
98
|
+
if Global.cam.x != 0 or Global.cam.y != 0:
|
|
99
|
+
rect.x += ((nx * tilemap.tileDim[0])) - Global.cam.x
|
|
100
|
+
rect.y += ((ny * tilemap.tileDim[1])) - Global.cam.y
|
|
101
|
+
else:
|
|
102
|
+
rect.x += (nx * tilemap.tileDim[0]) - Global.cam.x
|
|
103
|
+
rect.y += (ny * tilemap.tileDim[1]) - Global.cam.y
|
|
104
|
+
collisions.append(rect)
|
|
105
|
+
|
|
106
|
+
#check collisions
|
|
107
|
+
x, y, vx, vy = _mainCollisionLogic(collisions, x, y, vx, vy, dim)
|
|
108
|
+
|
|
109
|
+
return x, y, vx, vy
|
|
110
|
+
|
|
@@ -51,6 +51,7 @@ class _keys:
|
|
|
51
51
|
self.escape = pygame.K_ESCAPE
|
|
52
52
|
self.space = pygame.K_SPACE
|
|
53
53
|
self.backspace = pygame.K_BACKSPACE
|
|
54
|
+
self.tab = pygame.K_TAB
|
|
54
55
|
|
|
55
56
|
#arrow keys
|
|
56
57
|
self.up = pygame.K_UP
|
|
@@ -62,7 +63,11 @@ class _keys:
|
|
|
62
63
|
self.mouse_x, self.mouse_y = 0, 0
|
|
63
64
|
def init(self): Global.game.objs.append(self)
|
|
64
65
|
|
|
65
|
-
def is_pressed(self, key):
|
|
66
|
+
def is_pressed(self, key):
|
|
67
|
+
for event in Global.event:
|
|
68
|
+
if event.type == pygame.KEYDOWN:
|
|
69
|
+
if event.key == key: return True
|
|
70
|
+
return False
|
|
66
71
|
|
|
67
72
|
def update(self): self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
|
|
68
73
|
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from ..basics._net import Global
|
|
5
|
+
from ..basics.utils import clamp, moveTward, zeroOut
|
|
6
|
+
from ..helpers._collisions import CheckCollisions
|
|
7
|
+
|
|
8
|
+
class _playerPhysics:
|
|
9
|
+
def __init__(self, player, speed: float = 1.0, gravity: float = 0.0, jump: float = 0.0, maxV: float = 10.0, airRes: float = 0.2):
|
|
10
|
+
self.player = player
|
|
11
|
+
self.rect = pygame.Rect(self.player.x, self.player.y, self.player.dim[0], self.player.dim[1])
|
|
12
|
+
|
|
13
|
+
self.speed = speed
|
|
14
|
+
self.gravity = gravity
|
|
15
|
+
self.jump = jump
|
|
16
|
+
self.player = player
|
|
17
|
+
self.maxV = maxV
|
|
18
|
+
self.airRes = airRes
|
|
19
|
+
self.onGround = False
|
|
20
|
+
self.canJump = False
|
|
21
|
+
|
|
22
|
+
self.sample = 10
|
|
23
|
+
|
|
24
|
+
self.vx, self.vy = 0, 0
|
|
25
|
+
|
|
26
|
+
def update(self):
|
|
27
|
+
self.ckeck_is_on_ground()
|
|
28
|
+
x, y, self.vx, self.vy = CheckCollisions(self.player.x, self.player.y,
|
|
29
|
+
self.vx, self.vy, self.player.dim,
|
|
30
|
+
self.sample,self.player)
|
|
31
|
+
if (Global.cam.follow) != (self.player):
|
|
32
|
+
self.player.x = x
|
|
33
|
+
self.player.y = y
|
|
34
|
+
elif (Global.cam.follow) == (self.player):
|
|
35
|
+
dx = -(self.player.x - x)
|
|
36
|
+
dy = -(self.player.y - y)
|
|
37
|
+
Global.cam.move(self.vx, self.vy)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def ckeck_is_on_ground(self):
|
|
43
|
+
self.canJump = False
|
|
44
|
+
rect = pygame.Rect(self.player.x, self.player.y + self.player.dim[1], self.player.dim[0], 10)
|
|
45
|
+
for collision in Global.collisions:
|
|
46
|
+
if collision.colliderect(rect):
|
|
47
|
+
self.canJump = True
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def move(self, x, y):
|
|
51
|
+
self.vx += x * self.speed
|
|
52
|
+
self.vy += y * self.speed
|
|
53
|
+
|
|
54
|
+
def top_down_movement(self):
|
|
55
|
+
keys = pygame.key.get_pressed()
|
|
56
|
+
if keys[pygame.K_a]:
|
|
57
|
+
self.move(-1, 0)
|
|
58
|
+
if keys[pygame.K_d]:
|
|
59
|
+
self.move(1, 0)
|
|
60
|
+
if keys[pygame.K_w]:
|
|
61
|
+
self.move(0, -1)
|
|
62
|
+
if keys[pygame.K_s]:
|
|
63
|
+
self.move(0, 1)
|
|
64
|
+
#limit velocity
|
|
65
|
+
self.vx = clamp(self.vx, -self.maxV, self.maxV)
|
|
66
|
+
self.vy = clamp(self.vy, -self.maxV, self.maxV)
|
|
67
|
+
self.vx = moveTward(self.vx, 0, self.airRes)
|
|
68
|
+
self.vy = moveTward(self.vy, 0, self.airRes)
|
|
69
|
+
self.vx = zeroOut(self.vx, 0.3)
|
|
70
|
+
self.vy = zeroOut(self.vy, 0.3)
|
|
71
|
+
|
|
72
|
+
def platforming_movement(self):
|
|
73
|
+
keys = pygame.key.get_pressed()
|
|
74
|
+
if keys[pygame.K_a]:
|
|
75
|
+
self.move(-1, 0)
|
|
76
|
+
if keys[pygame.K_d]:
|
|
77
|
+
self.move(1, 0)
|
|
78
|
+
if keys[pygame.K_w] and self.canJump:
|
|
79
|
+
self.vy -= self.jump
|
|
80
|
+
#limit velocity
|
|
81
|
+
self.vx = clamp(self.vx, -self.maxV, self.maxV)
|
|
82
|
+
self.vx = moveTward(self.vx, 0, self.airRes)
|
|
83
|
+
self.vx = zeroOut(self.vx, 0.3)
|
|
84
|
+
if self.player.gravity and not self.onGround: self.vy += self.gravity
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from..helpers._input import Keys
|
|
5
|
+
from ..basics._net import Global
|
|
6
|
+
from ._collisionDef import _tileCollisionDefs
|
|
7
|
+
|
|
8
|
+
from._editorBrushes import _brushPencil, _collisionsPencil
|
|
9
|
+
|
|
10
|
+
class _tilemapEditor:
|
|
11
|
+
def __init__(self, tilemap, activation):
|
|
12
|
+
self.tilemap = tilemap
|
|
13
|
+
self.activation = activation
|
|
14
|
+
self.active = False
|
|
15
|
+
|
|
16
|
+
self.mode = _brushPencil()
|
|
17
|
+
|
|
18
|
+
self.changes = {
|
|
19
|
+
"pencil": Keys.b,
|
|
20
|
+
"collisions": Keys.c
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
def _update(self):
|
|
25
|
+
if self.active:
|
|
26
|
+
self.change_mode()
|
|
27
|
+
#editor stuff
|
|
28
|
+
self.mode.update(self.tilemap)
|
|
29
|
+
#toggle
|
|
30
|
+
if Keys.is_pressed(self.activation): self.active = not self.active
|
|
31
|
+
|
|
32
|
+
def change_mode(self):
|
|
33
|
+
#key presses
|
|
34
|
+
if Keys.is_pressed(self.changes["pencil"]): self.mode = _brushPencil()
|
|
35
|
+
elif Keys.is_pressed(self.changes["collisions"]): self.mode = _collisionsPencil(self.tilemap)
|
|
36
|
+
|
|
@@ -3,6 +3,13 @@ import pygame
|
|
|
3
3
|
class _tileCollisionDefs:
|
|
4
4
|
def __init__(self, tileDim):
|
|
5
5
|
width, height = tileDim
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
self.rects = ["full", "none", "halfLeft", "halfRight", "halfTop", "halfBottom", "topRight",
|
|
9
|
+
"topLeft", "bottomRight", "bottomLeft", "dot"]
|
|
10
|
+
|
|
11
|
+
self.num = len(self.rects) + 1
|
|
12
|
+
|
|
6
13
|
self.full = pygame.Rect(0, 0, width, height)
|
|
7
14
|
|
|
8
15
|
self.halfLeft = pygame.Rect(0, 0, width / 2, height)
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from ..basics._net import Global
|
|
5
|
+
from ..helpers._input import Keys
|
|
6
|
+
from ._collisionDef import _tileCollisionDefs
|
|
7
|
+
|
|
8
|
+
class _brushPencil():
|
|
9
|
+
def __init__(self):
|
|
10
|
+
self.selectedTile = 1
|
|
11
|
+
self.mode = "paint"
|
|
12
|
+
|
|
13
|
+
def update(self, tilemap):
|
|
14
|
+
#get all mouse calculations
|
|
15
|
+
x, y = Keys.mouse_x, Keys.mouse_y
|
|
16
|
+
x += Global.cam.x
|
|
17
|
+
y += Global.cam.y
|
|
18
|
+
mx = x // tilemap.tileDim[0] * tilemap.tileDim[0]
|
|
19
|
+
my = y // tilemap.tileDim[1] * tilemap.tileDim[1]
|
|
20
|
+
|
|
21
|
+
#get mode
|
|
22
|
+
x, y = Keys.mouse_x, Keys.mouse_y
|
|
23
|
+
if x > tilemap.tileset.get_size()[0] * tilemap.tilescale / 2 or y > tilemap.tileset.get_size()[1] * tilemap.tilescale / 2:
|
|
24
|
+
self.mode = "paint"
|
|
25
|
+
else:
|
|
26
|
+
self.mode = "select"
|
|
27
|
+
|
|
28
|
+
#--show tileset
|
|
29
|
+
tile = tilemap.tiles[self.selectedTile]
|
|
30
|
+
image = pygame.transform.scale_by(tilemap.tileset, tilemap.tilescale / 2)
|
|
31
|
+
Global.screen.blit(image, (0, 0))
|
|
32
|
+
#--show outlined sellected tile
|
|
33
|
+
x, y = tilemap.tilePosInImage[self.selectedTile]
|
|
34
|
+
x *= tilemap.tilescale / 2
|
|
35
|
+
y *= tilemap.tilescale / 2
|
|
36
|
+
width = tilemap.orginDim[0] * tilemap.tilescale / 2
|
|
37
|
+
height = tilemap.orginDim[1] * tilemap.tilescale / 2
|
|
38
|
+
outline = pygame.Rect(x, y, width, height)
|
|
39
|
+
pygame.draw.rect(Global.screen, "white", outline, 2)
|
|
40
|
+
#other stuff
|
|
41
|
+
if self.mode == "paint":
|
|
42
|
+
x = mx
|
|
43
|
+
y = my
|
|
44
|
+
|
|
45
|
+
x -= Global.cam.x
|
|
46
|
+
y -= Global.cam.y
|
|
47
|
+
Global.screen.blit(tile, (x, y))
|
|
48
|
+
#set tile or erase
|
|
49
|
+
if pygame.mouse.get_pressed()[0]:
|
|
50
|
+
#check if mouse is on tilemap
|
|
51
|
+
x, y = mx // tilemap.tileDim[0], my // tilemap.tileDim[1]
|
|
52
|
+
if x >= 0 and x < tilemap.mapDim[0] and y >= 0 and y < tilemap.mapDim[1]:
|
|
53
|
+
tilemap.map[int(y)][int(x)] = self.selectedTile
|
|
54
|
+
elif pygame.mouse.get_pressed()[2]:
|
|
55
|
+
x, y = mx // tilemap.tileDim[0], my // tilemap.tileDim[1]
|
|
56
|
+
if x >= 0 and x < tilemap.mapDim[0] and y >= 0 and y < tilemap.mapDim[1]:
|
|
57
|
+
tilemap.map[int(y)][int(x)] = 0
|
|
58
|
+
elif self.mode == "select":
|
|
59
|
+
#paint mouse hovered tile
|
|
60
|
+
x, y = Keys.mouse_x, Keys.mouse_y
|
|
61
|
+
x = (x // width)
|
|
62
|
+
y = (y // height)
|
|
63
|
+
outline = pygame.Rect(x * width, y * width, width, height)
|
|
64
|
+
pygame.draw.rect(Global.screen, "black", outline, 2)
|
|
65
|
+
if pygame.mouse.get_pressed()[0]:
|
|
66
|
+
x *= tilemap.orginDim[0]
|
|
67
|
+
y *= tilemap.orginDim[1]
|
|
68
|
+
self.selectedTile = tilemap.posToTile[(int(x), int(y))]
|
|
69
|
+
|
|
70
|
+
#move selection by arrow keys
|
|
71
|
+
x, y = tilemap.tilePosInImage[self.selectedTile]
|
|
72
|
+
|
|
73
|
+
width = 16
|
|
74
|
+
height = 16
|
|
75
|
+
|
|
76
|
+
if Keys.is_pressed(Keys.left): x -= width
|
|
77
|
+
if Keys.is_pressed(Keys.right): x += width
|
|
78
|
+
if Keys.is_pressed(Keys.up): y -= height
|
|
79
|
+
if Keys.is_pressed(Keys.down): y += height
|
|
80
|
+
|
|
81
|
+
if (int(x), int(y)) in tilemap.posToTile:
|
|
82
|
+
self.selectedTile = tilemap.posToTile[(int(x), int(y))]
|
|
83
|
+
|
|
84
|
+
class _collisionsPencil():
|
|
85
|
+
def __init__(self, tilemap):
|
|
86
|
+
self.selectedTile = 0
|
|
87
|
+
self.shapes = tilemap.collisionShapes.num
|
|
88
|
+
self.mode = "paint"
|
|
89
|
+
size = tilemap.tilescale / 2
|
|
90
|
+
self.size = tilemap.orginDim[0] * size
|
|
91
|
+
self.coll = _tileCollisionDefs((tilemap.orginDim[0] * size, tilemap.orginDim[1] * size))
|
|
92
|
+
|
|
93
|
+
def update(self, tilemap):
|
|
94
|
+
x, y = Keys.mouse_x, Keys.mouse_y
|
|
95
|
+
#draw tilesets and background
|
|
96
|
+
Global.screen.fill("darkgrey")
|
|
97
|
+
image = pygame.transform.scale_by(tilemap.tileset, tilemap.tilescale / 2)
|
|
98
|
+
Global.screen.blit(image, (0, 0))
|
|
99
|
+
Global.screen.blit(image, (0, image.get_size()[1]+ 10))
|
|
100
|
+
#draw all collision rects on top tileset
|
|
101
|
+
for tile in range(tilemap.tilesetNum - 1):
|
|
102
|
+
tile+=1
|
|
103
|
+
if str(tile) not in tilemap.collisionDict: continue
|
|
104
|
+
rectshape = tilemap.collisionDict[str(tile)]
|
|
105
|
+
rect = getattr(self.coll, rectshape).copy()
|
|
106
|
+
tx, ty = tilemap.tilePosInImage[int(tile)]
|
|
107
|
+
tx = (tx // tilemap.orginDim[0]) * self.size
|
|
108
|
+
ty = (ty // tilemap.orginDim[1]) * self.size
|
|
109
|
+
rect.x += tx
|
|
110
|
+
rect.y += ty
|
|
111
|
+
pygame.draw.rect(Global.screen, "yellow", rect, 5)
|
|
112
|
+
outline = pygame.Rect(tx, ty, self.size, self.size)
|
|
113
|
+
pygame.draw.rect(Global.screen, "gray", outline, 2)
|
|
114
|
+
#change mode
|
|
115
|
+
if y > image.get_size()[1] * 2 + 50: self.mode = "select"
|
|
116
|
+
else: self.mode = "paint"
|
|
117
|
+
#draw collisions tiles at bottom
|
|
118
|
+
tx, ty = 0, image.get_size()[1] * 2 + 50
|
|
119
|
+
for tile in range(self.shapes-1):
|
|
120
|
+
rect = getattr(self.coll, tilemap.collisionShapes.rects[tile]).copy()
|
|
121
|
+
rect.x += tx
|
|
122
|
+
rect.y += ty
|
|
123
|
+
pygame.draw.rect(Global.screen, "yellow", rect, 5)
|
|
124
|
+
outline = pygame.Rect(tx, ty, self.size, self.size)
|
|
125
|
+
pygame.draw.rect(Global.screen, "gray", outline, 2)
|
|
126
|
+
tx += self.size + self.size / 2
|
|
127
|
+
#paint mode
|
|
128
|
+
if self.mode == "paint":
|
|
129
|
+
#draw tile around mouse
|
|
130
|
+
rect = getattr(self.coll, tilemap.collisionShapes.rects[self.selectedTile]).copy()
|
|
131
|
+
rect.x += x - self.size / 2
|
|
132
|
+
rect.y += y - self.size / 2
|
|
133
|
+
pygame.draw.rect(Global.screen, "yellow", rect, 5)
|
|
134
|
+
outline = pygame.Rect(x, y, self.size, self.size)
|
|
135
|
+
outline.center = (Keys.mouse_x, Keys.mouse_y)
|
|
136
|
+
pygame.draw.rect(Global.screen, "gray", outline, 2)
|
|
137
|
+
#paint collisions
|
|
138
|
+
size = tilemap.orginDim[0]
|
|
139
|
+
if pygame.mouse.get_pressed()[0]:
|
|
140
|
+
x = (x - (x % self.size)) // self.size
|
|
141
|
+
y = (y - (y % self.size)) // self.size
|
|
142
|
+
px = x * size
|
|
143
|
+
py = y * size
|
|
144
|
+
if (int(px), int(py)) in tilemap.posToTile:
|
|
145
|
+
tilemap.collisionDict[str(tilemap.posToTile[(int(px), int(py))])] = self.coll.rects[self.selectedTile]
|
|
146
|
+
#erase collisions
|
|
147
|
+
if pygame.mouse.get_pressed()[2]:
|
|
148
|
+
x = (x - (x % self.size)) // self.size
|
|
149
|
+
y = (y - (y % self.size)) // self.size
|
|
150
|
+
px = x * size
|
|
151
|
+
py = y * size
|
|
152
|
+
if (int(px), int(py)) in tilemap.posToTile:
|
|
153
|
+
tilemap.collisionDict[tilemap.posToTile[(int(px), int(py))]] = "none"
|
|
154
|
+
|
|
155
|
+
elif self.mode == "select":
|
|
156
|
+
#get tile at mouse
|
|
157
|
+
x = int(x // (self.size + self.size / 2))
|
|
158
|
+
#draw underline
|
|
159
|
+
underline = pygame.Rect((0, 0), (self.size, 5))
|
|
160
|
+
underline.center = (Keys.mouse_x, ty + self.size+5)
|
|
161
|
+
pygame.draw.rect(Global.screen, "black", underline)
|
|
162
|
+
#select tile
|
|
163
|
+
if pygame.mouse.get_pressed()[0] and x < self.shapes-1 and x >= 0:
|
|
164
|
+
self.selectedTile = int(x)
|
|
165
|
+
|
|
166
|
+
|
|
@@ -5,33 +5,40 @@ import json
|
|
|
5
5
|
from ..basics._net import Global
|
|
6
6
|
|
|
7
7
|
from ._collisionDef import _tileCollisionDefs
|
|
8
|
+
from ._Editor import _tilemapEditor
|
|
8
9
|
|
|
9
10
|
class TileMap:
|
|
10
|
-
def __init__(self, tileSet: str, tileDim: tuple, tileScale: float, mapDim: tuple, mapFill: int,
|
|
11
|
+
def __init__(self, tileSet: str, tileDim: tuple, tileScale: float, mapDim: tuple, mapFill: int, saveFile = None):
|
|
11
12
|
self.tilesetFile = tileSet
|
|
12
|
-
self.mapFile =
|
|
13
|
+
self.mapFile = saveFile
|
|
13
14
|
self.tileDim = (tileDim[0] * tileScale, tileDim[1] * tileScale)
|
|
14
15
|
self.tileDim = (self.tileDim[0] * Global.cam.scale, self.tileDim[1] * Global.cam.scale)
|
|
15
16
|
self.mapDim = mapDim
|
|
16
|
-
self.offset = offset
|
|
17
17
|
self.tilescale = tileScale
|
|
18
18
|
self.orginDim = tileDim
|
|
19
19
|
|
|
20
|
+
self.tilesetNum = 0
|
|
21
|
+
|
|
22
|
+
self.editor = None
|
|
23
|
+
|
|
20
24
|
Global.game.objs.append(self)
|
|
21
|
-
Global.tilemap = self
|
|
22
25
|
|
|
23
26
|
self.collisionShapes = _tileCollisionDefs(self.tileDim)
|
|
24
27
|
|
|
25
28
|
self.collisionDict = {}
|
|
26
29
|
|
|
30
|
+
self.tilePosInImage = {}
|
|
31
|
+
self.posToTile = {}
|
|
32
|
+
|
|
27
33
|
#map, tile splitting, ect
|
|
28
34
|
#--create map
|
|
29
35
|
self.map = np.full(self.mapDim, mapFill)
|
|
30
36
|
#--split map into tiles
|
|
31
37
|
self.tiles = {}
|
|
32
38
|
tileset = pygame.image.load(tileSet).convert_alpha()
|
|
39
|
+
self.tileset = tileset
|
|
33
40
|
tile_w, tile_h = tileDim
|
|
34
|
-
tile_id =
|
|
41
|
+
tile_id = 1
|
|
35
42
|
tileset_w, tileset_h = tileset.get_size()
|
|
36
43
|
|
|
37
44
|
for y in range(0, tileset_h, tile_h):
|
|
@@ -39,10 +46,12 @@ class TileMap:
|
|
|
39
46
|
tile = pygame.Surface(tileDim, pygame.SRCALPHA)
|
|
40
47
|
tile.blit(tileset, (0, 0), (x, y, tile_w, tile_h))
|
|
41
48
|
self.tiles[tile_id] = pygame.transform.scale(tile, self.tileDim)
|
|
49
|
+
self.tilePosInImage[tile_id] = (x, y)
|
|
50
|
+
self.posToTile[(x, y)] = tile_id
|
|
51
|
+
self.tilesetNum += 1
|
|
42
52
|
tile_id += 1
|
|
43
53
|
|
|
44
|
-
|
|
45
|
-
Global.tilmap = self
|
|
54
|
+
Global.tilemap.append(self)
|
|
46
55
|
|
|
47
56
|
def load_map_from_json(self, filePath: str):
|
|
48
57
|
with open(filePath, "r") as f:
|
|
@@ -54,16 +63,23 @@ class TileMap:
|
|
|
54
63
|
self.mapFile = path
|
|
55
64
|
self.collisionDict = data["collisions"]
|
|
56
65
|
|
|
66
|
+
def activate_editor(self, activation):
|
|
67
|
+
print(f"editor activated. press {activation} to toggle")
|
|
68
|
+
self.editor = _tilemapEditor(self, activation)
|
|
69
|
+
|
|
57
70
|
def update(self):
|
|
58
71
|
self.draw_tiles()
|
|
72
|
+
if self.editor is not None: self.editor._update()
|
|
59
73
|
|
|
60
74
|
|
|
61
75
|
def draw_tiles(self):
|
|
62
76
|
for y in range(self.mapDim[1]):
|
|
63
77
|
for x in range(self.mapDim[0]):
|
|
78
|
+
if self.map[y][x] == 0:
|
|
79
|
+
continue
|
|
64
80
|
tile = self.tiles[self.map[y][x]]
|
|
65
|
-
mx = (x * self.tileDim[0]
|
|
66
|
-
my = (y * self.tileDim[1]
|
|
81
|
+
mx = (x * self.tileDim[0]) - Global.cam.x
|
|
82
|
+
my = (y * self.tileDim[1]) - Global.cam.y
|
|
67
83
|
if mx < -self.tileDim[0] or mx > Global.screenDim[0] or my < -self.tileDim[1] or my > Global.screenDim[1]: continue
|
|
68
84
|
Global.screen.blit(tile, (mx, my))
|
|
69
85
|
|
|
@@ -72,4 +88,7 @@ class TileMap:
|
|
|
72
88
|
if self.mapFile is not None:
|
|
73
89
|
print('tilemap saved')
|
|
74
90
|
with open(self.mapFile, "w") as f:
|
|
75
|
-
|
|
91
|
+
data = {}
|
|
92
|
+
data["map"] = self.map.tolist()
|
|
93
|
+
data["collisions"] = self.collisionDict
|
|
94
|
+
json.dump(data, f)
|
|
@@ -2,7 +2,7 @@ from src.GameBox import *
|
|
|
2
2
|
import pygame
|
|
3
3
|
import os
|
|
4
4
|
|
|
5
|
-
width, height =
|
|
5
|
+
width, height = 1400, 900
|
|
6
6
|
game = Game(width, height, "blue", "First Game!")
|
|
7
7
|
win = game.get_screen()
|
|
8
8
|
|
|
@@ -10,11 +10,14 @@ Keys.init()
|
|
|
10
10
|
|
|
11
11
|
cam = Cammera()
|
|
12
12
|
|
|
13
|
-
player = Player((width / 2, height / 4), (50, 50), "green", False)
|
|
13
|
+
player = Player((width / 2, height / 4), (50, 50), "green", False)
|
|
14
14
|
player.add_physics(1.0, 3.0, 16, 7.0, 0.5)
|
|
15
15
|
|
|
16
|
-
map = TileMap("tests/levelTiles.png", (16, 16), 5, (25, 25),
|
|
16
|
+
map = TileMap("tests/levelTiles.png", (16, 16), 5, (25, 25), 0)
|
|
17
17
|
map.load_map_from_json("tests/testMap.json")
|
|
18
|
+
map.activate_editor(Keys.tab)
|
|
19
|
+
|
|
20
|
+
|
|
18
21
|
|
|
19
22
|
player.set_tilemap_sample(50)
|
|
20
23
|
|
|
@@ -23,14 +26,16 @@ cam.set_follow_target(player)
|
|
|
23
26
|
|
|
24
27
|
running = True
|
|
25
28
|
while running:
|
|
26
|
-
|
|
29
|
+
events = pygame.event.get()
|
|
30
|
+
for event in events:
|
|
27
31
|
if event.type == pygame.QUIT:
|
|
28
32
|
running = False
|
|
29
33
|
|
|
30
34
|
player.top_down_movement()
|
|
31
35
|
|
|
32
|
-
game.update(60)
|
|
36
|
+
game.update(events, 60)
|
|
37
|
+
|
|
33
38
|
|
|
34
|
-
|
|
35
|
-
#game.quit()
|
|
39
|
+
game.quit()
|
|
36
40
|
pygame.quit()
|
|
41
|
+
os.system("cls")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"map": [[11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 1, 2, 3, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 7, 8, 9, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 13, 14, 15, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 4, 5, 6, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 10, 11, 12, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 16, 17, 18, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]], "collisions": {"1": "full", "2": "halfTop", "7": "halfLeft", "3": "full", "13": "full", "15": "full", "9": "halfRight", "14": "halfBottom", "8": "none", "6": "bottomLeft", "5": "halfBottom", "4": "bottomRight", "10": "halfRight", "16": "topRight", "12": "halfLeft", "11": "none", "18": "topLeft", "17": "halfTop"}}
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import pygame
|
|
2
|
-
import numpy as np
|
|
3
|
-
|
|
4
|
-
from ..basics._net import Global
|
|
5
|
-
from ..basics.utils import clamp, moveTward, zeroOut
|
|
6
|
-
|
|
7
|
-
class _playerPhysics:
|
|
8
|
-
def __init__(self, player, speed: float = 1.0, gravity: float = 0.0, jump: float = 0.0, maxV: float = 10.0, airRes: float = 0.2):
|
|
9
|
-
self.player = player
|
|
10
|
-
self.rect = pygame.Rect(self.player.x, self.player.y, self.player.dim[0], self.player.dim[1])
|
|
11
|
-
|
|
12
|
-
self.speed = speed
|
|
13
|
-
self.gravity = gravity
|
|
14
|
-
self.jump = jump
|
|
15
|
-
self.player = player
|
|
16
|
-
self.maxV = maxV
|
|
17
|
-
self.airRes = airRes
|
|
18
|
-
self.onGround = False
|
|
19
|
-
self.canJump = False
|
|
20
|
-
|
|
21
|
-
self.sample = 10
|
|
22
|
-
|
|
23
|
-
self.vx, self.vy = 0, 0
|
|
24
|
-
|
|
25
|
-
def update(self):
|
|
26
|
-
self.ckeck_is_on_ground()
|
|
27
|
-
x, y = self.check_collisions()
|
|
28
|
-
if (Global.cam.follow) != (self.player):
|
|
29
|
-
self.player.x = x
|
|
30
|
-
self.player.y = y
|
|
31
|
-
elif (Global.cam.follow) == (self.player):
|
|
32
|
-
dx = -(self.player.x - x)
|
|
33
|
-
dy = -(self.player.y - y)
|
|
34
|
-
Global.cam.move(self.vx, self.vy)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def check_collisions(self):
|
|
38
|
-
x, y = self.player.x + self.vx, self.player.y + self.vy
|
|
39
|
-
|
|
40
|
-
#basic object collisions
|
|
41
|
-
x, y = self.collisionLogic(Global.collisions, x, y)
|
|
42
|
-
x, y = self.tilemap_collisions(x, y)
|
|
43
|
-
|
|
44
|
-
return x, y
|
|
45
|
-
|
|
46
|
-
def collisionLogic(self, collisions, x, y):
|
|
47
|
-
new_rect = pygame.Rect((x, self.player.y), self.player.dim)
|
|
48
|
-
for collision in collisions:
|
|
49
|
-
#pygame.draw.rect(Global.screen, (255, 0, 0), collision, 1)
|
|
50
|
-
if collision.colliderect(new_rect):
|
|
51
|
-
if self.vx > 0:
|
|
52
|
-
x = collision.left - self.player.dim[0]
|
|
53
|
-
elif self.vx < 0:
|
|
54
|
-
x = collision.right
|
|
55
|
-
self.vx = 0
|
|
56
|
-
|
|
57
|
-
# Y-axis collisions
|
|
58
|
-
new_rect = pygame.Rect((x, y), self.player.dim)
|
|
59
|
-
for collision in collisions:
|
|
60
|
-
#pygame.draw.rect(Global.screen, (255, 0, 0), collision, 1)
|
|
61
|
-
if collision.colliderect(new_rect):
|
|
62
|
-
if self.vy > 0: # falling
|
|
63
|
-
y = collision.top - self.player.dim[1]
|
|
64
|
-
self.vy = 0
|
|
65
|
-
elif self.vy < 0: # jumping
|
|
66
|
-
y = collision.bottom
|
|
67
|
-
self.vy = 0
|
|
68
|
-
|
|
69
|
-
return x, y
|
|
70
|
-
|
|
71
|
-
def tilemap_collisions(self, x, y):
|
|
72
|
-
sample = self.sample
|
|
73
|
-
if Global.tilemap is None:
|
|
74
|
-
return x, y
|
|
75
|
-
|
|
76
|
-
#get player reletive tilemap pos
|
|
77
|
-
prect = pygame.Rect(x, y, self.player.dim[0], self.player.dim[1])
|
|
78
|
-
prx, pry = prect.center
|
|
79
|
-
if Global.cam.follow != self.player:
|
|
80
|
-
if Global.cam.x != 0 or Global.cam.y != 0:
|
|
81
|
-
px = int((prx - Global.tilemap.offset[0]) - Global.cam.x)
|
|
82
|
-
py = int((pry - Global.tilemap.offset[1]) - Global.cam.y)
|
|
83
|
-
px = int(px // Global.tilemap.tileDim[0])
|
|
84
|
-
py = int(py // Global.tilemap.tileDim[1])
|
|
85
|
-
else:
|
|
86
|
-
px = int((x - Global.tilemap.offset[0]) / Global.tilemap.tileDim[0])
|
|
87
|
-
py = int((y - Global.tilemap.offset[1]) / Global.tilemap.tileDim[1])
|
|
88
|
-
else:
|
|
89
|
-
if Global.cam.x != 0 or Global.cam.y != 0:
|
|
90
|
-
px = int(((prx + Global.cam.x)-Global.tilemap.offset[0]) // Global.tilemap.tileDim[0])
|
|
91
|
-
py = int(((pry + Global.cam.y)-Global.tilemap.offset[1]) // Global.tilemap.tileDim[1])
|
|
92
|
-
else:
|
|
93
|
-
px = int((x - Global.tilemap.offset[0]) / Global.tilemap.tileDim[0])
|
|
94
|
-
py = int((y - Global.tilemap.offset[1]) / Global.tilemap.tileDim[1])
|
|
95
|
-
|
|
96
|
-
#draw collision rect
|
|
97
|
-
#print(f"px: {px}, py: {py} | cam.x: {Global.cam.x}, cam.y: {Global.cam.y} | x: {x}, y: {y}")
|
|
98
|
-
#rect = pygame.Rect(px , py, self.player.dim[0], self.player.dim[1])
|
|
99
|
-
#pygame.draw.rect(Global.screen, "red", rect)
|
|
100
|
-
#rect.x *= Global.tilemap.tileDim[0]
|
|
101
|
-
#rect.y *= Global.tilemap.tileDim[1]
|
|
102
|
-
#pygame.draw.rect(Global.screen, "yellow", rect, 1)
|
|
103
|
-
#pygame.display.update(rect)
|
|
104
|
-
|
|
105
|
-
#check if player is on tilemap
|
|
106
|
-
if px < 0 or px >= Global.tilemap.mapDim[0] or py < 0 or py >= Global.tilemap.mapDim[1]:
|
|
107
|
-
return x, y
|
|
108
|
-
#get collision rects around player
|
|
109
|
-
collisions: list[pygame.Rect] = []
|
|
110
|
-
for tx in range(px - sample, px + sample):
|
|
111
|
-
for ty in range(py - sample, py + sample):
|
|
112
|
-
nx = int(px + tx)
|
|
113
|
-
ny = int(py + ty)
|
|
114
|
-
#if tile is on map
|
|
115
|
-
if nx < 0 or nx >= Global.tilemap.mapDim[0] or ny < 0 or ny >= Global.tilemap.mapDim[1]:
|
|
116
|
-
continue
|
|
117
|
-
#if tile has defined collision shape
|
|
118
|
-
tile = str(Global.tilemap.map[ny][nx])
|
|
119
|
-
if tile not in Global.tilemap.collisionDict: continue
|
|
120
|
-
|
|
121
|
-
#get collision shape
|
|
122
|
-
rectshape = Global.tilemap.collisionDict[str(tile)]
|
|
123
|
-
rect = getattr(Global.tilemap.collisionShapes, rectshape).copy()
|
|
124
|
-
|
|
125
|
-
# Position rect correctly in the world
|
|
126
|
-
if Global.cam.follow != self.player:
|
|
127
|
-
if Global.cam.x != 0 or Global.cam.y != 0:
|
|
128
|
-
rect.x = ((nx * Global.tilemap.tileDim[0]) + Global.tilemap.offset[0]) - Global.cam.x
|
|
129
|
-
rect.y = ((ny * Global.tilemap.tileDim[1]) + Global.tilemap.offset[1]) - Global.cam.y
|
|
130
|
-
else:
|
|
131
|
-
rect.x += (nx * Global.tilemap.tileDim[0] + Global.tilemap.offset[0]) - Global.cam.x
|
|
132
|
-
rect.y += (ny * Global.tilemap.tileDim[1] + Global.tilemap.offset[1]) - Global.cam.y
|
|
133
|
-
else:
|
|
134
|
-
if Global.cam.x != 0 or Global.cam.y != 0:
|
|
135
|
-
rect.x += ((nx * Global.tilemap.tileDim[0]) + Global.tilemap.offset[0]) - Global.cam.x
|
|
136
|
-
rect.y += ((ny * Global.tilemap.tileDim[1]) + Global.tilemap.offset[1]) - Global.cam.y
|
|
137
|
-
else:
|
|
138
|
-
rect.x += (nx * Global.tilemap.tileDim[0] + Global.tilemap.offset[0]) - Global.cam.x
|
|
139
|
-
rect.y += (ny * Global.tilemap.tileDim[1] + Global.tilemap.offset[1]) - Global.cam.y
|
|
140
|
-
collisions.append(rect)
|
|
141
|
-
|
|
142
|
-
#check collisions
|
|
143
|
-
x, y = self.collisionLogic(collisions, x, y)
|
|
144
|
-
|
|
145
|
-
return x, y
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
def ckeck_is_on_ground(self):
|
|
151
|
-
self.canJump = False
|
|
152
|
-
rect = pygame.Rect(self.player.x, self.player.y + self.player.dim[1], self.player.dim[0], 10)
|
|
153
|
-
for collision in Global.collisions:
|
|
154
|
-
if collision.colliderect(rect):
|
|
155
|
-
self.canJump = True
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
def move(self, x, y):
|
|
159
|
-
self.vx += x * self.speed
|
|
160
|
-
self.vy += y * self.speed
|
|
161
|
-
|
|
162
|
-
def top_down_movement(self):
|
|
163
|
-
keys = pygame.key.get_pressed()
|
|
164
|
-
if keys[pygame.K_a]:
|
|
165
|
-
self.move(-1, 0)
|
|
166
|
-
if keys[pygame.K_d]:
|
|
167
|
-
self.move(1, 0)
|
|
168
|
-
if keys[pygame.K_w]:
|
|
169
|
-
self.move(0, -1)
|
|
170
|
-
if keys[pygame.K_s]:
|
|
171
|
-
self.move(0, 1)
|
|
172
|
-
#limit velocity
|
|
173
|
-
self.vx = clamp(self.vx, -self.maxV, self.maxV)
|
|
174
|
-
self.vy = clamp(self.vy, -self.maxV, self.maxV)
|
|
175
|
-
self.vx = moveTward(self.vx, 0, self.airRes)
|
|
176
|
-
self.vy = moveTward(self.vy, 0, self.airRes)
|
|
177
|
-
self.vx = zeroOut(self.vx, 0.3)
|
|
178
|
-
self.vy = zeroOut(self.vy, 0.3)
|
|
179
|
-
|
|
180
|
-
def platforming_movement(self):
|
|
181
|
-
keys = pygame.key.get_pressed()
|
|
182
|
-
if keys[pygame.K_a]:
|
|
183
|
-
self.move(-1, 0)
|
|
184
|
-
if keys[pygame.K_d]:
|
|
185
|
-
self.move(1, 0)
|
|
186
|
-
if keys[pygame.K_w] and self.canJump:
|
|
187
|
-
self.vy -= self.jump
|
|
188
|
-
#limit velocity
|
|
189
|
-
self.vx = clamp(self.vx, -self.maxV, self.maxV)
|
|
190
|
-
self.vx = moveTward(self.vx, 0, self.airRes)
|
|
191
|
-
self.vx = zeroOut(self.vx, 0.3)
|
|
192
|
-
if self.player.gravity and not self.onGround: self.vy += self.gravity
|
gamebox-0.1.3/tests/testMap.json
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"map": [[3, 3, 3, 3, 7, 7, 7, 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]],
|
|
3
|
-
"collisions": {"7": "none", "3": "halfBottom"}
|
|
4
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|