mapa2 #11
175
main.py
175
main.py
@ -1,34 +1,39 @@
|
|||||||
import pygame
|
import pygame as pg
|
||||||
|
import sys
|
||||||
|
from os import path
|
||||||
from map import *
|
from map import *
|
||||||
from agent import trashmaster
|
# from agent import trashmaster
|
||||||
from house import House
|
# from house import House
|
||||||
from sprites import *
|
from sprites import *
|
||||||
|
from settings import *
|
||||||
|
|
||||||
class WalleGame():
|
class Game():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.SCREEN_SIZE = [1024, 768]
|
pg.init()
|
||||||
#self.BACKGROUND_COLOR = '#ffffff'
|
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
|
||||||
|
pg.display.set_caption("Trashmaster")
|
||||||
|
self.clock = pg.time.Clock()
|
||||||
|
self.load_data()
|
||||||
|
|
||||||
pygame.init()
|
def load_data(self):
|
||||||
pygame.display.set_caption('Wall-e')
|
game_folder = path.dirname(__file__)
|
||||||
|
img_folder = path.join(game_folder, 'resources/textures')
|
||||||
|
map_folder = path.join(img_folder, 'map')
|
||||||
self.screen = pygame.display.set_mode(self.SCREEN_SIZE)
|
self.map = TiledMap(path.join(map_folder, 'roads.tmx'))
|
||||||
# self.screen.fill(pygame.Color(self.BACKGROUND_COLOR))
|
|
||||||
|
|
||||||
# krata
|
|
||||||
# self.map = preparedMap(self.SCREEN_SIZE)
|
|
||||||
# self.screen.blit(self.map, (0,0))
|
|
||||||
self.walls = pg.sprite.Group()
|
|
||||||
self.player_img = pg.image.load("./resources/textures/garbagetruck/trashmaster_blu.png").convert_alpha()
|
|
||||||
self.all_sprites = pg.sprite.Group()
|
|
||||||
|
|
||||||
|
|
||||||
self.map = TiledMap("resources/textures/map/roads.tmx")
|
|
||||||
self.map_img = self.map.make_map()
|
self.map_img = self.map.make_map()
|
||||||
self.map_rect = self.map_img.get_rect()
|
self.map_rect = self.map_img.get_rect()
|
||||||
self.screen.blit(self.map_img, (0,0))
|
|
||||||
|
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) )
|
||||||
|
self.wall_img = pg.image.load(path.join(img_folder, WALL_IMG)).convert_alpha()
|
||||||
|
self.wall_img = pg.transform.scale(self.wall_img, (TILESIZE, TILESIZE))
|
||||||
|
|
||||||
|
def new(self):
|
||||||
|
# initialize all variables and do all the setup for a new game
|
||||||
|
|
||||||
|
self.all_sprites = pg.sprite.Group()
|
||||||
|
self.walls = pg.sprite.Group()
|
||||||
|
|
||||||
for tile_object in self.map.tmxdata.objects:
|
for tile_object in self.map.tmxdata.objects:
|
||||||
if tile_object.name == 'player':
|
if tile_object.name == 'player':
|
||||||
@ -36,54 +41,112 @@ class WalleGame():
|
|||||||
if tile_object.name == 'wall':
|
if tile_object.name == 'wall':
|
||||||
Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
|
Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
|
||||||
|
|
||||||
|
# self.screen.blit(self.map_img, (0,0))
|
||||||
|
self.camera = Camera(self.map.width, self.map.height)
|
||||||
|
self.draw_debug = False
|
||||||
|
|
||||||
|
|
||||||
# self.camera = Camera(self.map.width, self.map.height)
|
|
||||||
#self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
#self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
||||||
|
|
||||||
|
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 update_window(self):
|
def quit(self):
|
||||||
pygame.display.update()
|
pg.quit()
|
||||||
# self.camera.update(self.image)
|
sys.exit()
|
||||||
|
|
||||||
def draw_object(self, drawable_object, pos):
|
def update(self):
|
||||||
# pos => (x, y)
|
# update portion of the game loop
|
||||||
# drawable object must have .image field inside class
|
self.all_sprites.update()
|
||||||
self.screen.blit(drawable_object.image, pos )
|
self.camera.update(self.player)
|
||||||
|
# pygame.display.update()
|
||||||
|
|
||||||
|
def draw_grid(self):
|
||||||
|
for x in range(0, WIDTH, TILESIZE):
|
||||||
|
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
|
||||||
|
for y in range(0, HEIGHT, TILESIZE):
|
||||||
|
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
||||||
|
|
||||||
|
|
||||||
def reloadMap(self):
|
# def draw(self, drawable_object, pos):
|
||||||
#self.screen.fill(pygame.Color(self.BACKGROUND_COLOR))
|
# # pos => (x, y)
|
||||||
self.screen.blit(self.map_img, (0,0))
|
# # drawable object must have .image field inside class
|
||||||
|
# self.screen.blit(drawable_object.image, pos )
|
||||||
|
|
||||||
def main():
|
def draw(self):
|
||||||
game = WalleGame()
|
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
||||||
game.update_window()
|
self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
|
||||||
|
|
||||||
smieciara_object = trashmaster(16,16,"./resources/textures/garbagetruck/trashmaster_blu.png")
|
for sprite in self.all_sprites:
|
||||||
game.draw_object(smieciara_object, (0, 0))
|
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)
|
||||||
|
if self.draw_debug:
|
||||||
|
for wall in self.walls:
|
||||||
|
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(wall.rect), 1)
|
||||||
|
|
||||||
#house_object = House(20, 20)
|
pg.display.flip()
|
||||||
# Test draw house object
|
|
||||||
#game.draw_object(house_object, (20,20))
|
|
||||||
|
|
||||||
game.update_window()
|
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
|
||||||
|
|
||||||
running = True
|
def show_start_screen(self):
|
||||||
|
pass
|
||||||
|
|
||||||
while running:
|
def show_go_screen(self):
|
||||||
for event in pygame.event.get():
|
pass
|
||||||
if event.type == pygame.QUIT:
|
|
||||||
running = False
|
|
||||||
if event.type == pygame.KEYDOWN:
|
|
||||||
game.reloadMap()
|
|
||||||
game.draw_object(smieciara_object, smieciara_object.movement(event.key, 16))
|
|
||||||
|
|
||||||
game.update_window()
|
# def reloadMap(self):
|
||||||
|
# #self.screen.fill(pygame.Color(self.BACKGROUND_COLOR))
|
||||||
|
# self.screen.blit(self.map_img, (0,0))
|
||||||
|
|
||||||
pygame.quit()
|
# def main():
|
||||||
|
# game = WalleGame()
|
||||||
|
# game.update_window()
|
||||||
|
|
||||||
|
# smieciara_object = trashmaster(16,16,"./resources/textures/garbagetruck/trashmaster_blu.png")
|
||||||
|
# game.draw_object(smieciara_object, (100, 100))
|
||||||
|
|
||||||
|
# #house_object = House(20, 20)
|
||||||
|
# # Test draw house object
|
||||||
|
# #game.draw_object(house_object, (20,20))
|
||||||
|
|
||||||
|
# game.update_window()
|
||||||
|
|
||||||
|
# running = True
|
||||||
|
|
||||||
|
# while running:
|
||||||
|
# for event in pygame.event.get():
|
||||||
|
# if event.type == pygame.QUIT:
|
||||||
|
# running = False
|
||||||
|
# if event.type == pygame.KEYDOWN:
|
||||||
|
# game.reloadMap()
|
||||||
|
# game.draw_object(smieciara_object, smieciara_object.movement(event.key, 16))
|
||||||
|
|
||||||
|
# game.update_window()
|
||||||
|
|
||||||
|
# pygame.quit()
|
||||||
|
# if __name__ == '__main__':
|
||||||
|
# main()
|
||||||
|
|
||||||
|
|
||||||
|
# create the game object
|
||||||
|
g = Game()
|
||||||
|
g.show_start_screen()
|
||||||
|
while True:
|
||||||
|
g.new()
|
||||||
|
g.run()
|
||||||
|
g.show_go_screen()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
14
map.py
14
map.py
@ -1,4 +1,5 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
|
from settings import *
|
||||||
import pytmx
|
import pytmx
|
||||||
|
|
||||||
# config
|
# config
|
||||||
@ -12,6 +13,7 @@ import pytmx
|
|||||||
# for y in range(0, screenSize[1], TILE_SIZE):
|
# for y in range(0, screenSize[1], TILE_SIZE):
|
||||||
# surface.blit(tileImage, (x, y))
|
# surface.blit(tileImage, (x, y))
|
||||||
# return surface
|
# return surface
|
||||||
|
|
||||||
def collide_hit_rect(one, two):
|
def collide_hit_rect(one, two):
|
||||||
return one.hit_rect.colliderect(two.rect)
|
return one.hit_rect.colliderect(two.rect)
|
||||||
class TiledMap:
|
class TiledMap:
|
||||||
@ -46,18 +48,18 @@ class Camera:
|
|||||||
def apply(self,entity):
|
def apply(self,entity):
|
||||||
return entity.rect.move(self.camera.topleft)
|
return entity.rect.move(self.camera.topleft)
|
||||||
|
|
||||||
# def apply_rect(self, rect):
|
def apply_rect(self, rect):
|
||||||
# return rect.move(self.camera.topleft)
|
return rect.move(self.camera.topleft)
|
||||||
|
|
||||||
def update(self,target):
|
def update(self,target):
|
||||||
x = -target.rect.x + int(1024/2)
|
x = -target.rect.x + int(WIDTH/2)
|
||||||
y = -target.rect.y + int(768 / 2)
|
y = -target.rect.y + int(HEIGHT / 2)
|
||||||
|
|
||||||
# limit scrolling to map size
|
# limit scrolling to map size
|
||||||
x = min(0, x) # left
|
x = min(0, x) # left
|
||||||
y = min(0, y) # top
|
y = min(0, y) # top
|
||||||
x = max(-(self.width - 1024), x) # right
|
x = max(-(self.width - WIDTH), x) # right
|
||||||
y = max(-(self.height - 768), y) # bottom
|
y = max(-(self.height - HEIGHT), y) # bottom
|
||||||
self.camera = pg.Rect(x, y, self.width, self.height)
|
self.camera = pg.Rect(x, y, self.width, self.height)
|
||||||
|
|
||||||
|
|
||||||
|
BIN
resources/textures/garbagetruck/trashmaster_v2.png
Normal file
BIN
resources/textures/garbagetruck/trashmaster_v2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 82 KiB |
@ -72,7 +72,7 @@
|
|||||||
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11
|
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11
|
||||||
</data>
|
</data>
|
||||||
</layer>
|
</layer>
|
||||||
<layer id="4" name="Player" width="30" height="30">
|
<layer id="4" name="Player" width="30" height="30" visible="0">
|
||||||
<data encoding="csv">
|
<data encoding="csv">
|
||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
28
settings.py
Normal file
28
settings.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import pygame as pg
|
||||||
|
|
||||||
|
vec = pg.math.Vector2
|
||||||
|
|
||||||
|
#colors
|
||||||
|
LIGHTGREY = (100, 100, 100)
|
||||||
|
CYAN = (0, 255, 255)
|
||||||
|
|
||||||
|
|
||||||
|
#game settings
|
||||||
|
WIDTH = 1024
|
||||||
|
HEIGHT = 768
|
||||||
|
FPS = 60
|
||||||
|
|
||||||
|
TILESIZE = 64
|
||||||
|
GRIDWIDTH = WIDTH/TILESIZE
|
||||||
|
GRIDHEIGHT = HEIGHT/TILESIZE
|
||||||
|
|
||||||
|
|
||||||
|
WALL_IMG = 'buliding\GTA2_TILE_26.bmp'
|
||||||
|
|
||||||
|
#player settings
|
||||||
|
PLAYER_SPEED = 280
|
||||||
|
PLAYER_ROT_SPEED = 200
|
||||||
|
PLAYER_IMG = 'garbagetruck/trashmaster_v2.png'
|
||||||
|
PLAYER_HIT_RECT = pg.Rect(0, 0, 50, 50)
|
||||||
|
PLAYER_WIDTH = 64
|
||||||
|
PLAYER_HEIGHT = 32
|
87
sprites.py
87
sprites.py
@ -1,4 +1,6 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
|
import pygame.image
|
||||||
|
from settings import *
|
||||||
from random import uniform
|
from random import uniform
|
||||||
from map import collide_hit_rect
|
from map import collide_hit_rect
|
||||||
|
|
||||||
@ -32,8 +34,9 @@ class Player(pg.sprite.Sprite):
|
|||||||
self.game = game
|
self.game = game
|
||||||
self.image = game.player_img
|
self.image = game.player_img
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
# self.hit_rect = PLAYER_HIT_RECT
|
self.rect.center = (x,y)
|
||||||
# self.hit_rect.center = self.rect.center
|
self.hit_rect = PLAYER_HIT_RECT
|
||||||
|
self.hit_rect.center = self.rect.center
|
||||||
self.vel = vec(0, 0)
|
self.vel = vec(0, 0)
|
||||||
self.pos = vec(x, y)
|
self.pos = vec(x, y)
|
||||||
self.rot = 0
|
self.rot = 0
|
||||||
@ -66,6 +69,86 @@ class Player(pg.sprite.Sprite):
|
|||||||
collide_with_walls(self, self.game.walls, 'y')
|
collide_with_walls(self, self.game.walls, 'y')
|
||||||
self.rect.center = self.hit_rect.center
|
self.rect.center = self.hit_rect.center
|
||||||
|
|
||||||
|
class Dump(pg.sprite.Sprite):
|
||||||
|
# wysypisko
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.glass = []
|
||||||
|
self.paper = []
|
||||||
|
self.bio = []
|
||||||
|
self.other_trash = []
|
||||||
|
class trash(pg.sprite.Sprite):
|
||||||
|
|
||||||
|
def __init__(self,x,y,img, type):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.width=16
|
||||||
|
self.height=16
|
||||||
|
|
||||||
|
self.type = type
|
||||||
|
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
self.image = pygame.image.load(img)
|
||||||
|
self.image = pygame.transform.scale(self.image, (self.x,self.y))
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
class trashbin(pg.sprite.Sprite):
|
||||||
|
|
||||||
|
def __init__(self,x,y,img, type):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
# trashbin type
|
||||||
|
self.type = type
|
||||||
|
|
||||||
|
# dimensions
|
||||||
|
if type == "small":
|
||||||
|
self.width = 4
|
||||||
|
self.height = 4
|
||||||
|
elif type == "medium":
|
||||||
|
self.width = 8
|
||||||
|
self.height = 8
|
||||||
|
elif type == "large":
|
||||||
|
self.width = 16
|
||||||
|
self.height = 16
|
||||||
|
|
||||||
|
# spawn coords
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
# load trashbin image
|
||||||
|
self.image = pygame.image.load(img)
|
||||||
|
self.image = pygame.transform.scale(self.image, (self.x,self.y))
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
|
||||||
|
class trashmaster(pg.sprite.Sprite):
|
||||||
|
|
||||||
|
def __init__(self,x,y,img):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.width=x
|
||||||
|
self.height=y
|
||||||
|
|
||||||
|
self.x = 0
|
||||||
|
self.y = 0
|
||||||
|
|
||||||
|
self.image = pygame.image.load(img)
|
||||||
|
self.image = pygame.transform.scale(self.image, (self.width,self.height))
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
|
||||||
|
def movement(self, key, vel):
|
||||||
|
if key == pygame.K_LEFT:
|
||||||
|
self.x -= vel
|
||||||
|
|
||||||
|
if key == pygame.K_RIGHT:
|
||||||
|
self.x += vel
|
||||||
|
|
||||||
|
if key == pygame.K_UP:
|
||||||
|
self.y -= vel
|
||||||
|
|
||||||
|
if key == pygame.K_DOWN:
|
||||||
|
self.y += vel
|
||||||
|
return (self.x, self.y)
|
||||||
|
|
||||||
class Obstacle(pg.sprite.Sprite):
|
class Obstacle(pg.sprite.Sprite):
|
||||||
def __init__(self, game, x, y, w, h):
|
def __init__(self, game, x, y, w, h):
|
||||||
|
Loading…
Reference in New Issue
Block a user