diff --git a/Block/GenericBlock.py b/Block/GenericBlock.py new file mode 100644 index 0000000..a1d0b39 --- /dev/null +++ b/Block/GenericBlock.py @@ -0,0 +1,6 @@ +from Block import * +from tkinter import * + +class GenericBlock: + def __init__(self, x, y, blocktype: str, canvas: Canvas): + Block.draw_block(x=x, y=y, block_id=blocktype, canvas=canvas) diff --git a/Block/__init__.py b/Block/__init__.py index 0ed1a4d..2f934f9 100644 --- a/Block/__init__.py +++ b/Block/__init__.py @@ -1,7 +1,6 @@ from tkinter import * class Block: - def convert_coords_to_str(self, coords: list) -> str: return str(str(coords[0]) + "," + str(coords[1])) @@ -11,7 +10,7 @@ def convert_coords_to_str(self, coords: list) -> str: "clone:air": "white" } - def get_xy( x, y): + def get_xy(x, y): start_x = 20 start_y = 20 width = 20 @@ -23,23 +22,19 @@ def decon_xy(x, y): width = 20 return x * width + start_x, y * width + start_y - - blocks = [ - ["clone:dirt", "clone:grass_block", "clone:dirt"], ["clone:dirt", "clone:dirt", "clone:dirt"], + ["clone:dirt", "clone:grass_block", "clone:dirt"], ["clone:dirt", "clone:dirt", "clone:dirt"] ] - def draw_block( x: int, y: int, block_id: str, canvas: Canvas): + def draw_block(x: int, y: int, block_id: str, canvas: Canvas): block = Block.block_ids[block_id] - xy: tuple = Block.get_xy(x=x, y=y) - canvas.create_rectangle(xy[0], xy[1], 39, 39, fill=block, outline=block) + xy = Block.get_xy(x, y) + canvas.create_rectangle(xy[0], xy[1], xy[0]+19, xy[1]+19, fill=block, outline=block) def draw_blocks(self, canvas: Canvas): for x in range(len(self.blocks)): - for y in range(len(self.blocks)): + for y in range(len(self.blocks[x])): print(self.blocks[x][y], "at", x, y) - self.draw_block(x=x, y=y, block_id=self.blocks[x][y], canvas=canvas) - - + self.draw_block(x, y, self.blocks[x][y], canvas) diff --git a/Player/__init__.py b/Player/__init__.py index 7ee1a59..a0784f2 100644 --- a/Player/__init__.py +++ b/Player/__init__.py @@ -13,7 +13,7 @@ def move_left(self): print("Pressed enter") Block.draw_block(x=self.coords[0], y=self.coords[1], block_id="clone:air", canvas=self.canvas) - self.coords=[self.coords[0] + 1, self.coords[1]] + self.coords=[self.coords[0]+1, self.coords[1]] self.draw_player(self.coords[0], self.coords[1]) @@ -23,13 +23,15 @@ def convert_coords_to_str(self, coords: list) -> str: def move(self, root: Tk): canvas = self.canvas - # root.bind("", self.move_left()) - # root.bind("", self.move_down()) + root.bind("", lambda event:self.move_left()) + root.bind("", lambda event: self.move_right()) + root.bind("", lambda event: self.move_up()) + root.bind("", lambda event:self.move_down()) def draw_player(self, x, y): canvas = self.canvas xy = Block.get_xy(x, y) - self.player: canvas.create_rectangle = canvas.create_rectangle(xy[0], xy[1], 39, 39, + self.player: canvas.create_rectangle = canvas.create_rectangle(xy[0], xy[1], xy[0]+19, xy[1]+19, fill="blue", outline="blue") def move_down(self): @@ -39,3 +41,13 @@ def move_down(self): def draw(self, root: Tk): self.move(root) + + def move_right(self): + Block.draw_block(x=self.coords[0], y=self.coords[1], block_id="clone:air", canvas=self.canvas) + self.coords = [self.coords[0]-1, self.coords[1]] + self.draw_player(self.coords[0], self.coords[1]) + + def move_up(self): + Block.draw_block(x=self.coords[0], y=self.coords[1], block_id="clone:air", canvas=self.canvas) + self.coords = [self.coords[0], self.coords[1]-1] + self.draw_player(self.coords[0], self.coords[1]) diff --git a/main.py b/main.py index 761f82d..c587433 100644 --- a/main.py +++ b/main.py @@ -41,13 +41,12 @@ def draw_rows(size: int = 17, height: float = 20, width: float = 20): raise GridNotFound.GridNotFound("Grid Not Found") draw_columns(chunk_size) draw_rows(chunk_size) - Block.draw_block(x=0, y=1, block_id="clone:grass_block", canvas=canvas) - # player = Player(canvas) + player = Player(canvas) while True: - # player.draw(tk) + player.draw(tk) tk.update()