Merge remote-tracking branch 'origin/master' into Player
This commit is contained in:
commit
c5fddf144d
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
/.idea/
|
/.idea/
|
||||||
__pycache__
|
__pycache__
|
||||||
|
/venv/
|
||||||
|
BIN
data/images/entities/bush.png
Normal file
BIN
data/images/entities/bush.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.4 KiB |
BIN
data/images/entities/treasure.png
Normal file
BIN
data/images/entities/treasure.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.7 KiB |
@ -14,7 +14,7 @@ class Collidable(Entity):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
col = Collidable(1, 1, 1)
|
#col = Collidable(1, 1, 1)
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -13,25 +13,20 @@ class Player(Entity):
|
|||||||
|
|
||||||
# Move in a desired direction
|
# Move in a desired direction
|
||||||
def move(self, rotation):
|
def move(self, rotation):
|
||||||
# If the player is not facing given direction, it will not move the first time, it will only get rotated
|
if rotation.value == Rotations.NORTH.value:
|
||||||
if self.rotation.value != rotation.value:
|
self.rect.y -= self.rect.w
|
||||||
self.rotate(rotation)
|
elif rotation.value == Rotations.EAST.value:
|
||||||
# Otherwise, move one tile to a given direction
|
self.rect.x += self.rect.w
|
||||||
else:
|
elif rotation.value == Rotations.SOUTH.value:
|
||||||
# TODO: Collision checks
|
self.rect.y += self.rect.w
|
||||||
if rotation.value == Rotations.NORTH.value:
|
elif rotation.value == Rotations.WEST.value:
|
||||||
self.rect.y -= self.rect.w
|
self.rect.x -= self.rect.w
|
||||||
elif rotation.value == Rotations.EAST.value:
|
|
||||||
self.rect.x += self.rect.w
|
|
||||||
elif rotation.value == Rotations.SOUTH.value:
|
|
||||||
self.rect.y += self.rect.w
|
|
||||||
elif rotation.value == Rotations.WEST.value:
|
|
||||||
self.rect.x -= self.rect.w
|
|
||||||
|
|
||||||
def rotate(self, rotation):
|
def rotate(self, rotation):
|
||||||
self.image = pygame.transform.rotate(self.image, ((self.rotation.value - rotation.value) * 90))
|
# If the player is not facing given direction, it will not move the first time, it will only get rotated
|
||||||
self.rotation = rotation
|
if self.rotation.value != rotation.value:
|
||||||
|
self.image = pygame.transform.rotate(self.image, ((self.rotation.value - rotation.value) * 90))
|
||||||
|
self.rotation = rotation
|
||||||
|
|
||||||
class Rotations(Enum):
|
class Rotations(Enum):
|
||||||
NORTH = 0
|
NORTH = 0
|
||||||
|
@ -7,6 +7,7 @@ TIMEOUT = 100
|
|||||||
class EventManager:
|
class EventManager:
|
||||||
keyTimeout = 0
|
keyTimeout = 0
|
||||||
|
|
||||||
|
#self.game.map
|
||||||
def __init__(self, gameObject, player):
|
def __init__(self, gameObject, player):
|
||||||
self.game = gameObject
|
self.game = gameObject
|
||||||
self.player = player
|
self.player = player
|
||||||
@ -34,11 +35,21 @@ class EventManager:
|
|||||||
# TODO: Load key bindings from JSON
|
# TODO: Load key bindings from JSON
|
||||||
|
|
||||||
if keys[pygame.K_w]:
|
if keys[pygame.K_w]:
|
||||||
self.player.move(Rotations.NORTH)
|
self.player.rotate(Rotations.NORTH)
|
||||||
|
if not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w):
|
||||||
|
self.player.move(Rotations.NORTH)
|
||||||
if keys[pygame.K_s]:
|
if keys[pygame.K_s]:
|
||||||
self.player.move(Rotations.SOUTH)
|
self.player.rotate(Rotations.SOUTH)
|
||||||
|
if not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w):
|
||||||
|
self.player.move(Rotations.SOUTH)
|
||||||
if keys[pygame.K_d]:
|
if keys[pygame.K_d]:
|
||||||
self.player.move(Rotations.EAST)
|
self.player.rotate(Rotations.EAST)
|
||||||
|
if not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y):
|
||||||
|
self.player.move(Rotations.EAST)
|
||||||
if keys[pygame.K_a]:
|
if keys[pygame.K_a]:
|
||||||
self.player.move(Rotations.WEST)
|
self.player.rotate(Rotations.WEST)
|
||||||
|
if not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y):
|
||||||
|
self.player.move(Rotations.WEST)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ from os import path
|
|||||||
from game.EventManager import EventManager
|
from game.EventManager import EventManager
|
||||||
from game.Screen import Screen
|
from game.Screen import Screen
|
||||||
from game.Map import Map
|
from game.Map import Map
|
||||||
|
from src.entities.Player import Player
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
@ -27,6 +28,7 @@ class Game:
|
|||||||
print("Initializing pygame...", end=" ")
|
print("Initializing pygame...", end=" ")
|
||||||
pygame.init()
|
pygame.init()
|
||||||
self.spritesList = pygame.sprite.Group()
|
self.spritesList = pygame.sprite.Group()
|
||||||
|
|
||||||
print("OK")
|
print("OK")
|
||||||
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ")
|
print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ")
|
||||||
|
|
||||||
@ -38,12 +40,11 @@ class Game:
|
|||||||
self.screen = Screen(self, self.config["window"])
|
self.screen = Screen(self, self.config["window"])
|
||||||
print("OK")
|
print("OK")
|
||||||
|
|
||||||
self.eventManager = EventManager(self)
|
|
||||||
|
|
||||||
# Start Map implement
|
|
||||||
self.mapDataFolder = path.dirname("../data/mapdata/")
|
self.mapDataFolder = path.dirname("../data/mapdata/")
|
||||||
self.map = Map(path.join(self.mapDataFolder, 'map.txt'), self.screen)
|
self.map = Map(path.join(self.mapDataFolder, 'map.txt'), self.screen)
|
||||||
# End Map implement
|
self.player = Player((0, 0), self.map.tileSize)
|
||||||
|
self.map.addEntity(self.player)
|
||||||
|
self.eventManager = EventManager(self, self.player)
|
||||||
|
|
||||||
self.mainLoop()
|
self.mainLoop()
|
||||||
|
|
||||||
|
@ -6,7 +6,9 @@ class Map:
|
|||||||
def __init__(self, filename, screen):
|
def __init__(self, filename, screen):
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.terrain = []
|
self.terrain = []
|
||||||
self.entities = []
|
self.collidableTerrain = []
|
||||||
|
self.collidables = pygame.sprite.Group()
|
||||||
|
|
||||||
with open(filename, 'rt') as f:
|
with open(filename, 'rt') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
self.terrain.append(line)
|
self.terrain.append(line)
|
||||||
@ -19,15 +21,26 @@ class Map:
|
|||||||
self.height = self.tileHeight * self.tileSize
|
self.height = self.tileHeight * self.tileSize
|
||||||
|
|
||||||
self.terrainDraw()
|
self.terrainDraw()
|
||||||
|
|
||||||
def terrainDraw(self):
|
def terrainDraw(self):
|
||||||
for row, tiles in enumerate(self.terrain):
|
for row, tiles in enumerate(self.terrain):
|
||||||
for col, tile in enumerate(tiles):
|
for col, tile in enumerate(tiles):
|
||||||
if tile == 'w':
|
if tile == 'w':
|
||||||
self.screen.draw(TerrainTile('wall.png', self.tileSize), Locations.MAP, col*self.tileSize, row*self.tileSize)
|
object = TerrainTile(col, row, 'wall.png', self.tileSize)
|
||||||
|
self.screen.draw(object, Locations.MAP, 0, 0)
|
||||||
|
self.collidables.add(object)
|
||||||
elif tile == ',':
|
elif tile == ',':
|
||||||
self.screen.draw(TerrainTile('floor.png', self.tileSize), Locations.MAP, col*self.tileSize, row*self.tileSize)
|
self.screen.draw(TerrainTile(col, row, 'floor.png', self.tileSize), Locations.MAP, 0, 0)
|
||||||
elif tile == '.':
|
elif tile == '.':
|
||||||
self.screen.draw(TerrainTile('grass.png', self.tileSize), Locations.MAP, col*self.tileSize, row*self.tileSize)
|
self.screen.draw(TerrainTile(col, row, 'grass.png', self.tileSize), Locations.MAP, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def addEntity(self, entity):
|
||||||
|
self.screen.draw(entity, Locations.MAP, 0, 0)
|
||||||
|
self.collidables.add(entity)
|
||||||
|
|
||||||
|
# add object to map.collidables list to be collidable
|
||||||
|
def collision(self, x, y):
|
||||||
|
for b in self.collidables:
|
||||||
|
if b.rect.x == x and b.rect.y == y:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
@ -4,10 +4,14 @@ from os import path
|
|||||||
|
|
||||||
|
|
||||||
class TerrainTile(pygame.sprite.Sprite):
|
class TerrainTile(pygame.sprite.Sprite):
|
||||||
def __init__(self, texture, tileSize):
|
def __init__(self, x, y, texture, tileSize):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.imagesFolder = path.dirname("../data/images/")
|
self.imagesFolder = path.dirname("../data/images/")
|
||||||
self.terrainFolder = path.join(self.imagesFolder, 'terrain')
|
self.terrainFolder = path.join(self.imagesFolder, 'terrain')
|
||||||
self.image = pygame.image.load(os.path.join(self.terrainFolder, texture)).convert()
|
self.image = pygame.image.load(os.path.join(self.terrainFolder, texture)).convert()
|
||||||
self.image = pygame.transform.scale(self.image, (tileSize, tileSize))
|
self.image = pygame.transform.scale(self.image, (tileSize, tileSize))
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.rect.x = x * tileSize
|
||||||
|
self.rect.y = y * tileSize
|
||||||
|
@ -1,4 +1,66 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
# Timer class for the game
|
||||||
|
# We count time every second, an the time elapsed in game is 60 times the actual time
|
||||||
|
# So, if we spend 60 seconds, we count that we spent 60 minutes.
|
||||||
|
|
||||||
|
# Day/night cycle parameters
|
||||||
|
DAY_START = "7:00"
|
||||||
|
NIGHT_START = "22:00"
|
||||||
|
|
||||||
|
|
||||||
class Timer:
|
class Timer:
|
||||||
def __init__(self):
|
def __init__(self, startTime="12:00"):
|
||||||
self.time
|
self.clock = pygame.time.Clock()
|
||||||
self.cycle
|
|
||||||
|
# Time in milliseconds updated every frame, starts counting from what we specify as a parameter
|
||||||
|
self.timePassed = self.timeToMs(startTime)
|
||||||
|
# You have to start the clock manually by calling startClock() method
|
||||||
|
self.isStarted = False
|
||||||
|
|
||||||
|
def startClock(self):
|
||||||
|
self.isStarted = True
|
||||||
|
|
||||||
|
def stopClock(self):
|
||||||
|
self.isStarted = False
|
||||||
|
|
||||||
|
# Returns a string with formatted time
|
||||||
|
def getPrettyTime(self):
|
||||||
|
# 60 times faster than real time
|
||||||
|
minutes = int(self.timePassed / 1000) % 60
|
||||||
|
hours = int(self.timePassed / 60000) % 24
|
||||||
|
|
||||||
|
# Add 0's at the beginning if necessary
|
||||||
|
prefixHr = ""
|
||||||
|
prefixMin = ""
|
||||||
|
if len(str(hours)) < 2:
|
||||||
|
prefixHr = "0"
|
||||||
|
if len(str(minutes)) < 2:
|
||||||
|
prefixMin = "0"
|
||||||
|
|
||||||
|
# Return a formatted time
|
||||||
|
return prefixHr + str(hours) + ":" + prefixMin + str(minutes)
|
||||||
|
|
||||||
|
# Returns true, if it's daytime
|
||||||
|
def isItDay(self):
|
||||||
|
if self.timeToMs(DAY_START) < self.timePassed < self.timeToMs(NIGHT_START):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Called every frame to update the timer
|
||||||
|
def updateTime(self, elapsed):
|
||||||
|
# Only happens if the time is set to be running
|
||||||
|
if self.isStarted:
|
||||||
|
# Modulo, since we use the 24-hour cycle
|
||||||
|
# In our case, the time loops every 24 minutes
|
||||||
|
self.timePassed =(self.timePassed + elapsed) % 1440000
|
||||||
|
|
||||||
|
# Converts time as string to integer milliseconds
|
||||||
|
def timeToMs(self, timeString):
|
||||||
|
timeList = timeString.split(':')
|
||||||
|
hours = timeList[0]
|
||||||
|
minutes = timeList[1]
|
||||||
|
return int(hours) * 60000 + int(minutes) * 1000
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user