-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses_test.py
More file actions
108 lines (88 loc) · 3.52 KB
/
Copy pathclasses_test.py
File metadata and controls
108 lines (88 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import msvcrt
import os
# variables to define play area
global boardX
boardX = 40
global boardY
boardY = 20
class Square(object):
def __init__(self, positions, size = 1):
"""clockwise from top left"""
self.positions = [positions, (positions + size), ((positions + size) + (boardX * (size-1))), (positions + (boardX * (size-1)))]
def __change_pos(self, amount):
"""function to move the square object by a given amount"""
#print('change:',self.positions)
# move every corner by amount
for i in range(len(self.positions)):
self.positions[i] += amount
def move_shape(self):
"""User input function"""
# gets keyboard input using msvcrt
direction = ord(msvcrt.getch())
#print("direction=",direction)
# have to use ascii codes for this
if direction == 119: # w for up
self.__change_pos((boardX-(boardX*2)))
elif direction == 97: # a for left
self.__change_pos(-1)
elif direction == 115: # s for down
self.__change_pos(boardX)
elif direction == 100: # d for right
self.__change_pos(1)
elif direction == 113: # q for quit
exit()
class BoardClass(object):
def __init__(self, fill = '0', border_fill = '#'):
self.boardX = boardX # make the board X and Y an
self.boardY = boardY # attribute of boardclass
self.fill = fill # background will become this char
self.border_fill = border_fill # border will become this char
self.grid = boardX * boardY # this will be the grid that i print
self.border_values = self.__make_border() # get border indexes
def clear_board(self):
"""function to empty the board"""
self.grid = [self.fill for i in range(0, (self.boardX*self.boardY))]
def fill_shape(self, shape, fill = '1'):
"""This function fills the space between the edges"""
for i in range(shape.positions[0], shape.positions[1], 1):
self.grid[i] = fill
for i in range(shape.positions[3], shape.positions[2], 1):
self.grid[i] = fill
for i in range(shape.positions[0], shape.positions[3], self.boardX):
self.grid[i] = fill
for i in range(shape.positions[1], shape.positions[2], self.boardX):
self.grid[i-1] = fill
def __make_border(self):
"""Get the values that will become the border"""
border_values_list = []
for i in range(self.grid):
if i < boardX:
border_values_list.append(i)
if i >= boardX * (boardY - 1):
border_values_list.append(i)
if i % boardX == 0:
border_values_list.append(i)
border_values_list.append(i-1)
return border_values_list
def draw_board(self):
"""print the board"""
#self.draw_border('#')
#print(self.border_values)
for i in self.border_values:
self.grid[i] = self.border_fill
for i, j in enumerate(self.grid):
if (i+1) % boardX == 0 and i != 0:
print(j)
else:
print(j, end='')
def draw():
os.system('cls')
board.clear_board()
board.fill_shape(shape1)
board.draw_board()
#------------------------------------MAIN---------------------------------------
board = BoardClass(' ', '#')
shape1 = Square(0, 5)
while True:
draw()
shape1.move_shape()