Skip to content

Commit

Permalink
clean up prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Rother committed Jan 8, 2024
1 parent c006256 commit 0dfaa1f
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions getting_started/prototype_opencv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Install dependencies:
pip install numpy opencv-python
pip install opencv-python
"""
import numpy as np
import cv2
Expand All @@ -13,37 +13,41 @@
SCREEN_SIZE_X, SCREEN_SIZE_Y = 640, 640
TILE_SIZE = 64

size2x = lambda a: np.kron(a, np.ones((2, 2, 1), dtype=a.dtype))

# load image and extract square tiles from it
wall = size2x(cv2.imread('tiles/wall.png'))
player = size2x(cv2.imread('tiles/deep_elf_high_priest.png'))
def draw_player(background, player, x, y):
"""draws the player image on the screen"""
frame = background.copy()
xpos, ypos = x * TILE_SIZE, y * TILE_SIZE
frame[ypos : ypos + TILE_SIZE, xpos : xpos + TILE_SIZE] = player
cv2.imshow("frame", frame)


def double_size(img):
"""returns an image twice as big"""
return np.kron(img, np.ones((2, 2, 1), dtype=img.dtype))

# define boundaries of the 2D grid
min_x, max_x = 0, SCREEN_SIZE_X // TILE_SIZE
min_y, max_y = 0, SCREEN_SIZE_Y // TILE_SIZE

# NumPy array used as background (BGR color channels)
# load image
player = double_size(cv2.imread("tiles/deep_elf_high_priest.png"))

# create black background image with BGR color channels
background = np.zeros((SCREEN_SIZE_Y, SCREEN_SIZE_X, 3), np.uint8)

# starting position of the player
# starting position of the player in dungeon
x, y = 4, 4

while True:
exit_pressed = False

# draw
frame = background.copy()
xpos, ypos = x * TILE_SIZE, y * TILE_SIZE
frame[ypos:ypos + TILE_SIZE, xpos:xpos + TILE_SIZE] = player
cv2.imshow('frame', frame)
while not exit_pressed:
draw_player(background, player, x, y)

# handle keyboard input
key = chr(cv2.waitKey(1) & 0xFF)
if key == 'q':
break
elif key == 'd':
if key == "d":
x += 1
elif key == 'a':
elif key == "a":
x -= 1
elif key == "q":
exit_pressed = True

cv2.destroyAllWindows()

0 comments on commit 0dfaa1f

Please sign in to comment.