-
Notifications
You must be signed in to change notification settings - Fork 0
/
SokoBoy.py
146 lines (126 loc) · 3.02 KB
/
SokoBoy.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
import os
import sys
import getch
SCREEN = []
PLAYER = {'X':0, 'Y':0, 'BX':0, 'BY':0,}
STONES = []
PITS = []
class init():
def __init__(self):
self.field()
self.player()
self.stones(6,3)
self.pits(6,2)
self.stones(4,3)
self.pits(4,2)
def field(self,s_width=10,s_height=6):
global SCREEN
row = []
for y in range(s_height):
for x in range(s_width):
row.append("*")
row.append("\n")
SCREEN.append(row)
row = []
for y in range(len(SCREEN)-2):
for x in range(len(SCREEN[0])-3):
SCREEN[y+1][x+1] = '.'
def player(self,p_x=3,p_y=3):
global PLAYER
PLAYER['X'] = p_x
PLAYER['BX'] = p_x
PLAYER['Y'] = p_y
PLAYER['BY'] = p_y
def stones(self,x,y):
global STONES
STONES.append([x,y])
def pits(self,x,y):
global PITS
PITS.append([x,y])
def move(x=0,y=0):
'''update player pos'''
global PLAYER
next_x = PLAYER['X']+x
next_y = PLAYER['Y']+y
if SCREEN[next_y][next_x]=='o':
movestone(x,y,next_x,next_y)
if SCREEN[next_y][next_x]=='.':
PLAYER['BX'] = PLAYER['X']
PLAYER['BY'] = PLAYER['Y']
PLAYER['X'] = next_x
PLAYER['Y'] = next_y
def movestone(x,y,nx,ny):
global SCREEN
global STONES
stone = [nx,ny]
if SCREEN[ny+y][nx+x]=='.' or \
SCREEN[ny+y][nx+x]=='x':
for j in range(len(STONES)):
if STONES[j] == stone:
STONES[j] = [nx+x,ny+y]
SCREEN[ny][nx] = '.'
def key():
key = getch.getch()
if key == 'h':
move(-1,0)
if key == 'j':
move(0,1)
if key == 'k':
move(0,-1)
if key == 'l':
move(1,0)
if key == 'q':
sys.exit()
class draw():
def __init__(self):
self.player()
self.stones()
self.pits()
self.field()
self.player2()
self.finish()
def player(self):
global SCREEN
SCREEN[PLAYER['Y']][PLAYER['X']] = '@'
def player2(self):
global SCREEN
SCREEN[PLAYER['BY']][PLAYER['BX']] = '.'
def field(self):
buffer = []
for x in SCREEN:
buffer.append(''.join(x))
print(''.join(buffer))
def pits(self):
global SCREEN
for x in PITS:
px,py = x
SCREEN[py][px] = 'x'
if x in STONES:
SCREEN[py][px] = '0'
def stones(self):
for x in STONES:
sx,sy = x
SCREEN[sy][sx] = 'o'
def finish(self):
finish = 0
for x in SCREEN:
for y in x:
if 'x' in y:
finish = 1
if finish == 0:
print('win!!')
def monitor():
print(PLAYER)
print(STONES)
print(PITS)
init()
# update()
# draw()
os.system('clear')
while True:
move()
draw()
# monitor()
key()
os.system('clear')