A downloadable game

Enter "WASD" to move around the board. 

My entry for the #TweetTweetJam using python. The game was done in 551 characters.

Here's the code:

from random import randint as v
def P(m,s):
  print("Score: ",s)
  for r in m:
    for c in r:
      print(c,end=" ")
    print()
def R(map,G):
  b[v(0,3)][v(0,3)]=G
X,E,G='X','-','O';x,y,pX,pY=0,0,0,0;s=0;b=[[E]*4 for _ in range(4)];b[y][x]=X;R(b,G);P(b,s);
while(True):
  c='';
  while c.lower() not in {'w','a','s','d'}:c=input("WASD? ")
  if(y+1>0 and y<4 and x+1>0 and x<4):
    if c=='w':y-=1
    if c=='s':y+=1
    if c=='a':x-=1
    if c=='d':x+=1
    if b[y][x]==G:s+=1;R(b,G)
    b[y][x]=X;b[pY][pX]=E;pY,pX=y,x;
  P(b,s)

The non-shortened code with proper variable and function names:

from random import randint as random
def printBoard(m,s):
  print("Score: ", s)
  for row in m:
    for col in row:
      print(col,end=" ")
    print()
def reset(m, goal):
  board[random(0,3)][random(0,3)] = GOAL
PLAYER, EMPTY, GOAL = 'X', '-', 'O'
x, y, pX, pY = 0, 0, 0, 0
score = 0
board = [[EMPTY]*4 for _ in range(4)]
board[y][x] = PLAYER
reset(board, GOAL)
printBoard(board, score)
while(True):
  choice = ''
  while choice.lower() not in {'w', 'a', 's', 'd'}:
    choice = input("WASD? ")
  if(y + 1 > 0 and y < 4 and x + 1 > 0 and x < 4):
    if choice == 'w': y -= 1
    if choice == 's': y += 1
    if choice == 'a': x -= 1
    if choice == 'd': x += 1
    if board[y][x] == GOAL:
      score += 1
      reset(board, GOAL)
    board[y][x] = PLAYER
    board[pY][pX] = EMPTY
    pY, pX = y, x
  printBoard(board, score)

Install instructions

  1. Have Python3.8 installed and (if on Windows) System Environment Variables set up.
  2. Navigate to the file location in Command Prompt/ Terminal.
  3. Run the game with: python3 tweettweetjam.py

Sometimes, the game places the O on the character, so just restart when that happens.

Download

Download
tweettweetjam.py 567 bytes

Leave a comment

Log in with itch.io to leave a comment.