Compare commits
No commits in common. "0476dadfa4f0770c6e42fc7b4d192a82a236a0b6" and "6c535d3073b2c792a66e057c1248f271fc020c06" have entirely different histories.
0476dadfa4
...
6c535d3073
1 changed files with 28 additions and 29 deletions
57
game.py
57
game.py
|
|
@ -136,7 +136,7 @@ class Game:
|
||||||
|
|
||||||
for wall in self.walls:
|
for wall in self.walls:
|
||||||
pygame.draw.rect(self.display, "grey", wall)
|
pygame.draw.rect(self.display, "grey", wall)
|
||||||
|
|
||||||
|
|
||||||
def calculate_collsions(self) -> None:
|
def calculate_collsions(self) -> None:
|
||||||
if self.item_rect.colliderect(self.flashlight_rect):
|
if self.item_rect.colliderect(self.flashlight_rect):
|
||||||
|
|
@ -151,15 +151,17 @@ class Player:
|
||||||
self.game = game
|
self.game = game
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
|
|
||||||
self.width = width
|
self.player_width = width
|
||||||
self.height = height
|
self.player_height = height
|
||||||
|
|
||||||
self.speed = 150
|
self.speed = 150
|
||||||
|
|
||||||
self.rect = pygame.Rect(self.pos[0]-self.width/2, self.pos[1]-self.height/2, width, height)
|
self.rect = pygame.Rect(self.pos[0]-self.player_width/2, self.pos[1]-self.player_height/2, width, height)
|
||||||
|
|
||||||
def update(self, walls, dt):
|
def update(self, walls, dt):
|
||||||
|
|
||||||
|
self.player_rect = pygame.Rect(self.pos[0]-10, self.pos[1]-10, self.player_width, self.player_height)
|
||||||
|
|
||||||
|
|
||||||
# Smooth Movement
|
# Smooth Movement
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
|
|
@ -175,60 +177,57 @@ class Player:
|
||||||
movement: pygame.Vector2 = direction * self.speed * dt
|
movement: pygame.Vector2 = direction * self.speed * dt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
self.pos.x += movement.x
|
self.pos.x += movement.x
|
||||||
|
|
||||||
if self.pos.x - self.width/2 < 0:
|
|
||||||
self.pos.x = self.width/2
|
|
||||||
|
|
||||||
|
|
||||||
if self.pos.x + self.width/2 > SCREEN_WIDTH/2:
|
|
||||||
self.pos.x = SCREEN_WIDTH/2 - self.width/2
|
|
||||||
# self.pos += movement
|
# self.pos += movement
|
||||||
|
|
||||||
|
|
||||||
# player_rect.center = round(player_pos)
|
# player_rect.center = round(player_pos)
|
||||||
self.rect.centerx = round(self.pos.x)
|
self.player_rect.centerx = round(self.pos.x)
|
||||||
|
|
||||||
for wall in walls:
|
for wall in walls:
|
||||||
if self.rect.colliderect(wall):
|
if self.player_rect.colliderect(wall):
|
||||||
if movement.x > 0:
|
if movement.x > 0:
|
||||||
self.rect.right = wall.left
|
self.player_rect.right = wall.left
|
||||||
|
|
||||||
elif movement.x < 0:
|
elif movement.x < 0:
|
||||||
self.rect.left = wall.right
|
self.player_rect.left = wall.right
|
||||||
|
|
||||||
self.pos.x = self.rect.centerx
|
self.pos.x = self.player_rect.centerx
|
||||||
|
|
||||||
|
# if self.pos.x < self.player_rect.width/2 or self.pos.x + self.player_rect.width > SCREEN_WIDTH:
|
||||||
|
# self.pos.x = old_position_x
|
||||||
|
if self.pos.x - self.player_rect.width/2 < 0:
|
||||||
|
self.pos.x = self.player_rect.width/2
|
||||||
|
|
||||||
|
if self.pos.x + self.player_rect.width/2 > SCREEN_WIDTH:
|
||||||
|
self.pos.x = SCREEN_WIDTH - self.player_rect.width/2
|
||||||
|
|
||||||
self.pos.y += movement.y
|
self.pos.y += movement.y
|
||||||
|
|
||||||
if self.pos.y - self.height/2 < 0:
|
self.player_rect.centery = round(self.pos.y)
|
||||||
self.pos.y = self.height/2
|
|
||||||
|
|
||||||
if self.pos.y + self.height/2 > SCREEN_HEIGHT/2:
|
|
||||||
self.pos.y = SCREEN_HEIGHT/2 - self.height/2
|
|
||||||
|
|
||||||
self.rect.centery = round(self.pos.y)
|
|
||||||
|
|
||||||
for wall in walls:
|
for wall in walls:
|
||||||
if self.rect.colliderect(wall):
|
if self.player_rect.colliderect(wall):
|
||||||
if movement.y > 0:
|
if movement.y > 0:
|
||||||
self.rect.bottom = wall.top
|
self.player_rect.bottom = wall.top
|
||||||
|
|
||||||
elif movement.y < 0:
|
elif movement.y < 0:
|
||||||
self.rect.top = wall.bottom
|
self.player_rect.top = wall.bottom
|
||||||
|
|
||||||
self.pos.y = self.rect.centery
|
|
||||||
|
|
||||||
|
self.pos.y = self.player_rect.centery
|
||||||
|
|
||||||
|
if self.pos.y - self.player_rect.height/2 < 0:
|
||||||
|
self.pos.y = self.player_rect.height/2
|
||||||
|
|
||||||
|
if self.pos.y + self.player_rect.height/2 > SCREEN_HEIGHT:
|
||||||
|
self.pos.y = SCREEN_HEIGHT - self.player_rect.height/2
|
||||||
|
|
||||||
|
|
||||||
def render(self, surface):
|
def render(self, surface):
|
||||||
|
|
||||||
pygame.draw.rect(surface, (255, 0, 0), self.rect)
|
pygame.draw.rect(surface, (255, 0, 0), self.player_rect)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue