From 0dfaa1f4ad2c456c1ede513c338f69c4685af3a9 Mon Sep 17 00:00:00 2001 From: Kristian Rother Date: Mon, 8 Jan 2024 14:08:27 +0100 Subject: [PATCH] clean up prototype --- getting_started/prototype_opencv.py | 44 ++++++++++++++++------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/getting_started/prototype_opencv.py b/getting_started/prototype_opencv.py index d5c2a78..1c06abe 100644 --- a/getting_started/prototype_opencv.py +++ b/getting_started/prototype_opencv.py @@ -3,7 +3,7 @@ Install dependencies: - pip install numpy opencv-python + pip install opencv-python """ import numpy as np import cv2 @@ -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()