prawie bfs
This commit is contained in:
parent
6c4176ab7f
commit
f79a2d91d9
BIN
main/src/__pycache__/bfs.cpython-38.pyc
Normal file
BIN
main/src/__pycache__/bfs.cpython-38.pyc
Normal file
Binary file not shown.
BIN
main/src/__pycache__/bfs.cpython-39.pyc
Normal file
BIN
main/src/__pycache__/bfs.cpython-39.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
main/src/__pycache__/settings.cpython-39.pyc
Normal file
BIN
main/src/__pycache__/settings.cpython-39.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
main/src/__pycache__/trash.cpython-39.pyc
Normal file
BIN
main/src/__pycache__/trash.cpython-39.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
main/src/__pycache__/truck1.cpython-39.pyc
Normal file
BIN
main/src/__pycache__/truck1.cpython-39.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
main/src/__pycache__/walls.cpython-39.pyc
Normal file
BIN
main/src/__pycache__/walls.cpython-39.pyc
Normal file
Binary file not shown.
75
main/src/bfs.py
Normal file
75
main/src/bfs.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import pygame as pg
|
||||||
|
import sys
|
||||||
|
from os import path
|
||||||
|
from collections import deque
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
|
def bfs():
|
||||||
|
class Map:
|
||||||
|
def __init__(self, x, y, state):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.pos = [self.x, self.y]
|
||||||
|
self.nodes = []
|
||||||
|
self.prev = None
|
||||||
|
self.wall = False if state not in ('b', 'w') else True
|
||||||
|
self.visited = False
|
||||||
|
|
||||||
|
def add_nodes(self, arr):
|
||||||
|
if self.x < len(map_raw)-1:
|
||||||
|
self.nodes.append(arr[self.x+1][self.y])
|
||||||
|
if self.x > 0:
|
||||||
|
self.nodes.append(arr[self.x-1][self.y])
|
||||||
|
if self.y < len(map_raw[0])-1:
|
||||||
|
self.nodes.append(arr[self.x][self.y+1])
|
||||||
|
if self.y > 0:
|
||||||
|
self.nodes.append(arr[self.x][self.y-1])
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'{self.x}, {self.y}'
|
||||||
|
|
||||||
|
game_path = path.join(path.dirname(__file__), 'map.txt')
|
||||||
|
|
||||||
|
map_raw = [line.strip() for line in open(game_path, 'rt')]
|
||||||
|
map_arr = []
|
||||||
|
queue = deque()
|
||||||
|
bfs_path = []
|
||||||
|
|
||||||
|
truck = [0, 0]
|
||||||
|
trash = [0, 0]
|
||||||
|
|
||||||
|
for x in range(len(map_raw)):
|
||||||
|
temp = []
|
||||||
|
for y in range(len(map_raw[0])):
|
||||||
|
temp.append(Map(x, y, map_raw[x][y]))
|
||||||
|
if map_raw[x][y] == 'P':
|
||||||
|
truck = temp[-1]
|
||||||
|
if map_raw[x][y] == 't':
|
||||||
|
trash = temp[-1]
|
||||||
|
map_arr.append(temp)
|
||||||
|
|
||||||
|
for x in range(len(map_raw)):
|
||||||
|
for y in range(len(map_raw[0])):
|
||||||
|
map_arr[x][y].add_nodes(map_arr)
|
||||||
|
|
||||||
|
queue.append(truck)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if len(queue) > 0:
|
||||||
|
cur_node = queue.popleft()
|
||||||
|
cur_node.visited = True
|
||||||
|
if cur_node == trash:
|
||||||
|
temp = cur_node
|
||||||
|
while temp.prev:
|
||||||
|
bfs_path.append(temp.prev)
|
||||||
|
temp = temp.prev
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
for node in cur_node.nodes:
|
||||||
|
if not node.visited and not node.wall:
|
||||||
|
node.visited = True
|
||||||
|
node.prev = cur_node
|
||||||
|
queue.append(node)
|
||||||
|
|
||||||
|
return [node.pos for node in bfs_path[:-1]]
|
BIN
main/src/img/bg.png
Normal file
BIN
main/src/img/bg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.7 KiB |
BIN
main/src/img/truck — kopia.png
Normal file
BIN
main/src/img/truck — kopia.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.4 KiB |
Binary file not shown.
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.4 KiB |
@ -6,6 +6,7 @@ from truck1 import *
|
|||||||
from walls import *
|
from walls import *
|
||||||
from settings import *
|
from settings import *
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pg.init()
|
pg.init()
|
||||||
@ -20,7 +21,8 @@ class Game:
|
|||||||
self.map_data = []
|
self.map_data = []
|
||||||
with open(path.join(game_folder, 'map.txt'), 'rt') as f:
|
with open(path.join(game_folder, 'map.txt'), 'rt') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
self.map_data.append(line)
|
self.map_data.append(line.strip())
|
||||||
|
print(line.strip())
|
||||||
|
|
||||||
def new(self):
|
def new(self):
|
||||||
self.all_sprites = pg.sprite.Group()
|
self.all_sprites = pg.sprite.Group()
|
||||||
@ -32,7 +34,7 @@ class Game:
|
|||||||
if tile == 'b':
|
if tile == 'b':
|
||||||
Wall(self, col, row)
|
Wall(self, col, row)
|
||||||
if tile == 'w':
|
if tile == 'w':
|
||||||
Wall1(self, col,row)
|
Wall1(self, col, row)
|
||||||
if tile == 'P':
|
if tile == 'P':
|
||||||
garbageTruck(self, col, row)
|
garbageTruck(self, col, row)
|
||||||
if tile == 't':
|
if tile == 't':
|
||||||
@ -55,7 +57,7 @@ class Game:
|
|||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
bg = pg.image.load("img\\bg.png")
|
bg = pg.image.load("img\\bg.png")
|
||||||
self.screen.blit(bg,(0,0))
|
self.screen.blit(bg, (0, 0))
|
||||||
self.screen.fill(BGCOLOR)
|
self.screen.fill(BGCOLOR)
|
||||||
self.all_sprites.draw(self.screen)
|
self.all_sprites.draw(self.screen)
|
||||||
pg.display.flip()
|
pg.display.flip()
|
||||||
@ -75,10 +77,10 @@ class Game:
|
|||||||
def show_go_screen(self):
|
def show_go_screen(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
g = Game()
|
g = Game()
|
||||||
g.show_start_screen()
|
g.show_start_screen()
|
||||||
while True:
|
while True:
|
||||||
g.new()
|
g.new()
|
||||||
g.run()
|
g.run()
|
||||||
g.show_go_screen()
|
g.show_go_screen()
|
||||||
|
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
||||||
bbbbb bbb bbb bb bb b bb
|
bbbbb bbb bbb bb bb b bb
|
||||||
b t w b
|
b w b
|
||||||
b w w b
|
b w t w b
|
||||||
b wwwwww wwwww bbb
|
b wwwwww wwwww bbb
|
||||||
bbb w w w w b
|
bbb w w w w b
|
||||||
b w t w b
|
b w w b
|
||||||
b wwwww wwwwwww t bb
|
b wwwww wwwwwww bb
|
||||||
b wwwwww w w b
|
b wwwwww w w b
|
||||||
b t w w www wwww bbbb
|
b w w www wwww bbbb
|
||||||
b w w w b
|
b w w w b
|
||||||
b wwwww www www w b
|
b wwwww www www w b
|
||||||
b w P ww w b
|
b w ww w b
|
||||||
b t b
|
b P b
|
||||||
b w www w www w ww b
|
b w www w www w ww b
|
||||||
b w wwww bb
|
b w wwww bb
|
||||||
b bbbb b b bb t b
|
b bbbb b b bb b
|
||||||
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
|
@ -1,6 +1,8 @@
|
|||||||
import random
|
import random
|
||||||
import pygame as pg
|
import pygame as pg
|
||||||
from settings import *
|
from settings import TILESIZE
|
||||||
|
|
||||||
|
|
||||||
class Trash(pg.sprite.Sprite):
|
class Trash(pg.sprite.Sprite):
|
||||||
def __init__(self, game, x, y):
|
def __init__(self, game, x, y):
|
||||||
image = pg.image.load("img\\trash.png")
|
image = pg.image.load("img\\trash.png")
|
||||||
@ -10,23 +12,24 @@ class Trash(pg.sprite.Sprite):
|
|||||||
self.game = game
|
self.game = game
|
||||||
self.image = pg.Surface((TILESIZE, TILESIZE))
|
self.image = pg.Surface((TILESIZE, TILESIZE))
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
self.image.blit(image,(0,0))
|
self.image.blit(image, (0, 0))
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.rect.x = x * TILESIZE
|
self.rect.x = x * TILESIZE
|
||||||
self.rect.y = y * TILESIZE
|
self.rect.y = y * TILESIZE
|
||||||
self.position = (x,y)
|
self.position = (x, y)
|
||||||
self.randomize_position()
|
self.randomize_position()
|
||||||
|
|
||||||
def randomize_position(self):
|
def randomize_position(self):
|
||||||
self.position = (random.randint(0, 32-1)*40, random.randint(0, 18-1)*40)
|
self.position = (random.randint(0, 32-1)*40,
|
||||||
|
random.randint(0, 18-1)*40)
|
||||||
|
|
||||||
def collected(self):
|
def collected(self):
|
||||||
hits = pg.sprite.spritecollide(self, self.game.player, False)
|
hits = pg.sprite.spritecollide(self, self.game.player, False)
|
||||||
if hits:
|
if hits:
|
||||||
self.image.blit(image1,(0,0))
|
self.image.blit(image1, (0, 0))
|
||||||
self.x=0
|
self.x = 0
|
||||||
self.y=0
|
self.y = 0
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.collected()
|
self.collected()
|
||||||
|
@ -1,62 +1,63 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
from settings import *
|
from settings import TILESIZE, TRUCK_SPEED
|
||||||
|
from bfs import bfs
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
class garbageTruck(pg.sprite.Sprite):
|
class garbageTruck(pg.sprite.Sprite):
|
||||||
def __init__(self, game, x, y):
|
def __init__(self, game, x, y):
|
||||||
image = pg.image.load("img\\truck.png")
|
self.og_image = pg.image.load("img\\truck.png")
|
||||||
self.groups = game.all_sprites, game.player
|
self.groups = game.all_sprites, game.player
|
||||||
pg.sprite.Sprite.__init__(self, self.groups)
|
pg.sprite.Sprite.__init__(self, self.groups)
|
||||||
self.game = game
|
self.game = game
|
||||||
self.image = pg.Surface((TILESIZE, TILESIZE))
|
self.image = pg.Surface((TILESIZE, TILESIZE))
|
||||||
self.image.blit(image,(0,0))
|
self.image.blit(self.og_image, (0, 0))
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
self.vx, self.vy = 0, 0
|
self.vx = 0
|
||||||
|
self.vy = 0
|
||||||
self.x = x * TILESIZE
|
self.x = x * TILESIZE
|
||||||
self.y = y * TILESIZE
|
self.y = y * TILESIZE
|
||||||
self.capacity = 0
|
self.capacity = 0
|
||||||
self.max_capacity=3000
|
self.max_capacity = 3000
|
||||||
self.position=[((self.x),(self.y))]
|
self.position = [self.x, self.y]
|
||||||
self.direction=0
|
self.directions = [-1, 1, 1, -1] # góra prawo dół lewo
|
||||||
|
self.d = 1
|
||||||
|
self.turned = False
|
||||||
|
self.bfs_path = bfs()
|
||||||
|
|
||||||
def get_position(self):
|
def get_position(self):
|
||||||
return self.position[0]
|
return [self.x, self.y]
|
||||||
|
|
||||||
def turn(self):
|
def turn(self, d):
|
||||||
if self.direction==0:
|
self.og_image = pg.transform.rotate(
|
||||||
self.direction=1
|
self.og_image, 90*d*-1)
|
||||||
else:
|
self.image.blit(self.og_image, (0, 0))
|
||||||
self.direction=0
|
self.d = (self.d+d) % 4
|
||||||
|
|
||||||
def move(self, r):
|
def move(self):
|
||||||
if self.direction==0:
|
if self.d in [1, 3]:
|
||||||
self.vx=(TRUCK_SPEED*r)
|
self.vx = (TRUCK_SPEED*self.directions[self.d])
|
||||||
else:
|
else:
|
||||||
self.vy=(TRUCK_SPEED*r)
|
self.vy = (TRUCK_SPEED*self.directions[self.d])
|
||||||
|
|
||||||
def throw(self):
|
def throw(self):
|
||||||
self.capacity=0
|
self.capacity = 0
|
||||||
|
|
||||||
def get_keys(self):
|
def get_keys(self):
|
||||||
self.vx, self.vy = 0, 0
|
self.vx, self.vy = 0, 0
|
||||||
keys = pg.key.get_pressed()
|
keys = pg.key.get_pressed()
|
||||||
if keys[pg.K_LEFT] or keys[pg.K_a]:
|
if not self.turned and (keys[pg.K_LEFT] or keys[pg.K_a]):
|
||||||
if self.direction == 0:
|
self.turn(-1)
|
||||||
self.move(-1)
|
self.turned = True
|
||||||
else: self.turn()
|
if not self.turned and (keys[pg.K_RIGHT] or keys[pg.K_d]):
|
||||||
if keys[pg.K_RIGHT] or keys[pg.K_d]:
|
self.turn(1)
|
||||||
if self.direction == 0:
|
self.turned = True
|
||||||
self.move(1)
|
if not (keys[pg.K_RIGHT] or keys[pg.K_d] or keys[pg.K_LEFT] or keys[pg.K_a]) and self.turned:
|
||||||
else: self.turn()
|
self.turned = False
|
||||||
if keys[pg.K_UP] or keys[pg.K_w]:
|
if keys[pg.K_UP] or keys[pg.K_w]:
|
||||||
if self.direction == 1:
|
self.move()
|
||||||
self.move(-1)
|
if keys[pg.K_g] and len(self.bfs_path) != 0:
|
||||||
else: self.turn()
|
self.navigate_bfs()
|
||||||
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):
|
def collide_with_walls(self, dir):
|
||||||
if dir == 'x':
|
if dir == 'x':
|
||||||
@ -98,6 +99,33 @@ class garbageTruck(pg.sprite.Sprite):
|
|||||||
self.vy = 0
|
self.vy = 0
|
||||||
self.rect.y = self.y
|
self.rect.y = self.y
|
||||||
|
|
||||||
|
def auto_drive(self, direction):
|
||||||
|
while self.d != direction:
|
||||||
|
self.turn(1)
|
||||||
|
|
||||||
|
if direction in [1, 3]:
|
||||||
|
self.x += self.directions[self.d] * TILESIZE
|
||||||
|
else:
|
||||||
|
self.y += self.directions[self.d] * TILESIZE
|
||||||
|
|
||||||
|
def navigate_bfs(self):
|
||||||
|
print(self.bfs_path)
|
||||||
|
dest = self.bfs_path.pop()
|
||||||
|
dest[0], dest[1] = dest[1]*TILESIZE, dest[0]*TILESIZE
|
||||||
|
|
||||||
|
pos = [int(i) for i in self.get_position()]
|
||||||
|
print(pos[0] - dest[0], pos[1] - dest[1])
|
||||||
|
|
||||||
|
if pos[0] - dest[0] < 0:
|
||||||
|
self.auto_drive(1)
|
||||||
|
elif pos[0] - dest[0] > 0:
|
||||||
|
self.auto_drive(3)
|
||||||
|
|
||||||
|
if pos[1] - dest[1] < 0:
|
||||||
|
self.auto_drive(2)
|
||||||
|
elif pos[1] - dest[1] > 0:
|
||||||
|
self.auto_drive(0)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.get_keys()
|
self.get_keys()
|
||||||
self.x += self.vx * self.game.dt
|
self.x += self.vx * self.game.dt
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
from settings import *
|
from settings import TILESIZE
|
||||||
|
|
||||||
|
|
||||||
class Wall(pg.sprite.Sprite):
|
class Wall(pg.sprite.Sprite):
|
||||||
def __init__(self, game, x, y):
|
def __init__(self, game, x, y):
|
||||||
@ -9,12 +10,13 @@ class Wall(pg.sprite.Sprite):
|
|||||||
self.game = game
|
self.game = game
|
||||||
self.image = pg.Surface((TILESIZE, TILESIZE))
|
self.image = pg.Surface((TILESIZE, TILESIZE))
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
self.image.blit(image,(0,0))
|
self.image.blit(image, (0, 0))
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.rect.x = x * TILESIZE
|
self.rect.x = x * TILESIZE
|
||||||
self.rect.y = y * TILESIZE
|
self.rect.y = y * TILESIZE
|
||||||
|
|
||||||
|
|
||||||
class Wall1(pg.sprite.Sprite):
|
class Wall1(pg.sprite.Sprite):
|
||||||
def __init__(self, game, x, y):
|
def __init__(self, game, x, y):
|
||||||
image = pg.image.load("img\\house.png")
|
image = pg.image.load("img\\house.png")
|
||||||
@ -23,7 +25,7 @@ class Wall1(pg.sprite.Sprite):
|
|||||||
self.game = game
|
self.game = game
|
||||||
self.image = pg.Surface((TILESIZE, TILESIZE))
|
self.image = pg.Surface((TILESIZE, TILESIZE))
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
self.image.blit(image,(-1,-1))
|
self.image.blit(image, (-1, -1))
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.rect.x = x * TILESIZE
|
self.rect.x = x * TILESIZE
|
||||||
|
Loading…
Reference in New Issue
Block a user