Kaydet (Commit) 61119cda authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

more spriting

üst 7a8705ae
...@@ -10,10 +10,9 @@ from typing import Tuple ...@@ -10,10 +10,9 @@ from typing import Tuple
import pygame import pygame
from pygame.locals import * 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
from lordlarkamarasi.players import PlayerRegistery from lordlarkamarasi.players import PlayerRegistery
from lordlarkamarasi.sprites import Player
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):
...@@ -54,43 +53,44 @@ class Map(UserList): ...@@ -54,43 +53,44 @@ class Map(UserList):
class Game: class Game:
def __init__(self, window: Tuple[int, int] = (800, 800)): def __init__(self):
pygame.init() pygame.init()
pygame.display.set_caption("Lordlar Kamarası") pygame.display.set_caption("Lordlar Kamarası")
self.surface = pygame.display.set_mode(window) self.surface = pygame.display.set_mode((800, 800), DOUBLEBUF)
self.surface.set_alpha()
self.map = Map.from_file('maps/base_map.ini') self.map = Map.from_file('maps/base_map.ini')
self.sprites = pygame.sprite.Group()
self.playerreg = PlayerRegistery()
self.playerreg = PlayerRegistery(sprite_hook = self.sprites)
self.player = self.playerreg.join("BTaskaya") self.player = self.playerreg.join("BTaskaya")
self.logger = logging.getLogger('Game') self.logger = logging.getLogger('Game')
self.frame_count, self.frame_rate = 0, 0
def process_event(self, event: pygame.event.EventType): def process_event(self, event: pygame.event.EventType):
if event.type is MOUSEMOTION: self.sprites.update()
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()
pygame.display.update()
def event_handler(self): def event_handler(self):
event = pygame.event.wait() event = pygame.event.wait()
while event.type is not QUIT: while event.type != QUIT:
t0 = time.clock()
self.logger.info(f"Event handled: {event}") self.logger.info(f"Event handled: {event}")
self.process_event(event) self.process_event(event)
event = pygame.event.wait() self.draw_players()
pygame.display.update()
t1 = time.clock()
self.logger.info(f"Event processed in {t1 - t0} seconds")
event = pygame.event.poll()
else: else:
pygame.quit() pygame.quit()
def start(self): def start(self):
self.draw_map()
self.event_handler() self.event_handler()
def draw_map(self): def draw_map(self):
...@@ -101,12 +101,12 @@ class Game: ...@@ -101,12 +101,12 @@ class Game:
self.surface.blit( self.surface.blit(
block.value.texture, (cpos * TILE_SIZE, rpos * TILE_SIZE) block.value.texture, (cpos * TILE_SIZE, rpos * TILE_SIZE)
) )
def draw_players(self):
self.logger.info(f"Drawing {len(self.playerreg)} players") self.logger.info(f"Drawing {len(self.playerreg)} players")
for player in self.playerreg.values(): self.sprites.draw(self.surface)
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) logging.basicConfig(level=logging.INFO)
game = Game() game = Game()
game.start() game.start()
from dataclasses import dataclass, field from dataclasses import dataclass, field
from collections import UserDict from collections import UserDict
from lordlarkamarasi.sprites import Player
@dataclass @dataclass
class Coordinates: class Coordinates:
...@@ -12,11 +13,17 @@ class Coordinates: ...@@ -12,11 +13,17 @@ class Coordinates:
class Player: class Player:
username: str username: str
coord: Coordinates = field(default_factory=Coordinates, repr=False) coord: Coordinates = field(default_factory=Coordinates, repr=False)
player: Player = field(default_factory=Player, repr=False)
class PlayerRegistery(UserDict): class PlayerRegistery(UserDict):
def __init__(self, sprite_hook, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sprite_hook = sprite_hook
def join(self, username): def join(self, username):
self.data[username] = Player(username) self.data[username] = Player(username)
self.sprite_hook.add(self.data[username].player)
return self.data[username] return self.data[username]
def quit(self, username): def quit(self, username):
self.sprite_hook.remove(self.data[username].player)
del self.data[username] del self.data[username]
import pygame
from lordlarkamarasi.textures import relative_load
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = relative_load('textures/entities/player.png')
self.rect = self.image.get_rect()
self.rect.centerx = 800 / 2
self.rect.bottom = 800 - 10
self.speedx = 0
def update(self):
self.speedx = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -1
if keystate[pygame.K_RIGHT]:
self.speedx = 1
self.rect.x += self.speedx
if self.rect.right > 800:
self.rect.right = 800
if self.rect.left < 0:
self.rect.left = 0
...@@ -29,9 +29,6 @@ class Blocks(Enum): ...@@ -29,9 +29,6 @@ class Blocks(Enum):
block, = filter(lambda member: member.value.id == id, cls.__members__.values()) block, = filter(lambda member: member.value.id == id, cls.__members__.values())
return cls(block) return cls(block)
class Entities(Enum):
PLAYER = Texture(0xF0, relative_load("textures/entities/player.png"))
ALL_BLOCKS = set(Blocks.__members__.values()) ALL_BLOCKS = set(Blocks.__members__.values())
AVAILABLE_BLOCKS = set(filter(lambda block: not block.value.must_connect, ALL_BLOCKS)) AVAILABLE_BLOCKS = set(filter(lambda block: not block.value.must_connect, ALL_BLOCKS))
MUST_CONNECT_BLOCKS = ALL_BLOCKS - AVAILABLE_BLOCKS MUST_CONNECT_BLOCKS = ALL_BLOCKS - AVAILABLE_BLOCKS
......
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