Skip to content

c-xinghan/lightcycle

Repository files navigation

LightCycle

Replica of the Tron arcade game.

LightCycle.demo.mp4

Background

This was my contribution to a group project for my first-year undergraduate studies at SUTD. I was responsible for:

  • Creating a terminal-based main menu to serve as a 'launcher' for each group member's game
  • Creating this game

The rubrics dictate that we use only Python standard libraries. This necessitated building a helper class to handle vector calculations.

Full project (including all other members' work) can be found here.

Instructions

  1. Run MainMenu.py in your favourite terminal.
  2. Type 5 and press the Enter key to select the LightCycle game.

Files for the other games are not included in this repository; they can be acquired seperately here.

  1. Choose a desired resolution and difficulty level. The difficulty level determines the speed of both players.
  2. Player 1 ( #00ffff cyan) moves using the WASD keys. Player 2 ( #ff8000 orange) moves using the ← → ↑ ↓ (arrow) keys.
  3. To win, make your opponent collide with the map boundaries or any player's "tail".

Pressing Esc quits the game instance.

Documentation

MainMenu.py

Function Description Parameter(s)
choose_option(filename)
  • Opens the text file defined in the string filename.
  • Prompts user for input with a message consisting of first line in text file + each option's Displayed value and the corresponding input to select it.
  • User will be prompted for input until a valid input is received.
  • Once a valid input is received, its corresponding Actual value will be executed.
  • After execution is complete, this function then calls itself recursively, unless the user inputs ‘exit’.
  • This is to allow the main menu to stay open after returning from a mini-game.
String filename
Name of text file, in string format (e.g. “settings.txt”). The first line of the text file contains a message to be displayed. Subsequent lines contain the list of options to choose from (one option per line). Each option should have a Displayed value followed by an Actual value, separated by a pipe |. The Actual value should be a callable function/method.

LightCycle.py

Function Description Parameter(s) Returned value(s)
choose_option(filename)
  • Opens the text file defined in the string filename.
  • Prompts user for input with a message consisting of first line in text file + each option's Displayed value and the corresponding input to select it.
  • User will be prompted for input until a valid input is received.
  • Once a valid input is received, its corresponding Actual value will be executed.
  • After execution is complete, this function then calls itself recursively, unless the user inputs ‘exit’.
  • This is to allow the main menu to stay open after returning from a mini-game.
String filename
Name of text file, in string format (e.g. “settings.txt”). The first line of the text file contains a message to be displayed. Subsequent lines contain the list of options to choose from (one option per line). Each option should have a Displayed value followed by an Actual value, separated by a pipe |. The Actual value should be a callable function/method.
NIL
draw_square(size, colour, x_offset, y_offset, fill=True)
  • Uses a Turtle instance and the parameters passed in to draw a square.
Integer size
length of square’s sides

String colour
colour of square’s outline and/or fill

Integer x_offset
x position of square

Integer y_offset
y position of square

Boolean fill
whether to fill square with colour (defaults to True)
NIL
in_boundary(head, res)
  • Derives boundaries from the integer res, and checks if the x and y attributes of head are within those boundaries.
Vector head
Vector instance to check

Integer res
resolution of game window
Boolean
True if x and y attributes of head are within boundaries, False otherwise.
render()
  • Represents a single frame of the game.
  • Game advances as this function calls itself recursively until the game concludes
  • Moves players forwards using the Vector.translate() method.
  • Uses in_boundary() and the in operator to check for collisions with trails or boundaries.
  • If collision is detected, announce the winner and return None to exit the recursive loop.
  • Adds the Vector instance representing the current position to a Set of trails.
  • Uses function draw_square() to draw a new square representing the players’ current positions.
  • Calls itself to render the next frame
NIL NIL
main()
  • Called to start the game.
  • Prints welcome message from a .txt file
  • Calls function choose_option() to let the user specify game options
  • Counts down to game start using a countdown message
  • Instantiates Vector objects representing each player’s starting position and direction vectors
  • Defines empty Sets, which will later contain Vector instances representing each player’s trails.
  • Instantiates turtle.Screen() and turtle.Turtle() objects, calls a series of methods on those instances
  • Binds controls to change direction by associating keypresses with calls to the Vector.rotate() method
  • Calls function render() to start the game
  • Binds control to exit game by associating the Esc key with calls to the Turtle.Screen.bye() method
NIL NIL

helper.py

Vector class

Attribute Description
x Instance attribute. Represents the x-component of the Vector.
y Instance attribute. Represents the y-component of the Vector.
dir Instance attribute. A String describing the direction of the Vector. Can be any of the Strings from the directions class attribute.
hash Instance attribute. Stores the hash value of the Vector instance, or None if the Vector is not hashed.
directions Class attribute. A List containing the possible dirs a Vector can have.
unit_vectors Class attribute. A Dictionary containing unit vectors as keys referencing their corresponding dir strings.
Method Description Parameter(s) Returned value(s)
__init__()
  • Instantiates a Vector object. Assigns input parameters to x and y attributes, evaluates x and y components of its unit vector and looks up this unit vector in unit_vectors to find and assign the suitable String to assign to the dir attribute.
Integer/Float x
x-component of the vector.

Integer/Float y
y-component of the vector.
Vector instance
x()
  • Getter method for instance attribute x.
NIL Instance attribute x
x(new_x)
  • Setter method for instance attribute x.
Integer new_x
Value to assign to instance attribute x
NIL
y()
  • Getter method for instance attribute y.
NIL Instance attribute y
y(new_y)
  • Setter method for instance attribute y.
Integer new_y
Value to assign to instance attribute y
NIL
dir()
  • Getter method for instance attribute dir.
NIL Instance attribute y
translate(translator)
  • Performs vector summation by adding translator's x and y components to self’s x and y components respectively.
Vector translator

Vector to be added to self.
NIL
rotate(new_dir)
  • Performs rotation transformation on self.
  • Firstly, the number of 90 degree quadrants to rotate the vector by is calculated by comparing the indexes of parameter new_dir and instance attribute dir within class attribute directions.
  • The method then checks for 0 or 180 degree rotations and prevents them from happening.
  • Parameter new_dir is then assigned to instance attribute dir.
  • Lastly, these equations $$x_2 = x_1 \cos \theta - y_1 \sin \theta$$ $$y_2 = x_1 \sin \theta + y_1 \cos \theta$$ as seen here are used to calculate and assign the new x and y attributes.
String new_dir
Intended dir after rotation transformation is complete.
NIL
copy()
  • Creates a new Vector instance with identical attributes to self and returns it.
NIL Vector instance
__eq__(other)
  • Overrides default == operator behaviour, causing it to return True if two Vector instances being compared have identical x and y components.
Vector other
A Vector to perform the equality check against.
NIL
__hash__()
  • Hashes the Vector instance if it is not hashed.
NIL NIL

About

Tron-inspired game

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages