working arrays and movement
This commit is contained in:
parent
be307ebab5
commit
586a94e0c0
@ -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)
|
||||||
|
@ -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
|
||||||
@ -18,23 +18,30 @@ def initial_draw(window_dimensions, board_size):
|
|||||||
# initialize board array
|
# initialize board array
|
||||||
newGrid = Grid(board_size)
|
newGrid = Grid(board_size)
|
||||||
newGrid.add(objectOnTile(1, 1, acceptedType.PLAYER))
|
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)
|
|
||||||
|
|
||||||
# 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)
|
||||||
|
|
||||||
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)
|
pygame.time.delay(30)
|
||||||
|
|
||||||
|
|
||||||
@ -68,7 +75,7 @@ class Grid:
|
|||||||
self.list: List[objectOnTile] = []
|
self.list: List[objectOnTile] = []
|
||||||
|
|
||||||
# render the array
|
# 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
|
tile_size = window_dimensions / board_size
|
||||||
|
|
||||||
# render object with respect to type
|
# render object with respect to type
|
||||||
@ -87,7 +94,7 @@ class Grid:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# image rendering function
|
# image rendering function
|
||||||
renderer.circle(
|
drawer.circle(
|
||||||
render_x,
|
render_x,
|
||||||
render_y,
|
render_y,
|
||||||
tile_size / PLAYER_RADIUS_RATIO,
|
tile_size / PLAYER_RADIUS_RATIO,
|
||||||
@ -109,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:
|
||||||
@ -123,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})!")
|
||||||
|
Loading…
Reference in New Issue
Block a user