In the RPS game, I will be recording the number of times they win/draw before losing.
Here's the code:
# coding: utf-8
from scene import *
import random
import sys
choices = '๐', '๐', 'โ๏ธ'
def get_ruling(player_choice, opponent_choice):
if player_choice == opponent_choice:
return "Draw"
elif player_choice == '๐':
return 'You win' if opponent_choice == 'โ๏ธ' else 'You lose'
elif player_choice == '๐':
return 'You win' if opponent_choice == '๐' else 'You lose'
else: # player_choice == 'โ๏ธ'
return 'You win' if opponent_choice == '๐' else 'You lose'
class rps(Scene):
def __init__(self):
self.player = None
self.computer = None
def setup(self):
self.button = Button(Rect(self.size.w/2-60, self.size.h/1-150, 125, 125))
self.button.background = Color(0,0,0,0)
self.button.stroke = Color(0,0,0,0)
self.button.image = 'Moon_2'
self.button.action = self.rock_action
self.add_layer(self.button)
self.button1 = Button(Rect(self.size.w/2-60, self.size.h/1-300, 125, 125))
self.button1.background = Color(0,0,0,0)
self.button1.stroke = Color(0,0,0,0)
self.button1.image = 'Page_Facing_Up'
self.button1.action = self.paper_action
self.add_layer(self.button1)
self.button2 = Button(Rect(self.size.w/2-60, self.size.h/1-450, 125, 125))
self.button2.background = Color(0,0,0,0)
self.button2.stroke = Color(0,0,0,0)
self.button2.image = 'Scissors'
self.button2.action = self.scissors_action
self.add_layer(self.button2)
def rock_action(self):
self.button.background = Color(0,0,0,0)
self.player = '๐'
self.computer = random.choice(choices)
run(ending(self.player, self.computer))
def paper_action(self):
self.button1.background = Color(0,0,0,0)
self.player = '๐'
self.computer = random.choice(choices)
run(ending(self.player, self.computer))
def scissors_action(self):
self.button2.background = Color(0,0,0,0)
self.player = 'โ๏ธ'
self.computer = random.choice(choices)
run(ending(self.player, self.computer))
def draw(self):
background(0, 0.05, 0.2)
self.root_layer.update(self.dt)
self.root_layer.draw()
class ending(Scene):
def __init__(self, player, computer):
self.player = player
self.computer = computer
self.ruling = get_ruling(player, computer)
def setup(self):
self.next = Button(Rect(self.size.w/2+77, self.size.h/1-480, 80, 40), 'Next')
self.next.background = Color(1,1,1)
self.next.stroke = Color(1,1,1)
self.next.action = self.next_action
self.add_layer(self.next)
self.done = Button(Rect(self.size.w/2+77, self.size.h/1-480, 80, 40), 'Done')
self.done.background = Color(1,1,1)
self.done.stroke = Color(1,1,1)
self.done.action = self.done_action
global stre
stre = 0
global count
count = 0
global streak
streak = 0
def next_action(self):
self.next.background = Color(1,1,1)
run(rps(), PORTRAIT)
def done_action(self):
self.done.background = Color(1,1,1)
run(lose())
def draw(self):
background(0,0.05,0.2)
self.root_layer.update(self.dt)
self.root_layer.draw()
tint(0.80, 0.40, 1.00)
msg = '{}!'.format(self.ruling)
if str(msg) == 'You lose!':
self.add_layer(self.done)
global stre
global count
global streak
if str(msg) == 'You win!' or str(msg) == 'Draw!':
while count != 1:
stre = stre + 1
streak = streak + stre
count = count + 1
text(msg, x=self.size.w/3+50, y=self.size.h/5*3+60, font_size=70)
if self.player == '๐':
self.name = 'Rock'
if self.player == '๐':
self.name = 'Paper'
if self.player == 'โ๏ธ':
self.name = 'Scissors'
tint(1.00, 1.00, 0.00)
msg = '\n\n\n\nYou chose:\n\n\nComputer chose:\n'
name = '{}'.format(self.name)
text(msg, x=self.size.w/3+29, y=self.size.h/5*3, font_size=35)
tint(1,1,1)
text(name, x=self.size.w/3-10, y=self.size.h/5*3-70, font_size=50)
msg = '\n\n {}\n\n'.format(self.player)
text(msg, x=self.size.w/3+50, y=self.size.h/5*3-10, font_size=110)
if self.computer == '๐':
self.name3 = 'Rock'
if self.computer == '๐':
self.name3 = 'Paper'
if self.computer == 'โ๏ธ':
self.name3 = 'Scissors'
name3 = '{}'.format(self.name3)
msg = '\n\n\n\n {}'.format(self.computer)
text(msg, x=self.size.w/3+50, y=self.size.h/5*3-20, font_size=80)
text(name3, x=self.size.w/3-10, y=self.size.h/5*3-200, font_size=50)
if self.ruling == 'You lose!':
self.add_layer(self.done)
tint(0,1,0)
msg = 'Streak: {}'.format(streak)
text(msg, x=self.size.w/3-15, y=self.size.h/5*3+135, font_size=35)
run(rps(), PORTRAIT)
I've tried everything:
Flipping if and while statements
Changing the variables to None
Etc.
However I need it I be like this code:
count = 0
storage = 0
while count != 5:
c = raw_input('')
storage = storage + int(c)
count = count + 1
print(storage)
Where it adds up everything.
Thank in advance!
Also, if I am not explicit enough, please let me know