{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#########################################################################\n", "# Imports\n", "#########################################################################\n", "import random\n", "import importlib\n", "\n", "#########################################################################\n", "# Functions provided, you don't have to touch these.\n", "#########################################################################\n", "\n", "def updateScore(score):\n", " \"\"\"Increments the score by one (you don't need to change this)\"\"\"\n", " return score + 1\n", "\n", "def prettyPrint(cubes, player, score, gridSize=5):\n", " \"\"\"Prints the board state (you don't need to change this)\"\"\"\n", " assert(0 <= player[0] < gridSize)\n", " assert(player[1] == gridSize - 1)\n", " square = u\" \\u25A0 \"\n", " triangle = u\" \\u25B2 \"\n", " space = \" \"\n", " toRet = \"\\n+\" + \"---+\"*gridSize + \"\\n\"\n", " for y in range(gridSize):\n", " toRet += \"|\" + \"|\".join([triangle if [x,y] == player else (space if [x,y] not in cubes else square) for x in\n", " range(gridSize)]) + \"|\\n\"\n", " toRet += \"+\" + \"---+\"*gridSize + \"\\n\"\n", " print(toRet)\n", "\n", "def reload():\n", " \"\"\"Don't change this! This lets you reload your file by typing `reload()`\"\"\"\n", " import cubegame\n", " importlib.reload(cubegame)\n", " exec(\"from cubegame import *\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Cube Runner \n", "---\n", "\n", "
\n", " The goal of the cube runner is to stay away from the cubes as long as possible; for each turn we survive, we will earn a point. \n", "
\n", "\n", "\n", " We can input: \"left\", \"right\", or \"stay\" to move left or right on a 5x5 grid, represented in [x,y] with [0,0] being the top left. \n", "
\n", " \n", "\n", " Make sure the player can't go out of bounds! \n", "
\n", "\n", "\n", " Each turn, the existing cubes will move one space down, and three new cubes will spawn at the top of the grid.\n", "
\n", "\n", "updateCubes()
updatePlayerLocation()
collision()
\n",
" updateCubes()
will take a list of cubes and then output a list of updated cubes on the gameboard. \n",
" \n",
"
\n",
" Each cubes location is represented by a list [x, y]
. \n",
"
\n",
"Here is an example of cubes
: [[1, 2], [4, 4], [2, 3], [1, 0]]
.\n",
"\n",
"
\n", " Steps:\n", "
while
loop and the random.randint()
function \n",
" updatePlayerLocation()
will take the player's current location and a direction to move (or not move) and then output the player's new location. Directions can be 'left', 'right', or 'stay'. If their direction would move them off the grid, they stay still instead (and lose their turn).\n",
"
\n", " Steps:\n", "
\n",
" collision()
will take the list of cubes and the player location and then output if player is going to collide with a cube or not (a boolean). \n",
"
\n", " Run this once you have completed the previous functions. You don't need to edit any of the code below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def runGame():\n", " \"\"\"Contains the main loop for running the game\"\"\"\n", " # Game state\n", " player = [2,4] # initial location of the player\n", " score = 0 # initial score\n", " cubes = [[0,0], [3,0], [4,0]] # initial cube locations\n", "\n", " print(\"Welcome to cubes! Quit by typing 'quit'\")\n", " prettyPrint(cubes, player, score)\n", "\n", " # Main loop\n", " while True:\n", " direction = input(\"Input 'left', 'right', 'stay', or 'quit': \")\n", " if direction=='quit':\n", " print(\"You quit! Score was\", score)\n", " break\n", " if direction !='left' and direction != 'right' and direction != 'stay':\n", " continue\n", " player = updatePlayerLocation(player, direction)\n", "\n", " cubes = updateCubes(cubes)\n", " score = updateScore(score)\n", " prettyPrint(cubes, player, score)\n", "\n", " if collision(cubes, player):\n", " print(\"You lose! Score was\", score)\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run Game" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "runGame()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extensions\n", "\n", "
\n", " Here are some extra ideas for making our game more interesting!\n", "
player
, score
, and cubes
variables.