Code:
import os
width = 10
height = 10
empty_char = " "
wall_char = "░░░"
bullet_char = [" ▸ ", " ▾ ", " ◂ ", " ▴ "]
tank1_char = ["o>o", "ovo", "o<o", "o^o"]
tank2_char = ["*>*", "*v*", "*<*", "*^*"]
tank1_name = "Arjo's tank"
tank2_name = "Renze's tank"
current_move = 1
tank1 = []
tank1_dir = 0
tank2 = []
tank2_dir = 0
bullets = [] #bullets = [[x, y, direction]]
winner = None
walls = []
for y in range(height):
walls.append([0 for x in range(width)])
map_1 = '''
xxxxxxxxxx
x1------2x
x-x--xxx-x
x-x----x-x
x---xx-x-x
x-x-xx---x
x-x----x-x
x-xxx--x-x
x--------x
xxxxxxxxxx
'''
def generate_map(map_string):
global tank1, tank2, walls
for y, line in enumerate(map_string.splitlines()[1:]):
for x, char in enumerate(line):
if char == 'x':
walls[y][x] = 1
else:
if char == '1':
tank1 = [x, y]
elif char == '2':
tank2 = [x, y]
else:
walls[y][x] = 0
def draw_winner():
os.system("clear")
if winner == "tie":
print("It's a tie!")
else:
print("The winner is: "+winner+"!")
def draw_screen():
# Clear the screen
os.system("clear")
# Prints the battlefield
for y in range(height):
draw_line = ""
for x in range(width):
if [x, y] == tank1:
character = tank1_char[tank1_dir]
elif [x, y] == tank2:
character = tank2_char[tank2_dir]
elif walls[y][x] == 1:
character = wall_char
else:
character = empty_char
for b in range(len(bullets)):
if (x == bullets[b][0]) and (y == bullets[b][1]):
character = bullet_char[bullets[b][2]]
draw_line += character
print(draw_line)
#Prints the information
print("MOVE "+str(current_move))
def do_action(tank, action_type):
global tank1_dir, tank2_dir, bullets
# RETURN CODES #
# 0: no wall, tank moves to that place
# 1: wall or other tank, tank stays on the current place
# 2: illegal move
if tank == 1:
xx = tank1[0]
yy = tank1[1]
_xx = tank2[0]
_yy = tank2[1]
tank_dir = tank1_dir
elif tank == 2:
xx = tank2[0]
yy = tank2[1]
_xx = tank1[0]
_yy = tank1[1]
tank_dir = tank2_dir
if action_type == "left":
if (walls[yy][xx-1] == 0) and not ((xx-1 == _xx) and (yy == _yy)):
tank_dir = 2
xx -= 1
else:
return 1
elif action_type == "right":
if (walls[yy][xx+1] == 0) and not ((xx+1 == _xx) and (yy == _yy)):
tank_dir = 0
xx += 1
else:
return 1
elif action_type == "up":
if (walls[yy-1][xx] == 0) and not ((xx == _xx) and (yy-1 == _yy)):
tank_dir = 3
yy -= 1
else:
return 1
elif action_type == "down":
if (walls[yy+1][xx] == 0) and not ((xx == _xx) and (yy+1 == _yy)):
tank_dir = 1
yy += 1
else:
return 1
elif action_type == "shoot":
new_bullet = [xx, yy, tank_dir]
bullets.append(new_bullet)
elif action_type == "turn_left":
tank_dir = 2
elif action_type == "turn_right":
tank_dir = 0
elif action_type == "turn_up":
tank_dir = 3
elif action_type == "turn_down":
tank_dir = 1
elif action_type == "pass":
pass
if tank == 1:
tank1[0] = xx
tank1[1] = yy
tank1_dir = tank_dir
elif tank == 2:
tank2[0] = xx
tank2[1] = yy
tank2_dir = tank_dir
return 0
def check_collision(bullet):
bul_x = bullet[0]
bul_y = bullet[1]
if walls[bul_y][bul_x] == 1:
return True
else:
return False
def move_bullets():
global bullets, winner
bullets = [bullet for bullet in bullets if not check_collision(bullet)]
for i in range(len(bullets)):
direction = bullets[i][2]
if direction == 2: #left
bullets[i][0] -= 1
elif direction == 0: #right
bullets[i][0] += 1
elif direction == 3: #up
bullets[i][1] -= 1
elif direction == 1: #down
bullets[i][1] += 1
# Check collision with the tanks
if (bullets[i][0] == tank1[0]) and (bullets[i][1] == tank1[1]):
if (bullets[i][0] == tank2[0]) and (bullets[i][1] == tank2[1]):
winner = "tie"
else:
winner = tank2_name+" ("+tank2_char[0]+")"
else:
if (bullets[i][0] == tank2[0]) and (bullets[i][1] == tank2[1]):
winner = tank1_name+" ("+tank1_char[0]+")"
def update():
global current_move
move_bullets()
draw_screen()
current_move += 1
def arjo_ai():
arjo_action = "right"
print("I did action "+arjo_action)
return arjo_action
def main():
generate_map(map_1)
update()
while winner == None:
#do_action(1, arjo_ai())
arjo_action = input(tank1_char[tank1_dir]+" ")
do_action(1, arjo_action)
renze_action = input(tank2_char[tank2_dir]+" ")
do_action(2, renze_action)
update()
draw_winner()
main()