GameBox 0.1.3__py3-none-any.whl → 0.3.0__py3-none-any.whl

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/__init__.py CHANGED
@@ -5,7 +5,7 @@ GameBox makes it easy to build 2D games with graphics, sound, and UI in just a f
5
5
  """
6
6
 
7
7
 
8
- __version__ = "0.1.3"
8
+ __version__ = "0.3.0"
9
9
  __author__ = "Sam Fertig"
10
10
 
11
11
  #____imports____
GameBox/_game.py CHANGED
@@ -28,7 +28,8 @@ class Game:
28
28
  self.objs = []
29
29
 
30
30
 
31
- def update(self, frame_rate=60):
31
+ def update(self, event: pygame.event,frame_rate=60):
32
+ Global.event = event
32
33
  #clear collisions
33
34
  Global.collisions.clear()
34
35
 
GameBox/basics/_net.py CHANGED
@@ -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: object = None
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
+
GameBox/helpers/_input.py CHANGED
@@ -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): return pygame.key.get_pressed()[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
 
@@ -3,6 +3,7 @@ import numpy as np
3
3
 
4
4
  from ..basics._net import Global
5
5
  from ..basics.utils import clamp, moveTward, zeroOut
6
+ from ..helpers._collisions import CheckCollisions
6
7
 
7
8
  class _playerPhysics:
8
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):
@@ -24,7 +25,9 @@ class _playerPhysics:
24
25
 
25
26
  def update(self):
26
27
  self.ckeck_is_on_ground()
27
- x, y = self.check_collisions()
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)
28
31
  if (Global.cam.follow) != (self.player):
29
32
  self.player.x = x
30
33
  self.player.y = y
@@ -32,117 +35,6 @@ class _playerPhysics:
32
35
  dx = -(self.player.x - x)
33
36
  dy = -(self.player.y - y)
34
37
  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
38
 
147
39
 
148
40
 
@@ -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, offset: tuple):
11
+ def __init__(self, tileSet: str, tileDim: tuple, tileScale: float, mapDim: tuple, mapFill: int, saveFile = None):
11
12
  self.tilesetFile = tileSet
12
- self.mapFile = None
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 = 0
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
- print(f"tiles: {self.tiles}")
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] + self.offset[0]) - Global.cam.x
66
- my = (y * self.tileDim[1] + self.offset[1]) - Global.cam.y
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
- json.dump(self.map.tolist(), f)
91
+ data = {}
92
+ data["map"] = self.map.tolist()
93
+ data["collisions"] = self.collisionDict
94
+ json.dump(data, f)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: GameBox
3
- Version: 0.1.3
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: {https://github.com/sfertig/GameBox}
17
+ github: https://github.com/sfertig/GameBox
18
18
 
19
19
  Versioning
20
20
  GameBox follows a modified semantic versioning scheme.
@@ -0,0 +1,20 @@
1
+ GameBox/__init__.py,sha256=uY6ByC6ujfiIjG257S2V75gHN0SWVX-N0MaeQqctlHU,727
2
+ GameBox/_game.py,sha256=QqfWQRLIiKQ-QjEGQYFLpsjEtPNuhxMqiCnPmXFyq3M,1548
3
+ GameBox/basics/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ GameBox/basics/_net.py,sha256=tZsfUc-rOsbRPxgjzR95gY6vKa8CxM8TJhCBtDnVhtg,766
5
+ GameBox/basics/_shapes.py,sha256=7k5Mxnm_NdFssW6Zao8DSJAHJQM3pVWx2xZ4QDyF6lQ,1035
6
+ GameBox/basics/cammera.py,sha256=qo20hc5f3FLiBpFeWgBObj-EQ93Hg4RgEUtIwPIylOI,930
7
+ GameBox/basics/utils.py,sha256=w6H34MhxcNoX58iQtlvbT5-4GXefy3RIMTxZGP9fSxA,432
8
+ GameBox/helpers/_collisions.py,sha256=f19oIYsyY3G9-5J-nUVBvbuFT1EPeDwcbycIP0TLGms,4336
9
+ GameBox/helpers/_input.py,sha256=97bPxM5dkonRywOhj11lgHVQVicuLAKYi_w8EPM0Af8,1963
10
+ GameBox/player/_player.py,sha256=R0vH1mZhl6RENiQl3Vg4ACF9agR5OP50MenvhgFBYOk,1769
11
+ GameBox/player/_playerControler.py,sha256=XEb87RFlgLFoXnXqxIS4lpzHN0wywMNCX5y-NIhw0cs,1715
12
+ GameBox/player/_playerPhysics.py,sha256=XqgEC5FB5mrnlalY3M5TLScw40mSvNAc0AE9g3amEr0,2918
13
+ GameBox/tilemap/_Editor.py,sha256=98JcwAvL6JgSG2S-V-527lwpfzbE-A-BbezCI9EfTyI,1027
14
+ GameBox/tilemap/_collisionDef.py,sha256=VxWcfz3nlhyYI_Hemp-LvC6IDC-YN6wIdRaU8AsgipU,1621
15
+ GameBox/tilemap/_editorBrushes.py,sha256=GFfRV0OB3t9sFb37eO3sB8Cmwyd5i_q64q5IjYlkx1k,7247
16
+ GameBox/tilemap/_tilemap.py,sha256=SEQlokpsUO9zQkLMF-j6amx1FqNeD2eM2CRFj3zCnZ8,3330
17
+ gamebox-0.3.0.dist-info/METADATA,sha256=gwV3s_16qJEtjpHlGRTHL9v1M7B02VXU14Rjm_bU1XA,1059
18
+ gamebox-0.3.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
19
+ gamebox-0.3.0.dist-info/licenses/LICENSE,sha256=gcuuhKKc5-dwvyvHsXjlC9oM6N5gZ6umYbC8ewW1Yvg,35821
20
+ gamebox-0.3.0.dist-info/RECORD,,
@@ -1,17 +0,0 @@
1
- GameBox/__init__.py,sha256=jU3a32yFDiWvSHanU2_xJHNi3Ngcqrvjn190SyKlNwE,727
2
- GameBox/_game.py,sha256=eG_rCsLwDhkY4AdjvnO80efswhyRrqPPxNsTSR8uIaY,1498
3
- GameBox/basics/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- GameBox/basics/_net.py,sha256=PWt57uI51seDLszyQBXriFEiLVNiVCBj3zpiDR9k0B4,735
5
- GameBox/basics/_shapes.py,sha256=7k5Mxnm_NdFssW6Zao8DSJAHJQM3pVWx2xZ4QDyF6lQ,1035
6
- GameBox/basics/cammera.py,sha256=qo20hc5f3FLiBpFeWgBObj-EQ93Hg4RgEUtIwPIylOI,930
7
- GameBox/basics/utils.py,sha256=w6H34MhxcNoX58iQtlvbT5-4GXefy3RIMTxZGP9fSxA,432
8
- GameBox/helpers/_input.py,sha256=XzlrMmhomNwR6XoaKsOq5nDiNPAdK-vRCQEuayCHGHA,1812
9
- GameBox/player/_player.py,sha256=R0vH1mZhl6RENiQl3Vg4ACF9agR5OP50MenvhgFBYOk,1769
10
- GameBox/player/_playerControler.py,sha256=XEb87RFlgLFoXnXqxIS4lpzHN0wywMNCX5y-NIhw0cs,1715
11
- GameBox/player/_playerPhysics.py,sha256=m_W2SrTFscIFG70v8gj0G2grLnEM2IeyPIhgMXA48yI,8115
12
- GameBox/tilemap/_collisionDef.py,sha256=lZCmSwIsLgxvEB3X30re_izg1DHY83IWp84NcM1cIkM,1393
13
- GameBox/tilemap/_tilemap.py,sha256=YrDMv7h2B_awe_J9LCW1JaGXHGve2mqfgLQk4-tvxsE,2695
14
- gamebox-0.1.3.dist-info/METADATA,sha256=3oHumIWtO2w1051z48ltCg6ujYfxtakvL_dDD4RfXso,1061
15
- gamebox-0.1.3.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
16
- gamebox-0.1.3.dist-info/licenses/LICENSE,sha256=gcuuhKKc5-dwvyvHsXjlC9oM6N5gZ6umYbC8ewW1Yvg,35821
17
- gamebox-0.1.3.dist-info/RECORD,,