-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_game.py
More file actions
125 lines (107 loc) · 4.36 KB
/
Copy pathsnake_game.py
File metadata and controls
125 lines (107 loc) · 4.36 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import msvcrt
import os
import string
import math
class Board(object):
def __init__(self, boardX, boardY, fill = ' ', border_fill = '#'):
self.boardX = boardX
self.boardY = boardY
self.fill = fill
self.border_fill = border_fill
# instead of generating a clean board each time i will keep this clean
# copy and overwrite teh previous board with this clean version
self.clean_board = self.__clean_and_make_border()
self.board_print = self.clean_board
def __clean_and_make_border(self):
# clear the baord
board_print = [[self.fill] * self.boardX] * self.boardY
# this border function scales to any size of play area
# and is much much less of a headache than what i had before
# make the top border
board_print[0] = self.border_fill * self.boardX
# make the bottom border
board_print[self.boardY-1] = self.border_fill * self.boardX
# make the side borders which is just the first index and the last index
# of each row from the Y index of 1 to Y - 2 since we already have the
# first and last row covered
for i in range(1,self.boardY-2):
board_print[i][0] = self.border_fill
board_print[i][-1] = self.border_fill
return board_print
# problem possibly here
def draw_board(self):
#self.board_print = self.clean_board
for i in self.board_print:
#print(*i, sep='')
print(i)
def clear_board(self):
self.board_print = self.clean_board
# problem possibly here
def draw_snake(self, snake_pos, body_fill):
for pos in snake_pos:
self.board_print[pos[1]][pos[0]] = body_fill
print(pos, self.board_print[pos[1]][pos[0]])
class Snake(object):
def __init__(self, boardX, boardY, body_fill = '@', init_length = 4):
self.body_fill = body_fill
# initial length of the snake
# index 0 will be the head
self.init_length = init_length
self.boardX = boardX
self.boardY = boardY
# the head will start at the center of the baord
self.startX = math.floor(boardX / 2)
self.startY = math.floor(boardY / 2)
# this 2d array will store the positions of the body pieces in [[X,Y]]
self.body_pos = [[i,self.startY] for i in range(self.startX, self.startX+self.init_length)]
self.direction = Snake.a
# movement and quit ascii numbers
w = 119
a = 97
s = 115
d = 100
q = 113
# incomplete
def move_snake(self, other):
# check for keybaord hit, will move in previous direction if not hit
if msvcrt.kbhit():
# get ascii value of input
self.direction = ord(msvcrt.getch())
if self.direction == Snake.w:
self.body_pos = self.shift_body('Y', -1)
elif self.direction == Snake.a:
self.body_pos = self.shift_body('X', -1)
elif self.direction == Snake.s:
self.body_pos = self.shift_body('Y', 1)
elif self.direction == Snake.d:
self.body_pos = self.shift_body('X', 1)
elif self.direction == Snake.q:
exit()
print(Snake.a)
#else:
# incomplete? maybe seems to work
def shift_body(self, direction, move):
# new list of pody pos which will have every element shifted by 1 to
# make space for the new head, old tail gets auto overwritten
new_body = [[]]
for i in range(len(self.body_pos-1)):
new_body.append(self.body_pos[i+1])
if direction == 'X':
# get teh old head pos and put it into the new head pos
new_body[0] = [new_body[1][0] + move ,new_body[1][1]]
return new_body
elif direction == 'Y':
# get teh old head pos and put it into the new head pos
new_body[0] = [new_body[1][0],new_body[1][1] + move]
return new_body
def draw(main_board, main_snake):
#os.system('cls')
main_board.clear_board()
main_board.draw_snake(main_snake.body_pos, main_snake.body_fill)
main_board.draw_board()
print(main_snake.body_pos)
main_board = Board(20, 10)
main_snake = Snake(main_board.boardX, main_board.boardY)
#while True:
draw(main_board, main_snake)
#main_snake.move_snake(main_board)