Kaydet (Commit) 8c0fdd0e authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

a try to player movement

üst e1cde7ff
import itertools
import random
import time
import logging
from collections import UserList
from contextlib import suppress
from configparser import ConfigParser
from typing import Tuple
import pygame
from pygame.locals import QUIT
from pygame.locals import *
from lordlarkamarasi.textures import AVAILABLE_BLOCKS, BASE_PATH, TILE_SIZE, Blocks, Entities
from lordlarkamarasi.players import PlayerRegistery
MOVE_KEYS = {K_RIGHT, K_LEFT, K_UP, K_DOWN}
class Map(UserList):
def __init__(self, width, height, surface):
self.width = width
......@@ -59,34 +62,51 @@ class Game:
self.map = Map.from_file('maps/base_map.ini')
self.playerreg = PlayerRegistery()
self.player = self.playerreg.join("BTaskaya")
self.logger = logging.getLogger('Game')
def process_event(self, event: pygame.event.EventType):
if event.type is MOUSEMOTION:
x, y = event.rel
if x > 0 and self.player.coord.x <= self.map.width:
self.player.coord.x += 1
elif x < 0 and self.player.coord.x > self.map.width:
self.player.coord.x -= 1
if y > 0 and self.player.coord.y <= self.map.height:
self.player.coord.y += 1
elif y < 0 and self.player.coord.y > self.map.height:
self.player.coord.y -= 1
self.draw_map()
def event_handler(self, interval: int = 0.005):
pygame.display.update()
def event_handler(self):
event = pygame.event.wait()
while event.type is not QUIT:
self.process_event(event)
pygame.display.update()
time.sleep(interval)
event = pygame.event.wait()
print(event)
else:
pygame.quit()
def start(self):
self.playerreg.join("BTaskaya")
self.event_handler()
def draw_map(self):
self.logger.info(f"Drawing surface to {self.map.width * self.map.height} tiles")
for rpos, row in enumerate(self.map):
for cpos, block in enumerate(row):
self.surface.blit(
block.value.texture, (cpos * TILE_SIZE, rpos * TILE_SIZE)
)
self.logger.info(f"Drawing {len(self.playerreg)} players")
for player in self.playerreg.values():
self.surface.blit(Entities.PLAYER.value.texture, (player.coord.x * TILE_SIZE, player.coord.y * TILE_SIZE))
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
game = Game()
game.start()
......@@ -6,14 +6,17 @@ class Coordinates:
x: int = 0
y: int = 0
def __repr__(self):
return f"{self.y}:{self.x}"
@dataclass
class Player:
username: str
coord: Coordinates = field(default_factory=Coordinates)
coord: Coordinates = field(default_factory=Coordinates, repr=False)
class PlayerRegistery(UserDict):
def join(self, username):
self.data[username] = Player(username)
return self.data[username]
def quit(self, username):
del self.data[username]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment