Posts: 14
Threads: 7
Joined: Dec 2020
Jan-27-2021, 09:01 PM
(This post was last modified: Jan-27-2021, 09:20 PM by AnunnakiKungFu.)
I am trying to make a Russian roulette app and I do not know where to start. My biggest issue is figuring out how to actually run the random choice of five empty chambers and a loaded one. Would I like, make a list and have the shuffle module choose from one of the six options in the list?
spin = ['ec1','ec2','ec3','ec4','ec5','DEAD']
and then have it shuffle through that? Or am I overthinking this?
Posts: 3,458
Threads: 101
Joined: Sep 2016
You could use random.choice(), to pick a random value each time. That would be easy, but wouldn't reflect real life, since you can hit the same chamber multiple times.
>>> import random
>>> options = ['1', '2', '3', '4', '5', 'BANG']
>>> random.shuffle(options)
>>> options
['3', '1', '4', 'BANG', '2', '5']
>>> while options:
... result = options.pop()
... print(result)
... if "BANG" == result:
... print("you died :(")
... break
...
5
2
BANG
you died :(
Posts: 14
Threads: 7
Joined: Dec 2020
(Jan-27-2021, 09:34 PM)nilamo Wrote: You could use random.choice(), to pick a random value each time. That would be easy, but wouldn't reflect real life, since you can hit the same chamber multiple times.
>>> import random
>>> options = ['1', '2', '3', '4', '5', 'BANG']
>>> random.shuffle(options)
>>> options
['3', '1', '4', 'BANG', '2', '5']
>>> while options:
... result = options.pop()
... print(result)
... if "BANG" == result:
... print("you died :(")
... break
...
5
2
BANG
you died :(
Sorry for this noob question, but I am way new to programming. What does "break" do exactly?
Posts: 3,458
Threads: 101
Joined: Sep 2016
break stops the loop earlier than it otherwise would.
Here's an example using a for loop, with and without break:
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
>>> for i in range(5):
... print(i)
... if i >= 2:
... break
...
0
1
2
Posts: 14
Threads: 7
Joined: Dec 2020
Jan-27-2021, 10:20 PM
(This post was last modified: Jan-27-2021, 10:21 PM by AnunnakiKungFu.)
Well, thus far, without having applied any advice, this is what I have:
from tkinter import *
import random
root = Tk()
root.title("Russian Roulette")
root.geometry("250x300")
button_Spin = Button(root, text="Spin", width=10, height=3)
button_Spin.grid(row=0, column=0)
button_Fire = Button(root, text="Fire", width=10, height=3)
button_Fire.grid(row=0, column=1)
root.mainloop() So, I want to make a function that shuffles through a list and then only one option outputs into an output box under the buttons. If I was going to make a function to add to the "Fire" button, that shuffles the list and outputs one item, what would it look like in this code I have provided?
And one more question. I am wondering how I would go about adding sounds to the button clicks. Obviously, I want to add the sound of a chamber spinning, which is all the "Spin" button will actually do. Then one the "Fire" button is clicked, it will play either an empty chamber fire or a gun blast depending on which item the list shuffles onto.
Posts: 8,085
Threads: 153
Joined: Sep 2016
You can use deque and replicate rotation of the chamber
from collections import deque
import random
chamber = [False]*5 + [True]
random.shuffle(chamber)
gun = deque(chamber)
print(gun)
while True:
play = input('Do you want to pull the trigger? (q for quit)')
if play.lower() == 'q':
break
else:
if gun[0]:
print('BANG')
break
else:
print('click')
gun.rotate(-1)
Posts: 6,120
Threads: 16
Joined: Feb 2020
Feb-17-2021, 11:38 PM
(This post was last modified: Feb-17-2021, 11:38 PM by deanhystad.)
The program is simpler if you always put the bullet in the same chamber, in particular chamber 0. Spinning the cylinder selects a random number in the range 0 to 5. This is the number of the chamber in firing position.
chamber = random.randint(0, 5) Pulling the trigger checks if the chamber is 0 (bang) or not (click).
if chamber:
print('click')
else:
print('BANG!')
chamber = (chamber + 1) % 6 There is need for a list or a deque.
If you spun the cylinder and it came up 3, the following events would be:
pull_trigger, 3 != 0, Click, chamber = (chamber + 1) % 6 == 4
pull_trigger, 4 != 0, Click, chamber = (chamber + 1) % 6 == 5
pull_trigger, 5 != 0, Click, chamber = (chamber + 1) % 6 == 0
pull_trigger, 0 == 0, Bang, chamber = (chamber + 1) % 6 == 1
|