forked from s481874/ai-wozek
bfs
This commit is contained in:
parent
6d0021d8f1
commit
5019d63022
BIN
forkliftE.png
Normal file
BIN
forkliftE.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 650 KiB |
BIN
forkliftN.png
Normal file
BIN
forkliftN.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 663 KiB |
BIN
forkliftS.png
Normal file
BIN
forkliftS.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 662 KiB |
BIN
forkliftW.png
Normal file
BIN
forkliftW.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 651 KiB |
198
wozek.py
198
wozek.py
@ -2,13 +2,21 @@ import pygame
|
||||
import sys
|
||||
import random
|
||||
import os
|
||||
import time
|
||||
|
||||
class Node():
|
||||
def __init__(self,position,rotation,action,parent):
|
||||
self.position=position
|
||||
self.rotation=rotation
|
||||
self.action=action
|
||||
self.parent=parent
|
||||
|
||||
# Initialize Pygame
|
||||
pygame.init()
|
||||
|
||||
# Constants
|
||||
TILE_SIZE = 96 # Size of a square tile in pixels
|
||||
GRID_WIDTH, GRID_HEIGHT = 16, 8 # Grid dimensions
|
||||
GRID_WIDTH, GRID_HEIGHT = 16,8 # Grid dimensions
|
||||
SCREEN_WIDTH, SCREEN_HEIGHT = GRID_WIDTH * TILE_SIZE, GRID_HEIGHT * TILE_SIZE
|
||||
FPS = 60 # Frames per second
|
||||
|
||||
@ -32,7 +40,8 @@ forklift_image_full = None
|
||||
freight_images_full = None
|
||||
|
||||
# Game variables
|
||||
forklift_pos = [7, 3] # Adjusted starting position of the forklift
|
||||
forklift_pos = [7, 0]
|
||||
rotation='E'# Adjusted starting position of the forklift
|
||||
carrying_freight = False
|
||||
carried_freight = None
|
||||
freight_positions = {} # Dictionary to keep track of freight positions and types
|
||||
@ -40,7 +49,16 @@ freight_positions = {} # Dictionary to keep track of freight positions and type
|
||||
# Load images
|
||||
def load_images():
|
||||
global forklift_image_full, freight_images_full
|
||||
forklift_image_full = load_image('forklift.png', (TILE_SIZE, TILE_SIZE))
|
||||
global rotation
|
||||
if rotation=='E':
|
||||
forklift_image_full = load_image('forkliftE.png', (TILE_SIZE, TILE_SIZE))
|
||||
elif rotation=='W':
|
||||
forklift_image_full = load_image('forkliftW.png', (TILE_SIZE, TILE_SIZE))
|
||||
elif rotation=='N':
|
||||
forklift_image_full = load_image('forkliftN.png', (TILE_SIZE, TILE_SIZE))
|
||||
elif rotation=='S':
|
||||
forklift_image_full = load_image('forkliftS.png', (TILE_SIZE, TILE_SIZE))
|
||||
#forklift_image_full = load_image('forklift.png', (TILE_SIZE, TILE_SIZE))
|
||||
freight_images_full = {
|
||||
'clothes': load_image('clothes.png', (TILE_SIZE, TILE_SIZE)),
|
||||
'fruit': load_image('fruit.png', (TILE_SIZE, TILE_SIZE)),
|
||||
@ -74,6 +92,7 @@ def draw_truck_bed_and_racks():
|
||||
for x in range(GRID_WIDTH):
|
||||
pygame.draw.rect(screen, (165, 42, 42), (x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE))
|
||||
|
||||
|
||||
def draw_forklift_and_freight():
|
||||
x, y = forklift_pos
|
||||
if carrying_freight:
|
||||
@ -91,12 +110,35 @@ def draw_freight():
|
||||
screen.blit(freight_images_full[freight_type], (x * TILE_SIZE, y * TILE_SIZE))
|
||||
|
||||
# Game mechanics
|
||||
def move_forklift(dx, dy):
|
||||
def move_forklift():
|
||||
global forklift_pos
|
||||
new_pos = [forklift_pos[0] + dx, forklift_pos[1] + dy]
|
||||
if(rotation=='E'):
|
||||
new_pos=[forklift_pos[0]+1,forklift_pos[1]]
|
||||
elif(rotation=='W'):
|
||||
new_pos=[forklift_pos[0]-1,forklift_pos[1]]
|
||||
elif(rotation=='N'):
|
||||
new_pos=[forklift_pos[0],forklift_pos[1]+1]
|
||||
elif (rotation == 'S'):
|
||||
new_pos = [forklift_pos[0], forklift_pos[1] - 1]
|
||||
#new_pos = [forklift_pos[0] + dx, forklift_pos[1] + dy]
|
||||
if 0 <= new_pos[0] < GRID_WIDTH and 0 <= new_pos[1] < GRID_HEIGHT:
|
||||
forklift_pos = new_pos
|
||||
def rotate_forklift(x):
|
||||
global rotation
|
||||
rot=['N','E','S','W']
|
||||
rots=rot.index(rotation)
|
||||
if x=='R':
|
||||
if rots==0:
|
||||
x=rot[3]
|
||||
else:
|
||||
x=rot[rots-1]
|
||||
|
||||
elif x=='L':
|
||||
if rots==3:
|
||||
x=rot[0]
|
||||
else:
|
||||
x=rot[rots+1]
|
||||
rotation=x
|
||||
def handle_freight():
|
||||
global carrying_freight, carried_freight, freight_positions
|
||||
pos_tuple = tuple(forklift_pos)
|
||||
@ -110,9 +152,145 @@ def handle_freight():
|
||||
carried_freight = freight_positions.pop(pos_tuple)
|
||||
carrying_freight = True
|
||||
|
||||
#searching for successors
|
||||
def succ(current_node):
|
||||
current_rotation=current_node.rotation
|
||||
x=current_node.position[0]
|
||||
y=current_node.position[1]
|
||||
successors=[]
|
||||
if(current_rotation=="N"):
|
||||
if(y<7):
|
||||
pos=[]
|
||||
pos.append(x)
|
||||
|
||||
pos.append(y+1)
|
||||
action='FW'
|
||||
successor=Node(pos,current_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
if(x>0):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y)
|
||||
new_rotation='W'
|
||||
action='L'
|
||||
successor = Node(pos, new_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
if(x<15):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y)
|
||||
new_rotation='E'
|
||||
action='R'
|
||||
successor = Node(pos, new_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
|
||||
|
||||
elif (current_rotation == "S"):
|
||||
if (y > 0):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y - 1)
|
||||
action = 'FW'
|
||||
successor = Node(pos, current_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
if (x > 0):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y)
|
||||
new_rotation = 'E'
|
||||
action='L'
|
||||
successor = Node(pos, new_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
if (x < 15):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y)
|
||||
new_rotation = 'W'
|
||||
action = 'R'
|
||||
successor = Node(pos, new_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
|
||||
elif (current_rotation == "E"):
|
||||
if (x <15):
|
||||
pos = []
|
||||
pos.append(x+1)
|
||||
pos.append(y)
|
||||
action = 'FW'
|
||||
successor = Node(pos, current_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
if (y <7):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y)
|
||||
new_rotation = 'N'
|
||||
action='R'
|
||||
successor = Node(pos, new_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
if (y >0):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y)
|
||||
new_rotation = 'S'
|
||||
action = 'L'
|
||||
successor = Node(pos, new_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
|
||||
elif (current_rotation == "W"):
|
||||
if (x > 0):
|
||||
pos = []
|
||||
pos.append(x+1)
|
||||
pos.append(y)
|
||||
action = 'FW'
|
||||
successor = Node(pos, current_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
if (y > 0):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y)
|
||||
new_rotation = 'S'
|
||||
action='R'
|
||||
successor = Node(pos, new_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
if (y < 7):
|
||||
pos = []
|
||||
pos.append(x)
|
||||
pos.append(y)
|
||||
new_rotation = 'N'
|
||||
action = 'L'
|
||||
successor = Node(pos, new_rotation,action,current_node)
|
||||
successors.append(successor)
|
||||
|
||||
return successors
|
||||
|
||||
#bfs
|
||||
def bfs(isstate,final):
|
||||
fringe=[]
|
||||
fringe.append(isstate)
|
||||
path=[]
|
||||
explored=[]
|
||||
while(True):
|
||||
if(len(fringe)==0):
|
||||
return False
|
||||
node=fringe.pop(0)
|
||||
if(node.position[0]==final.position[0] and node.position[1]==final.position[1]):
|
||||
while(node.parent!=None):
|
||||
path.append(node)
|
||||
node=node.parent
|
||||
return path
|
||||
explored.append(node)
|
||||
successors=succ(node)
|
||||
for successor in successors:
|
||||
fringe.append(successor)
|
||||
|
||||
# Main game loop
|
||||
def game_loop():
|
||||
init_game()
|
||||
current=Node(forklift_pos,rotation,'start',None)
|
||||
final=Node([10,6],'N','final',None)
|
||||
path=bfs(current,final)
|
||||
path.reverse()
|
||||
for node in path:
|
||||
print(node.action)
|
||||
running = True
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
@ -120,13 +298,13 @@ def game_loop():
|
||||
running = False
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LEFT:
|
||||
move_forklift(-1, 0)
|
||||
rotate_forklift('L')
|
||||
load_images()
|
||||
elif event.key == pygame.K_RIGHT:
|
||||
move_forklift(1, 0)
|
||||
rotate_forklift('R')
|
||||
load_images()
|
||||
elif event.key == pygame.K_UP:
|
||||
move_forklift(0, -1)
|
||||
elif event.key == pygame.K_DOWN:
|
||||
move_forklift(0, 1)
|
||||
move_forklift()
|
||||
elif event.key == pygame.K_SPACE:
|
||||
handle_freight()
|
||||
elif event.key == pygame.K_r:
|
||||
|
Loading…
Reference in New Issue
Block a user