2022-04-07 20:06:09 +02:00
|
|
|
from calendar import c
|
2022-04-23 19:14:23 +02:00
|
|
|
from game_objects.player import Player
|
2022-04-04 11:40:18 +02:00
|
|
|
import pygame as pg
|
|
|
|
import sys
|
|
|
|
from os import path
|
|
|
|
# from agent import trashmaster
|
|
|
|
# from house import House
|
|
|
|
from settings import *
|
2022-04-23 19:14:23 +02:00
|
|
|
from map import map
|
|
|
|
from map import map_utils
|
2022-04-07 20:06:09 +02:00
|
|
|
import math
|
2022-03-10 15:53:41 +01:00
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
class Game():
|
2022-03-10 16:16:59 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
2022-04-04 11:40:18 +02:00
|
|
|
pg.init()
|
2022-04-23 19:14:23 +02:00
|
|
|
self.clock = pg.time.Clock()
|
2022-04-04 11:40:18 +02:00
|
|
|
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
pg.display.set_caption("Trashmaster")
|
|
|
|
self.load_data()
|
2022-04-23 19:14:23 +02:00
|
|
|
self.init_game()
|
2022-04-04 11:40:18 +02:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
def init_game(self):
|
2022-04-04 11:40:18 +02:00
|
|
|
# initialize all variables and do all the setup for a new game
|
|
|
|
|
2022-04-08 02:17:02 +02:00
|
|
|
# sprite groups
|
2022-04-23 19:14:23 +02:00
|
|
|
self.roadTiles, self.wallTiles = map.get_tiles()
|
|
|
|
self.agentSprites = pg.sprite.Group()
|
2022-04-08 01:34:11 +02:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
# player obj
|
|
|
|
self.player = Player(self, 32, 100)
|
2022-04-03 15:47:41 +02:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
# camera obj
|
|
|
|
self.camera = map_utils.Camera(MAP_WIDTH_PX, MAP_HEIGHT_PX)
|
2022-04-03 15:47:41 +02:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
# other
|
2022-04-04 11:40:18 +02:00
|
|
|
self.draw_debug = False
|
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
def load_data(self):
|
|
|
|
game_folder = path.dirname(__file__)
|
|
|
|
img_folder = path.join(game_folder, 'resources/textures')
|
|
|
|
|
|
|
|
self.player_img = pg.image.load(path.join(img_folder,PLAYER_IMG)).convert_alpha()
|
|
|
|
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH,PLAYER_HEIGHT) )
|
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
def run(self):
|
|
|
|
# game loop - set self.playing = False to end the game
|
|
|
|
self.playing = True
|
|
|
|
while self.playing:
|
|
|
|
self.dt = self.clock.tick(FPS) / 1000.0
|
|
|
|
self.events()
|
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
pg.quit()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
# update portion of the game loop
|
2022-04-23 19:14:23 +02:00
|
|
|
self.agentSprites.update()
|
2022-04-04 11:40:18 +02:00
|
|
|
self.camera.update(self.player)
|
|
|
|
# pygame.display.update()
|
2022-04-03 15:47:41 +02:00
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
def draw(self):
|
2022-04-23 19:14:23 +02:00
|
|
|
#display fps as window title
|
2022-04-04 11:40:18 +02:00
|
|
|
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
2022-04-08 02:17:02 +02:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
#rerender map
|
|
|
|
map.render_tiles(self.roadTiles, self.screen, self.camera)
|
|
|
|
map.render_tiles(self.wallTiles, self.screen, self.camera, self.draw_debug)
|
|
|
|
|
|
|
|
#rerender additional sprites
|
|
|
|
for sprite in self.agentSprites:
|
2022-04-04 11:40:18 +02:00
|
|
|
self.screen.blit(sprite.image, self.camera.apply(sprite))
|
|
|
|
if self.draw_debug:
|
|
|
|
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
|
2022-03-10 16:16:59 +01:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
#finally update screen
|
2022-04-04 11:40:18 +02:00
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
def events(self):
|
|
|
|
for event in pg.event.get():
|
|
|
|
if event.type == pg.QUIT:
|
|
|
|
self.quit()
|
|
|
|
if event.type == pg.KEYDOWN:
|
|
|
|
if event.key == pg.K_ESCAPE:
|
|
|
|
self.quit()
|
|
|
|
if event.key == pg.K_h:
|
|
|
|
self.draw_debug = not self.draw_debug
|
2022-04-23 19:14:23 +02:00
|
|
|
if event.type == pg.MOUSEBUTTONUP:
|
|
|
|
pos = pg.mouse.get_pos()
|
2022-04-07 20:06:09 +02:00
|
|
|
clicked_coords = [math.floor(pos[0] / TILESIZE), math.floor(pos[1] / TILESIZE)]
|
|
|
|
print(clicked_coords)
|
2022-04-04 11:40:18 +02:00
|
|
|
|
|
|
|
def show_start_screen(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def show_go_screen(self):
|
|
|
|
pass
|
2022-03-09 21:27:57 +01:00
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
# create the game object
|
|
|
|
g = Game()
|
|
|
|
g.show_start_screen()
|
|
|
|
while True:
|
|
|
|
g.run()
|
|
|
|
g.show_go_screen()
|
2022-03-10 16:16:59 +01:00
|
|
|
|