Create-Array-for-Grid #8
@ -2,24 +2,24 @@ import pygame
|
||||
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():
|
||||
if event.type == pygame.QUIT:
|
||||
sys.exit()
|
||||
if event.type == pygame.KEYDOWN:
|
||||
# go left
|
||||
if event.key == pygame.K_LEFT and x > (tile_width / 2):
|
||||
x -= tile_width
|
||||
if event.key == pygame.K_LEFT and x > 0:
|
||||
x -= 1
|
||||
|
||||
# go right
|
||||
if event.key == pygame.K_RIGHT and x < (grid_width - (tile_width / 2)):
|
||||
x += tile_width
|
||||
# go right
|
||||
if event.key == pygame.K_RIGHT and x < board_size - 1:
|
||||
x += 1
|
||||
|
||||
# go up
|
||||
if event.key == pygame.K_UP and y > (tile_height / 2):
|
||||
y -= tile_height
|
||||
# go up
|
||||
if event.key == pygame.K_UP and y > 0:
|
||||
y -= 1
|
||||
|
||||
# go down
|
||||
if event.key == pygame.K_DOWN and y < (grid_height - (tile_height / 2)):
|
||||
y += tile_height
|
||||
# go down
|
||||
if event.key == pygame.K_DOWN and y < board_size - 1:
|
||||
y += 1
|
||||
return (x, y)
|
||||
|
@ -3,7 +3,7 @@ from typing import List
|
||||
from Interface.grid_draw import GridDraw, Colors
|
||||
import sys
|
||||
import pygame
|
||||
from Interface.movement import robot_movement
|
||||
from Interface.movement import movement_key_press
|
||||
|
||||
|
||||
# window_dimensions says how many pixels window have
|
||||
@ -18,23 +18,30 @@ def initial_draw(window_dimensions, board_size):
|
||||
# initialize board array
|
||||
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
|
||||
window_width = window_dimensions
|
||||
window_height = window_dimensions
|
||||
|
||||
# FIXME @countingthedots: please tell me what is going on there and why???
|
||||
#
|
||||
renderer = GridDraw(window_width, window_height)
|
||||
# initialize drawer
|
||||
drawer = GridDraw(window_width, window_height)
|
||||
|
||||
# rendering loop
|
||||
while True:
|
||||
renderer.start_draw()
|
||||
renderer.board(board_size, board_size)
|
||||
drawer.start_draw()
|
||||
drawer.board(board_size, board_size)
|
||||
|
||||
newGrid.render(renderer, window_dimensions, board_size)
|
||||
player = newGrid.findFirst(acceptedType.PLAYER)
|
||||
|
||||
renderer.end_draw()
|
||||
(x, y) = movement_key_press(board_size, player.position_x, player.position_y)
|
||||
|
||||
newGrid.move(player.position_x, player.position_y, x, y)
|
||||
|
||||
newGrid.render(drawer, window_dimensions, board_size)
|
||||
drawer.end_draw()
|
||||
pygame.time.delay(30)
|
||||
|
||||
|
||||
@ -68,7 +75,7 @@ class Grid:
|
||||
self.list: List[objectOnTile] = []
|
||||
|
||||
# render the array
|
||||
def render(self, renderer: GridDraw, window_dimensions, board_size):
|
||||
def render(self, drawer: GridDraw, window_dimensions, board_size):
|
||||
tile_size = window_dimensions / board_size
|
||||
|
||||
# render object with respect to type
|
||||
@ -87,7 +94,7 @@ class Grid:
|
||||
)
|
||||
|
||||
# image rendering function
|
||||
renderer.circle(
|
||||
drawer.circle(
|
||||
render_x,
|
||||
render_y,
|
||||
tile_size / PLAYER_RADIUS_RATIO,
|
||||
@ -109,7 +116,9 @@ class Grid:
|
||||
self.array[newObject.position_x][newObject.position_y] = 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
|
||||
for obj in self.list:
|
||||
if obj.position_x == position_x and obj.position_y == position_y:
|
||||
@ -123,22 +132,38 @@ class Grid:
|
||||
self.array[position_x][position_y] = objectOnTile(position_x, position_y)
|
||||
self.list.remove(obj)
|
||||
|
||||
# TODO update: update position from (start_x, start_y) to (end_x, end_y)
|
||||
def update(self, start_x, start_y, end_x, end_y):
|
||||
# move: update position from (start_x, start_y) to (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
|
||||
if self.array[start_x][start_y].type == acceptedType.EMPTY:
|
||||
print(
|
||||
f"Cannot move object at ({start_x}, {start_y}): no object on position"
|
||||
)
|
||||
return
|
||||
|
||||
# check if destination is empty
|
||||
if self.array[end_x][end_y].type != acceptedType.EMPTY:
|
||||
print(
|
||||
f"Cannot move object to ({end_x}, {end_y}): position already occupied"
|
||||
)
|
||||
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})!")
|
||||
|
Loading…
Reference in New Issue
Block a user