#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# User input to global variables

edited December 2016

I've just started using processing and am currently working on creating a game similar to "Minesweeper". One of the things I need is for the user to decide on the grid size and the number of bombs but I can't find an example of how to do that anywhere.

As of right now these variables have a set value. "rader", antal_bomber" and "kolumner" are the ones who's value i want the user to decide, they represent the grid size and number of bombs. Everything else works as it should. Does anyone perhaps have any example of how this can be done?

Code:

``````from random import randint

bredd = 40

antal_bomber = 10 (first variable)

kolumner = 9 (third)

flags = 0

mode =  "pågående"

class Brickor:
def __init__(self):

self.bomb = False

self.label = None

self.visible = False

self.flagged = False

spelplan = [  [Brickor() for n in range(kolumner)] for n in range(rader) ]

for n in range (antal_bomber):

while True:
x = randint(0, 8)
y = randint(0, 8)
if spelplan[y][x].bomb == False:
spelplan[y][x].bomb = True
break

def setup():
size(360,360)

def draw():
background(100)

if mode =="pågående":
y= 0
x = 0
#ritar bomben för test
if ruta.bomb == True:
fill(255,0,0)
elif ruta.visible:
fill(255)
elif not ruta.visible:#Synlig
fill(150)
if ruta.flagged:
fill(0)

text("X",x+bredd/2,y+bredd/2)
rect(x,y,bredd,bredd)

# Närliggande bomber
fill(0,0,0)
if ruta.label != None:
text(ruta.label,x+bredd/2,y+bredd/2)

x+=bredd
y+=bredd
if mode =="vann":
text("You won",100,100)
if mode =="förlust":
text("You lost :(", 100, 100)

def mouse_to_index():
x = mouseX/bredd
y = mouseY/bredd
return(x,y)

# Regestrerar vart man klickar via pixlar/bredden av spelplanen
def mousePressed():
global mode
global flags
(x,y) = mouse_to_index()

if mode == "förlust":

if mouseButton == LEFT:
ruta=spelplan[y][x]
if ruta.bomb == True:
mode ="förlust"
else:
search(x,y)

elif mouseButton == RIGHT:
ruta = spelplan[y][x]
flags += 1
if flags > 10:
ruta.flagged = False
else:
ruta.flagged = True

game_won = True
if t.bomb and not t.flagged:
game_won = False
if game_won:
mode = "vann"

def search(x,y):

#Ser om det är en bomb/ en bomb i närheten eller om rutan inte har några bomber på sig
if not inne(x,y):
return
ruta = spelplan[y][x]

if ruta.visible:
return

if ruta.bomb:
mode = "förlust"
return

ruta.visible = True
if s > 0:
spelplan[y][x].label = s
return
for (dx,dy) in [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]:
search(x+dx,y+dy)

s = 0
for (dx,dy) in [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]:
if inne(x+dx, y+dy) and spelplan[y+dy][x+dx].bomb:
s+=1
return s
# if s > 0:
#   spelplan[y][x].label = s

# Säkerhets kod för att se så at tman alltid klickar på spelplanen
def inne(x,y):
if x >= 0 and x < kolumner and y >= 0 and y < rader:
return True
return False
``````
Tagged:

• You should format your code.
Edit your post by pressing the gear icon on top right of post.
Select the code, press ctrl + o to indent.
Leave a line above and below.
Remember that the easier it is to read you code, the more the people will help you.

• edited December 2016

Thanks for the tip

`rader` och `kolumner` = rows and columns, yes?
You need to create a class Spelplan which takes rader and kolumner as constructor arguments. Then, for example, if the user keypresses "5" reset the game and create a new `Spelplan spelplan` 5x5. If they press `9` make it 9x9.