diff --git a/main/src/__pycache__/settings.cpython-38.pyc b/main/src/__pycache__/settings.cpython-38.pyc new file mode 100644 index 0000000..174633b Binary files /dev/null and b/main/src/__pycache__/settings.cpython-38.pyc differ diff --git a/main/src/__pycache__/trash.cpython-38.pyc b/main/src/__pycache__/trash.cpython-38.pyc new file mode 100644 index 0000000..4292d04 Binary files /dev/null and b/main/src/__pycache__/trash.cpython-38.pyc differ diff --git a/main/src/__pycache__/truck.cpython-38.pyc b/main/src/__pycache__/truck.cpython-38.pyc new file mode 100644 index 0000000..fb084ea Binary files /dev/null and b/main/src/__pycache__/truck.cpython-38.pyc differ diff --git a/main/src/__pycache__/truck1.cpython-38.pyc b/main/src/__pycache__/truck1.cpython-38.pyc new file mode 100644 index 0000000..fe5f924 Binary files /dev/null and b/main/src/__pycache__/truck1.cpython-38.pyc differ diff --git a/main/src/__pycache__/walls.cpython-38.pyc b/main/src/__pycache__/walls.cpython-38.pyc new file mode 100644 index 0000000..06678cc Binary files /dev/null and b/main/src/__pycache__/walls.cpython-38.pyc differ diff --git a/main/src/img/house.png b/main/src/img/house.png new file mode 100644 index 0000000..909c694 Binary files /dev/null and b/main/src/img/house.png differ diff --git a/main/src/img/house1.png b/main/src/img/house1.png new file mode 100644 index 0000000..cf9c5be Binary files /dev/null and b/main/src/img/house1.png differ diff --git a/main/src/img/trash.png b/main/src/img/trash.png new file mode 100644 index 0000000..ce64e37 Binary files /dev/null and b/main/src/img/trash.png differ diff --git a/main/src/img/trash1.png b/main/src/img/trash1.png new file mode 100644 index 0000000..fd4e1f6 Binary files /dev/null and b/main/src/img/trash1.png differ diff --git a/main/src/img/truck.png b/main/src/img/truck.png new file mode 100644 index 0000000..7860105 Binary files /dev/null and b/main/src/img/truck.png differ diff --git a/main/src/main.py b/main/src/main.py new file mode 100644 index 0000000..8497f93 --- /dev/null +++ b/main/src/main.py @@ -0,0 +1,84 @@ +import pygame as pg +import sys +from os import path +from trash import * +from truck1 import * +from walls import * +from settings import * + +class Game: + def __init__(self): + pg.init() + self.screen = pg.display.set_mode((WIDTH, HEIGHT)) + pg.display.set_caption(TITLE) + self.clock = pg.time.Clock() + pg.key.set_repeat(500, 100) + self.load_data() + + def load_data(self): + game_folder = path.dirname(__file__) + self.map_data = [] + with open(path.join(game_folder, 'map.txt'), 'rt') as f: + for line in f: + self.map_data.append(line) + + def new(self): + self.all_sprites = pg.sprite.Group() + self.walls = pg.sprite.Group() + self.trash = pg.sprite.Group() + self.player = pg.sprite.Group() + for row, tiles in enumerate(self.map_data): + for col, tile in enumerate(tiles): + if tile == 'b': + Wall(self, col, row) + if tile == 'w': + Wall1(self, col,row) + if tile == 'P': + garbageTruck(self, col, row) + if tile == 't': + Trash(self, col, row) + + def run(self): + self.playing = True + while self.playing: + self.dt = self.clock.tick(40) / 1000 + self.events() + self.update() + self.draw() + + def quit(self): + pg.quit() + sys.exit() + + def update(self): + self.all_sprites.update() + + def draw(self): + bg = pg.image.load("img\\bg.png") + self.screen.blit(bg,(0,0)) + self.screen.fill(BGCOLOR) + self.all_sprites.draw(self.screen) + pg.display.flip() + + def events(self): + # catch all events here + 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() + + def show_start_screen(self): + pass + + def show_go_screen(self): + pass + +g = Game() +g.show_start_screen() +while True: + g.new() + g.run() + g.show_go_screen() + diff --git a/main/src/map.txt b/main/src/map.txt new file mode 100644 index 0000000..a3b6600 --- /dev/null +++ b/main/src/map.txt @@ -0,0 +1,18 @@ +bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +bbbbb bbb bbb bb bb b bb +b t w b +b w w b +b wwwwww wwwww bbb +bbb w w w w b +b w t w b +b wwwww wwwwwww t bb +b wwwwww w w b +b t w w www wwww bbbb +b w w w b +b wwwww www www w b +b w P ww w b +b t b +b w www w www w ww b +b w wwww bb +b bbbb b b bb t b +bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb \ No newline at end of file diff --git a/main/src/settings.py b/main/src/settings.py new file mode 100644 index 0000000..6533bbd --- /dev/null +++ b/main/src/settings.py @@ -0,0 +1,7 @@ +WIDTH = 1280 +HEIGHT = 720 +TITLE = "Smieciarka" +TILESIZE = 40 +DARKGREY = (40, 40, 40) +BGCOLOR = DARKGREY +TRUCK_SPEED = 240 diff --git a/main/src/trash.py b/main/src/trash.py new file mode 100644 index 0000000..888432c --- /dev/null +++ b/main/src/trash.py @@ -0,0 +1,32 @@ +import random +import pygame as pg +from settings import * +class Trash(pg.sprite.Sprite): + def __init__(self, game, x, y): + image = pg.image.load("img\\trash.png") + image1 = pg.image.load("img\\trash1.png") + self.groups = game.all_sprites, game.trash + pg.sprite.Sprite.__init__(self, self.groups) + self.game = game + self.image = pg.Surface((TILESIZE, TILESIZE)) + self.rect = self.image.get_rect() + self.image.blit(image,(0,0)) + self.x = x + self.y = y + self.rect.x = x * TILESIZE + self.rect.y = y * TILESIZE + self.position = (x,y) + self.randomize_position() + + def randomize_position(self): + self.position = (random.randint(0, 32-1)*40, random.randint(0, 18-1)*40) + + def collected(self): + hits = pg.sprite.spritecollide(self, self.game.player, False) + if hits: + self.image.blit(image1,(0,0)) + self.x=0 + self.y=0 + + def update(self): + self.collected() diff --git a/main/src/truck.py b/main/src/truck.py new file mode 100644 index 0000000..9c8f817 --- /dev/null +++ b/main/src/truck.py @@ -0,0 +1,83 @@ +import pygame as pg +from settings import * +class garbageTruck(pg.sprite.Sprite): + def __init__(self, game, x, y): + image = pg.image.load("img\\truck.png") + self.groups = game.all_sprites + pg.sprite.Sprite.__init__(self, self.groups) + self.game = game + self.image = pg.Surface((TILESIZE, TILESIZE)) + self.image.blit(image,(0,0)) + self.rect = self.image.get_rect() + self.capacity = 0 + self.vx, self.vy = 0, 0 + self.max_capacity=3000 + self.x=x*TILESIZE + self.y=y*TILESIZE + self.position=[((self.x),(self.y))] + self.direction=0 + + def get_position(self): + return self.position[0] + + def turn(self): + if self.direction==0: + self.direction=1 + else: + self.direction=0 + + def move(self, r): + current = self.get_position() + if self.direction==0: + new = (((current[0]+(r*40))%1280),current[1]%720) + else: + new = (current[0]%1280,(current[1]+(r*40))%720) + self.position.insert(0,new) + self.position.pop() + + def throw(self): + self.capacity=0 + + + def handle_keys(self): + keys = pg.key.get_pressed() + if keys[pg.K_LEFT] or keys[pg.K_a]: + if self.direction == 0: + self.move(-1) + else: self.turn() + elif keys[pg.K_RIGHT] or keys[pg.K_d]: + if self.direction == 0: + self.move(1) + else: self.turn() + elif keys[pg.K_UP] or keys[pg.K_w]: + if self.direction == 1: + self.move(-1) + else: self.turn() + elif keys[pg.K_DOWN] or keys[pg.K_s]: + if self.direction == 1: + self.move(1) + else: self.turn() + + def get_keys(self): + self.vx, self.vy = 0, 0 + keys = pg.key.get_pressed() + if keys[pg.K_LEFT] or keys[pg.K_a]: + self.vx = -TRUCK_SPEED + if keys[pg.K_RIGHT] or keys[pg.K_d]: + self.vx = TRUCK_SPEED + if keys[pg.K_UP] or keys[pg.K_w]: + self.vy = -TRUCK_SPEED + if keys[pg.K_DOWN] or keys[pg.K_s]: + self.vy = TRUCK_SPEED + if self.vx != 0 and self.vy != 0: + self.vx *= 1 + self.vy *= 1 + + def update(self): + self.get_keys() + self.x += self.vx * self.game.dt + self.y += self.vy * self.game.dt + + + + diff --git a/main/src/truck1.py b/main/src/truck1.py new file mode 100644 index 0000000..1c63dcf --- /dev/null +++ b/main/src/truck1.py @@ -0,0 +1,110 @@ +import pygame as pg +from settings import * +class garbageTruck(pg.sprite.Sprite): + def __init__(self, game, x, y): + image = pg.image.load("img\\truck.png") + self.groups = game.all_sprites, game.player + pg.sprite.Sprite.__init__(self, self.groups) + self.game = game + self.image = pg.Surface((TILESIZE, TILESIZE)) + self.image.blit(image,(0,0)) + self.rect = self.image.get_rect() + self.vx, self.vy = 0, 0 + self.x = x * TILESIZE + self.y = y * TILESIZE + self.capacity = 0 + self.max_capacity=3000 + self.position=[((self.x),(self.y))] + self.direction=0 + + def get_position(self): + return self.position[0] + + def turn(self): + if self.direction==0: + self.direction=1 + else: + self.direction=0 + + def move(self, r): + if self.direction==0: + self.vx=(TRUCK_SPEED*r) + else: + self.vy=(TRUCK_SPEED*r) + + def throw(self): + self.capacity=0 + + def get_keys(self): + self.vx, self.vy = 0, 0 + keys = pg.key.get_pressed() + if keys[pg.K_LEFT] or keys[pg.K_a]: + if self.direction == 0: + self.move(-1) + else: self.turn() + if keys[pg.K_RIGHT] or keys[pg.K_d]: + if self.direction == 0: + self.move(1) + else: self.turn() + if keys[pg.K_UP] or keys[pg.K_w]: + if self.direction == 1: + self.move(-1) + else: self.turn() + if keys[pg.K_DOWN] or keys[pg.K_s]: + if self.direction == 1: + self.move(1) + else: self.turn() + if self.vx != 0 and self.vy != 0: + self.vx *= 1 + self.vy *= 1 + + def collide_with_walls(self, dir): + if dir == 'x': + hits = pg.sprite.spritecollide(self, self.game.walls, False) + if hits: + if self.vx > 0: + self.x = hits[0].rect.left - self.rect.width + if self.vx < 0: + self.x = hits[0].rect.right + self.vx = 0 + self.rect.x = self.x + if dir == 'y': + hits = pg.sprite.spritecollide(self, self.game.walls, False) + if hits: + if self.vy > 0: + self.y = hits[0].rect.top - self.rect.height + if self.vy < 0: + self.y = hits[0].rect.bottom + self.vy = 0 + self.rect.y = self.y + + def collecting_trash(self, dir): + if dir == 'x': + hits = pg.sprite.spritecollide(self, self.game.trash, False) + if hits: + if self.vx > 0: + self.x = hits[0].rect.left - self.rect.width + if self.vx < 0: + self.x = hits[0].rect.right + self.vx = 0 + self.rect.x = self.x + if dir == 'y': + hits = pg.sprite.spritecollide(self, self.game.trash, False) + if hits: + if self.vy > 0: + self.y = hits[0].rect.top - self.rect.height + if self.vy < 0: + self.y = hits[0].rect.bottom + self.vy = 0 + self.rect.y = self.y + + def update(self): + self.get_keys() + self.x += self.vx * self.game.dt + self.y += self.vy * self.game.dt + self.rect.x = self.x + self.collide_with_walls('x') + self.collecting_trash('x') + self.rect.y = self.y + self.collide_with_walls('y') + self.collecting_trash('y') diff --git a/main/src/walls.py b/main/src/walls.py new file mode 100644 index 0000000..968c72f --- /dev/null +++ b/main/src/walls.py @@ -0,0 +1,30 @@ +import pygame as pg +from settings import * + +class Wall(pg.sprite.Sprite): + def __init__(self, game, x, y): + image = pg.image.load("img\\house1.png") + self.groups = game.all_sprites, game.walls + pg.sprite.Sprite.__init__(self, self.groups) + self.game = game + self.image = pg.Surface((TILESIZE, TILESIZE)) + self.rect = self.image.get_rect() + self.image.blit(image,(0,0)) + self.x = x + self.y = y + self.rect.x = x * TILESIZE + self.rect.y = y * TILESIZE + +class Wall1(pg.sprite.Sprite): + def __init__(self, game, x, y): + image = pg.image.load("img\\house.png") + self.groups = game.all_sprites, game.walls + pg.sprite.Sprite.__init__(self, self.groups) + self.game = game + self.image = pg.Surface((TILESIZE, TILESIZE)) + self.rect = self.image.get_rect() + self.image.blit(image,(-1,-1)) + self.x = x + self.y = y + self.rect.x = x * TILESIZE + self.rect.y = y * TILESIZE