Skip to content

Instantly share code, notes, and snippets.

@hawkz
Created February 27, 2017 12:54
Show Gist options
  • Save hawkz/927316f2919e7f38c3972de886405473 to your computer and use it in GitHub Desktop.
Save hawkz/927316f2919e7f38c3972de886405473 to your computer and use it in GitHub Desktop.
Quick Python game
#!/usr/bin/python3
"""An LED wizard battle."""
import random
def reset(beam=[0] * 30):
"""Start the battle."""
beam = ['-'] * int(len(beam) / 2) + ['-'] * int(len(beam) / 2)
beam[int(len(beam) / 2)] = '*'
return beam
def paint(beam, p1_colour='-', p2_colour='-', position=14):
"""Output game state with player colours (-=≡) and position."""
return([p1_colour] * position + [p2_colour] * (len(beam) - position))
def play():
"""Main game loop. Rock paper scissors over time."""
beam = [0] * 30
beam = reset(beam)
print(beam)
position = int(len(beam) / 2)
beam = paint(beam, '=', '≡', position)
print(beam, len(beam))
while True:
p1 = random.randint(1, 3)
p2 = random.randint(1, 3)
if p1 > p2:
position += 1
elif p2 > p1:
position -= 1
if position == -1:
print('P2 wins')
break
elif position == len(beam) + 1:
print('P1 Wins')
break
beam = paint(beam, '1', '2', position)
print(beam, beam.count('2'), len(beam))
if __name__ == '__main__':
play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment