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

a try to player movement

üst e1cde7ff
import itertools import itertools
import random import random
import time import time
import logging
from collections import UserList from collections import UserList
from contextlib import suppress from contextlib import suppress
from configparser import ConfigParser from configparser import ConfigParser
from typing import Tuple from typing import Tuple
import pygame 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.textures import AVAILABLE_BLOCKS, BASE_PATH, TILE_SIZE, Blocks, Entities
from lordlarkamarasi.players import PlayerRegistery from lordlarkamarasi.players import PlayerRegistery
MOVE_KEYS = {K_RIGHT, K_LEFT, K_UP, K_DOWN}
class Map(UserList): class Map(UserList):
def __init__(self, width, height, surface): def __init__(self, width, height, surface):
self.width = width self.width = width
...@@ -59,34 +62,51 @@ class Game: ...@@ -59,34 +62,51 @@ class Game:
self.map = Map.from_file('maps/base_map.ini') self.map = Map.from_file('maps/base_map.ini')
self.playerreg = PlayerRegistery() self.playerreg = PlayerRegistery()
self.player = self.playerreg.join("BTaskaya")
self.logger = logging.getLogger('Game')
def process_event(self, event: pygame.event.EventType): 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() self.draw_map()
pygame.display.update()
def event_handler(self, interval: int = 0.005):
def event_handler(self):
event = pygame.event.wait() event = pygame.event.wait()
while event.type is not QUIT: while event.type is not QUIT:
self.process_event(event) self.process_event(event)
pygame.display.update() event = pygame.event.wait()
time.sleep(interval) print(event)
else: else:
pygame.quit() pygame.quit()
def start(self): def start(self):
self.playerreg.join("BTaskaya")
self.event_handler() self.event_handler()
def draw_map(self): 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 rpos, row in enumerate(self.map):
for cpos, block in enumerate(row): for cpos, block in enumerate(row):
self.surface.blit( self.surface.blit(
block.value.texture, (cpos * TILE_SIZE, rpos * TILE_SIZE) block.value.texture, (cpos * TILE_SIZE, rpos * TILE_SIZE)
) )
self.logger.info(f"Drawing {len(self.playerreg)} players")
for player in self.playerreg.values(): for player in self.playerreg.values():
self.surface.blit(Entities.PLAYER.value.texture, (player.coord.x * TILE_SIZE, player.coord.y * TILE_SIZE)) self.surface.blit(Entities.PLAYER.value.texture, (player.coord.x * TILE_SIZE, player.coord.y * TILE_SIZE))
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
game = Game() game = Game()
game.start() game.start()
...@@ -6,14 +6,17 @@ class Coordinates: ...@@ -6,14 +6,17 @@ class Coordinates:
x: int = 0 x: int = 0
y: int = 0 y: int = 0
def __repr__(self):
return f"{self.y}:{self.x}"
@dataclass @dataclass
class Player: class Player:
username: str username: str
coord: Coordinates = field(default_factory=Coordinates) coord: Coordinates = field(default_factory=Coordinates, repr=False)
class PlayerRegistery(UserDict): class PlayerRegistery(UserDict):
def join(self, username): def join(self, username):
self.data[username] = Player(username) self.data[username] = Player(username)
return self.data[username]
def quit(self, username): def quit(self, username):
del self.data[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