Compare commits

..

2 Commits

Author SHA1 Message Date
Mateusz Dokowicz
586a94e0c0 working arrays and movement 2023-03-23 00:54:05 +01:00
Mateusz Dokowicz
be307ebab5 bugFixes 2023-03-22 23:11:13 +01:00
2 changed files with 67 additions and 48 deletions

View File

@ -2,24 +2,24 @@ import pygame
import sys import sys
def robot_movement(grid_width, grid_height, tile_width, tile_height, x, y): def movement_key_press(board_size, x, y):
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
sys.exit() sys.exit()
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
# go left # go left
if event.key == pygame.K_LEFT and x > (tile_width / 2): if event.key == pygame.K_LEFT and x > 0:
x -= tile_width x -= 1
# go right # go right
if event.key == pygame.K_RIGHT and x < (grid_width - (tile_width / 2)): if event.key == pygame.K_RIGHT and x < board_size - 1:
x += tile_width x += 1
# go up # go up
if event.key == pygame.K_UP and y > (tile_height / 2): if event.key == pygame.K_UP and y > 0:
y -= tile_height y -= 1
# go down # go down
if event.key == pygame.K_DOWN and y < (grid_height - (tile_height / 2)): if event.key == pygame.K_DOWN and y < board_size - 1:
y += tile_height y += 1
return (x, y) return (x, y)

View File

@ -3,7 +3,7 @@ from typing import List
from Interface.grid_draw import GridDraw, Colors from Interface.grid_draw import GridDraw, Colors
import sys import sys
import pygame import pygame
from Interface.movement import robot_movement from Interface.movement import movement_key_press
# window_dimensions says how many pixels window have # window_dimensions says how many pixels window have
@ -13,34 +13,36 @@ def initial_draw(window_dimensions, board_size):
pygame.display.set_caption("AI Vacuum Cleaner") pygame.display.set_caption("AI Vacuum Cleaner")
# define additional variables # define additional variables
tile_size = window_width / board_size tile_size = window_dimensions / board_size
# initialize board array # initialize board array
newGrid = Grid(board_size, tile_size) newGrid = Grid(board_size)
newGrid.add(objectOnTile(1, 1, acceptedType.PLAYER))
player = newGrid.findFirst(acceptedType.PLAYER)
newGrid.move(1, 1, 1, 2)
newGrid.move(1, 2, 1, 1)
# set window dimension # set window dimension
window_width = window_dimensions window_width = window_dimensions
window_height = window_dimensions window_height = window_dimensions
# FIXME @countingthedots: please tell me what is going on there and why??? # initialize drawer
# drawer = GridDraw(window_width, window_height)
renderer = GridDraw(window_width, window_height)
tile_width = window_width / board_size
tile_height = window_height / board_size
radius = tile_width / 3
x = tile_width / 2
y = tile_height / 2
# rendering loop # rendering loop
while True: while True:
renderer.start_draw() drawer.start_draw()
renderer.board(board_size, board_size) drawer.board(board_size, board_size)
(x, y) = robot_movement(
window_width, window_height, tile_width, tile_height, x, y player = newGrid.findFirst(acceptedType.PLAYER)
)
renderer.circle(x, y, radius, color=Colors.RED) (x, y) = movement_key_press(board_size, player.position_x, player.position_y)
renderer.end_draw()
pygame.time.delay(10) newGrid.move(player.position_x, player.position_y, x, y)
newGrid.render(drawer, window_dimensions, board_size)
drawer.end_draw()
pygame.time.delay(30)
# TODO wrap it all to another file that handles array rendering # TODO wrap it all to another file that handles array rendering
@ -61,22 +63,21 @@ class objectOnTile:
# calculate position from array position to window position eg.: array_position = 0 => window_position = 50 (px) # calculate position from array position to window position eg.: array_position = 0 => window_position = 50 (px)
def _translate_array_to_window_position( def _translate_array_to_window_position(array_position, tile_size_window) -> int:
array_position, window_dimensions, board_size
) -> int:
tile_size_window = window_dimensions / board_size
return array_position * tile_size_window + tile_size_window / 2 return array_position * tile_size_window + tile_size_window / 2
class Grid: class Grid:
def __init__(self, size_array, tile_size): def __init__(self, size_array):
self.array = [ self.array = [
[objectOnTile(i, j) for j in range(size_array)] for i in range(size_array) [objectOnTile(i, j) for j in range(size_array)] for i in range(size_array)
] ]
self.list: List[objectOnTile] = [] self.list: List[objectOnTile] = []
# render the array # render the array
def render(self, renderer: GridDraw, window_dimensions, board_size, tile_width): def render(self, drawer: GridDraw, window_dimensions, board_size):
tile_size = window_dimensions / board_size
# render object with respect to type # render object with respect to type
for item in self.list: for item in self.list:
if item.type == acceptedType.PLAYER: if item.type == acceptedType.PLAYER:
@ -86,17 +87,17 @@ class Grid:
# position on screen # position on screen
render_x = _translate_array_to_window_position( render_x = _translate_array_to_window_position(
item.position_x, window_dimensions, board_size item.position_x, tile_size
) )
render_y = _translate_array_to_window_position( render_y = _translate_array_to_window_position(
item.position_y, window_dimensions, board_size item.position_y, tile_size
) )
# image rendering function # image rendering function
renderer.circle( drawer.circle(
render_x, render_x,
render_y, render_y,
tile_width / PLAYER_RADIUS_RATIO, tile_size / PLAYER_RADIUS_RATIO,
color=PLAYER_COLOR, color=PLAYER_COLOR,
) )
# TODO act accordingly to other options # TODO act accordingly to other options
@ -115,7 +116,9 @@ class Grid:
self.array[newObject.position_x][newObject.position_y] = newObject self.array[newObject.position_x][newObject.position_y] = newObject
self.list.append(newObject) self.list.append(newObject)
def delete(self, position_x, position_y): # deletes object from game
# untested, potentially not working
def delete(self, position_x: int, position_y: int):
# Find the object with the given position in the list # Find the object with the given position in the list
for obj in self.list: for obj in self.list:
if obj.position_x == position_x and obj.position_y == position_y: if obj.position_x == position_x and obj.position_y == position_y:
@ -129,22 +132,38 @@ class Grid:
self.array[position_x][position_y] = objectOnTile(position_x, position_y) self.array[position_x][position_y] = objectOnTile(position_x, position_y)
self.list.remove(obj) self.list.remove(obj)
# TODO update: update position from (start_x, start_y) to (end_x, end_y) # move: update position from (start_x, start_y) to (end_x, end_y)
def update(self, start_x, start_y, end_x, end_y): def move(self, start_x: int, start_y: int, end_x: int, end_y: int):
# no change
if start_x == end_x and start_y == end_y:
return
# check if obj exist at starting position # check if obj exist at starting position
if self.array[start_x][start_y].type == acceptedType.EMPTY: if self.array[start_x][start_y].type == acceptedType.EMPTY:
print( print(
f"Cannot move object at ({start_x}, {start_y}): no object on position" f"Cannot move object at ({start_x}, {start_y}): no object on position"
) )
return return
# check if destination is empty # check if destination is empty
if self.array[end_x][end_y].type != acceptedType.EMPTY: if self.array[end_x][end_y].type != acceptedType.EMPTY:
print( print(
f"Cannot move object to ({end_x}, {end_y}): position already occupied" f"Cannot move object to ({end_x}, {end_y}): position already occupied"
) )
return return
# all OK
self.array[end_x][end_y].type = self.array[start_x][start_y].type
self.array[start_x][start_y].type = acceptedType.EMPTY
# TODO change movement to work with arrays # all OK
# change position attribute in array
self.array[start_x][start_y].position_x = end_x
self.array[start_x][start_y].position_y = end_y
# change position in array
self.array[end_x][end_y] = self.array[start_x][start_y]
self.array[start_x][start_y] = objectOnTile(start_x, start_y)
def findFirst(self, find_type: acceptedType) -> objectOnTile:
for item in self.list:
if item.type == find_type:
return item
else:
print(f"Cannot find object of type: ({find_type})!")