#########################################################################
# Imports
#########################################################################
import random
import importlib
#########################################################################
# Functions provided, you don't have to touch these.
#########################################################################
def updateScore(score):
"""Increments the score by one (you don't need to change this)"""
return score + 1
def prettyPrint(cubes, player, score, gridSize=5):
"""Prints the board state (you don't need to change this)"""
assert(0 <= player[0] < gridSize)
assert(player[1] == gridSize - 1)
square = u" \u25A0 "
triangle = u" \u25B2 "
space = " "
toRet = "\n+" + "---+"*gridSize + "\n"
for y in range(gridSize):
toRet += "|" + "|".join([triangle if [x,y] == player else (space if [x,y] not in cubes else square) for x in
range(gridSize)]) + "|\n"
toRet += "+" + "---+"*gridSize + "\n"
print(toRet)
def reload():
"""Don't change this! This lets you reload your file by typing `reload()`"""
import cubegame
importlib.reload(cubegame)
exec("from cubegame import *")
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.
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.
Make sure the player can't go out of bounds!
Each turn, the existing cubes will move one space down, and three new cubes will spawn at the top of the grid.
updateCubes()
updatePlayerLocation()
collision()
updateCubes()
will take a list of cubes and then output a list of updated cubes on the gameboard.
Each cubes location is represented by a list [x, y]
.
Here is an example of cubes
: [[1, 2], [4, 4], [2, 3], [1, 0]]
.
Steps:
while
loop and the random.randint()
function ## Student Code
def updateCubes(cubes):
"""
input: list of cubes
output: the updated list of cubes
"""
pass # Delete the 'pass' once you finish your code.
# update each cube's location (for loop)
# generate list of 3 random, non-repeated numbers (while loop & random.randint())
# add 3 random new cubes to list (for loop)
# return list of cubes
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).
Steps:
def updatePlayerLocation(player, direction):
"""
input: player location & direction to move
output: updated player location
"""
pass # delete 'pass' once you have completed your code.
# if direction is 'left', move the player left if it will not move the player off the board.
# if direction is 'right', move the player right if it will not move the player off the board.
# return (new) player location
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).
def collision(cubes, player):
""" Take as input the list of cube locations and the player location.
Return True if there is a collision between the player and a cube, False otherwise.
"""
pass # delete 'pass' once you have completed your code.
# check if player will collide w/ a cube
# return true/false
Run this once you have completed the previous functions. You don't need to edit any of the code below.
def runGame():
"""Contains the main loop for running the game"""
# Game state
player = [2,4] # initial location of the player
score = 0 # initial score
cubes = [[0,0], [3,0], [4,0]] # initial cube locations
print("Welcome to cubes! Quit by typing 'quit'")
prettyPrint(cubes, player, score)
# Main loop
while True:
direction = input("Input 'left', 'right', 'stay', or 'quit': ")
if direction=='quit':
print("You quit! Score was", score)
break
if direction !='left' and direction != 'right' and direction != 'stay':
continue
player = updatePlayerLocation(player, direction)
cubes = updateCubes(cubes)
score = updateScore(score)
prettyPrint(cubes, player, score)
if collision(cubes, player):
print("You lose! Score was", score)
break
runGame()
Here are some extra ideas for making our game more interesting!
player
, score
, and cubes
variables.