User input to global variables

edited December 2016 in Python Mode

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?


from random import randint

bredd = 40

antal_bomber = 10 (first variable)

rader = 9 (second)

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

def setup():

def draw():

    if mode =="pågående":
        y= 0
        for rad in spelplan:
            x = 0
            for ruta in rad:
                #ritar bomben för test
                if ruta.bomb == True:
                elif ruta.visible:
                elif not ruta.visible:#Synlig
                if ruta.flagged:


                # Närliggande bomber
                if ruta.label != None:

    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

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

    if mode == "förlust":
        text("Du förlorade tyvärr", 100,100)

    if mouseButton == LEFT:
        if ruta.bomb == True:
            mode ="förlust"

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

        game_won = True
        for rad in spelplan:
            for t in rad:
                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):
    ruta = spelplan[y][x]

    if ruta.visible:

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

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

def adjacent_bombs(x,y):
    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:
    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


  • 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

  • Your welcome. I, however, do not use Python, so I cannot help you much.

  • edited December 2016 Answer ✓

    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.

    You can add more interface for choosing grid arguments (buttons, menus) later, but perhaps start there.

Sign In or Register to comment.