change free roam movement to grid movement
This commit is contained in:
parent
51d3ce520b
commit
0f92cbce7d
@ -6,13 +6,13 @@ window_width = 1280
|
||||
window_height = 720
|
||||
|
||||
|
||||
def draw_game():
|
||||
game_map.draw(win)
|
||||
def draw_game(delta):
|
||||
game_map.draw(delta, win)
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
def update_game(pressed_keys):
|
||||
game_map.update(pressed_keys)
|
||||
def update_game(delta, pressed_keys):
|
||||
game_map.update(delta, pressed_keys)
|
||||
pass
|
||||
|
||||
|
||||
@ -30,7 +30,7 @@ if __name__ == '__main__':
|
||||
|
||||
while run:
|
||||
# Set the framerate
|
||||
clock.tick(60)
|
||||
ms = clock.tick(60)
|
||||
|
||||
events = pygame.event.get()
|
||||
|
||||
@ -40,5 +40,5 @@ if __name__ == '__main__':
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
|
||||
draw_game()
|
||||
update_game(keys)
|
||||
draw_game(ms)
|
||||
update_game(ms, keys)
|
||||
|
@ -1,41 +1,17 @@
|
||||
from pygame.rect import Rect
|
||||
|
||||
from survival.player import Player
|
||||
from survival.quad_tree import QuadTree
|
||||
from survival.stone import Stone
|
||||
from survival.tile_layer import TileLayer
|
||||
|
||||
|
||||
class GameMap:
|
||||
|
||||
def __init__(self, width, height):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.game_objects = []
|
||||
self.player = Player()
|
||||
self.game_objects.append(self.player)
|
||||
self.tiles_layer = TileLayer(width, height)
|
||||
self.game_objects.append(Stone([100, 200]))
|
||||
self.quad_tree = QuadTree(0, Rect(0, 0, width * 32, height * 32))
|
||||
|
||||
def draw(self, window):
|
||||
def draw(self, delta, window):
|
||||
self.tiles_layer.draw(window)
|
||||
self.player.draw(window)
|
||||
|
||||
for game_object in self.game_objects:
|
||||
game_object.draw(window)
|
||||
|
||||
def update(self, pressed_keys):
|
||||
self.quad_tree.clear()
|
||||
|
||||
for game_object in self.game_objects:
|
||||
self.quad_tree.insert(game_object)
|
||||
|
||||
self.player.update(pressed_keys)
|
||||
|
||||
for game_object in self.game_objects:
|
||||
possible_colliders = []
|
||||
self.quad_tree.retrieve(possible_colliders, game_object)
|
||||
for collider in possible_colliders:
|
||||
if game_object.get_rect().colliderect(collider.get_rect()) and game_object != collider:
|
||||
game_object.velocity = [0, 0]
|
||||
game_object.pos = game_object.last_pos
|
||||
def update(self, delta, pressed_keys):
|
||||
self.player.update(delta, pressed_keys)
|
||||
|
@ -8,27 +8,39 @@ from survival.game_object import GameObject
|
||||
class Player(GameObject):
|
||||
def __init__(self):
|
||||
super().__init__([0, 0], os.path.join('..', 'assets', 'player.png'))
|
||||
self.speed = 3
|
||||
self.movement_target = [self.pos[0], self.pos[1]]
|
||||
|
||||
def draw(self, window):
|
||||
super().draw(window)
|
||||
|
||||
def update(self, pressed_keys):
|
||||
def is_moving(self):
|
||||
return self.pos != self.movement_target
|
||||
|
||||
def update(self, delta, pressed_keys):
|
||||
if self.is_moving():
|
||||
if self.velocity[0] != 0:
|
||||
self.pos[0] += self.velocity[0] * self.speed * delta / 100
|
||||
if abs(self.movement_target[0] - self.pos[0]) < 0.1 * self.speed:
|
||||
self.velocity = [0, 0]
|
||||
self.pos = self.movement_target
|
||||
else:
|
||||
self.pos[1] += self.velocity[1] * self.speed * delta / 100
|
||||
if abs(self.pos[1] - self.movement_target[1]) < 0.1 * self.speed:
|
||||
self.velocity = [0, 0]
|
||||
self.pos = self.movement_target
|
||||
return
|
||||
|
||||
if pressed_keys[pygame.K_LEFT]:
|
||||
self.velocity[0] = -1
|
||||
self.velocity = [-1, 0]
|
||||
self.movement_target = [self.pos[0] - 32, self.pos[1]]
|
||||
elif pressed_keys[pygame.K_RIGHT]:
|
||||
self.velocity[0] = 1
|
||||
else:
|
||||
self.velocity[0] = 0
|
||||
|
||||
if pressed_keys[pygame.K_DOWN]:
|
||||
self.velocity[1] = 1
|
||||
self.velocity = [1, 0]
|
||||
self.movement_target = [self.pos[0] + 32, self.pos[1]]
|
||||
elif pressed_keys[pygame.K_DOWN]:
|
||||
self.velocity = [0, 1]
|
||||
self.movement_target = [self.pos[0], self.pos[1] + 32]
|
||||
elif pressed_keys[pygame.K_UP]:
|
||||
self.velocity[1] = -1
|
||||
else:
|
||||
self.velocity[1] = 0
|
||||
|
||||
self.last_pos = [self.pos[0], self.pos[1]]
|
||||
|
||||
self.pos[0] += self.velocity[0]
|
||||
self.pos[1] += self.velocity[1]
|
||||
self.velocity = [0, -1]
|
||||
self.movement_target = [self.pos[0], self.pos[1] - 32]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user