Random number assignment help?

This is the place for queries that don't fit in any of the other categories.

Random number assignment help?

Postby Kegorian » Tue Jul 02, 2013 5:24 pm

Hello, I'm currently learning python. I've been using it for around three days or so now, going at it slow. I'm currently attempting to make a simple slot machine game.

I'm using pygame to do all the drawing etc, but I've stumbled across a problem. I'm using a random number which then assigns a different colour to the Icon which gets spins. In other words, I draw 9 circles always in the same place, but then the colour changes depending on the random number.

I've done a little bit of testing, but the colours constantly change, and I don't know how to keep them the same, until the person presses spin again.

Note, my program is very bare as I was going to get it all drawn out then implement the game logic.

Code: Select all
 
# Spin Icons
for i in range(1, 4):
    for j in range(1, 4):
        randIconColour = random.randrange(5) # Creates a random colour
        randColour = [green, purple, yellow, blue, pink]
           
        pygame.draw.ellipse(screen, randColour[randIconColour], [((85 * i) + (135 * (i - 1))), (117 * j), 80, 80])


This is the current code for drawing and creating the random colour, all of this is inside a game loop.

Thanks for any help, it would be much appreciated.
Kegorian
 
Posts: 4
Joined: Tue Jul 02, 2013 5:15 pm

Re: Random number assignment help?

Postby Yoriz » Tue Jul 02, 2013 5:35 pm

Hi,

You probably need an 'if' condition to check for currently spinning state.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Random number assignment help?

Postby Kegorian » Tue Jul 02, 2013 5:44 pm

I'm not even up to that bit yet. My problem is that, because the random.randrange is within the game-loop every frame, it draws a different colour, however I need at least 9 random numbers that change one for every icon.

I'm not too good at explaining things, so if you want to the rest of my code, just ask :)
Kegorian
 
Posts: 4
Joined: Tue Jul 02, 2013 5:15 pm

Re: Random number assignment help?

Postby Yoriz » Tue Jul 02, 2013 6:07 pm

If i understand correctly.
Create a function that makes a single random colour, call that function 9 times to create 9 random colours.
something like this ?
Code: Select all
import random


def createSingleColour():
    return random.choice(('green', 'purple', 'yellow', 'blue', 'pink'))

colours = [createSingleColour() for i in range(9)]
print colours

Code: Select all
['purple', 'yellow', 'purple', 'blue', 'blue', 'purple', 'purple', 'yellow', 'blue']
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Random number assignment help?

Postby Kegorian » Tue Jul 02, 2013 6:48 pm

This may help. Since I've only been going a few days I haven't came across functions yet. But I'll skip to that part so I know what I'm doing with this code. I'll give it a try next time I have some free time and let you know if it works or not.
Kegorian
 
Posts: 4
Joined: Tue Jul 02, 2013 5:15 pm

Re: Random number assignment help?

Postby Yoriz » Tue Jul 02, 2013 6:55 pm

Ok here is the same code simplified with no functions and a normal loop
Code: Select all
colours = []
for i in range(9):
    colour = random.choice(('green', 'purple', 'yellow', 'blue', 'pink'))
    colours.append(colour)
print colours

Code: Select all
['blue', 'blue', 'green', 'blue', 'purple', 'purple', 'yellow', 'yellow', 'green']
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Random number assignment help?

Postby Kegorian » Tue Jul 02, 2013 7:25 pm

This version is a lot better. Had some trouble but managed to get the other working. However for now at least. I'm going to swap to this one. Thanks for all the help
Kegorian
 
Posts: 4
Joined: Tue Jul 02, 2013 5:15 pm


Return to General Coding Help

Who is online

Users browsing this forum: buran and 4 guests