• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Dalphin

unassinged in Python

7 posts in this topic

Hello everyone,

I'm making a small game for kids in python, but i have a problem.

my code is:

import random
import time
    
def geslaagd():
    print("goed gedaan")
    gevallenOpgelost = gevallenOpgelost +1

def gefaald():
    print("Helaas de patiënt is overleden")
    gevallenGefaald = gevallenGefaald +1
    
def game():
    print("Welkom, dokter, dit is de EHBO-afdeling van het ziekenhuis.")
    time.sleep(2)
    print("Vanaf nu is dit uw nieuwe werkplek en neemt u de beslissingen over de spoedgevallen.")
    time.sleep(2)
    print("Ik wens u succes!")
    time.sleep(2)
    print("En oja, mochten er mensen komen te overlijden door uw schuld, wordt u per direct ontslagen, dus verpest het niet!")
    time.sleep(2)

    gevallenOpgelost = 0
    gevallenGefaald = 0

    
    while gevallenGefaald == 0:
        situatie = random.choice (["gebroken arm", "gebroken been", 'hartaanval', "plotselinge bevalling", 'allergische reactie'])
        print ("Attentie, dokter, we hebben een spoedgeval. Er is iemand binnengekomen met een " + situatie)
        print("Wat gaat u doen? (kies uit: verbinden en een mitella eromheen (1), verbinden en gips eromheen (2), aan een monitor op de intensive-care (3), een kraamvrouw inroepen (4), een tegengif injecteren (5).)")

        if situatie == "gebroken arm":
            oplossingArm = input()
            oplossingArm = int(oplossingArm)
            if oplossingArm == 1:
                print("goed gedaan")
                gevallenOpgelost = gevallenOpgelost +1
        

            if oplossingArm != 1:
                print("Helaas de patient is overleden")
                gevallenGefaald = gevallenGefaald +1

        if situatie == "gebroken been":
            oplossingBeen = input()
            oplossingBeen = int(oplossingBeen)
            if oplossingBeen == 2:
                print("goed gedaan")
                gevallenOpgelost = gevallenOpgelost +1

            if oplossingBeen != 2:
                print("Helaas de patient is overleden.")
                gevallenGefaald = gevallenGefaald +1

        if situatie == 'hartaanval':
            oplossingHart = input()
            oplossingHart = int(oplossingHart)
            if oplossingHart == 3:
                    print("goed gedaan")
                    gevallenOpgelost = gevallenOpgelost +1

            if oplossingHart != 3:
                    print("Helaas de patient is overleden.")
                    gevallenGefaald = gevallenGefaald +1
    
        if situatie == "plotselinge bevalling":
            oplossingBevalling = input()
            oplossingBevalling = int(oplossingBevalling)
            if oplossingBevalling == 4:
                print("goed gedaan")
                gevallenOpgelost = gevallenOpgelost +1

            if oplossingBevalling != 4:
                print("Helaas de patient is overleden")
                gevallenGefaald = gevallenGefaald +1

        if situatie == "allergische reactie":
            oplossingAllergie = input()
            oplossingAllergie = int(oplossingAllergie)
            if oplossingAllergie == 5:
                geslaagd()

            if oplossingAllergie != 5:
                gefaald()
        
        

    gevallenOpgelost = str(gevallenOpgelost)
    print("Dokter, er zijn door uw schuld te veel patiënten overleden. U bent onslagen.")
    print("Het aantal patiënten dat u succesvol heeft geholpen is " +gevallenOpgelost)

opnieuw = 'ja'
while opnieuw == 'ja':

    game()

    print("Wil je opnieuw spelen (ja/nee)")
    opnieuw = input()

if opnieuw == 'nee':
    print("Bedankt voor het spelen van dit spel.")

(BTW i'm sorry it isn't english, but kids in my country have to understand it. So i hope you can still do something with it after you hear my explanation)

 

If i run this program, evrything works great, till i get to the read marked lines.

These are the first lines in which i use a defenition, but i geat this error:

 
Traceback (most recent call last):
  File "D:\game werkbank\python\EHBOtext-basedgameV1.0.py", line 95, in <module>
    game()
  File "D:\game werkbank\python\EHBOtext-basedgameV1.0.py", line 81, in game
    geslaagd()
  File "D:\game werkbank\python\EHBOtext-basedgameV1.0.py", line 7, in geslaagd
    gevallenOpgelost = gevallenOpgelost +1
UnboundLocalError: local variable 'gevallenOpgelost' referenced before assignment
 
I understand it's caused because i used it before i explained that the value has to be 0 at the beginning of the game and i tried to fix it, but i couldn't. So what is the way to explain it before using it,
 
I fully understand if you don't understand something of what i'm trying to say, if you ask it i can try to explain that part better.
Thanks
 

 

0

Share this post


Link to post
Share on other sites

Besides explicitly setting gevallenOpgelost = 0 at the top of the file, you may need to declare it global in the functions that use it.  (Don't forget to declare it global in game() either)

def geslaagd():
  global gevallenOpgelost 
  print("goed gedaan")
  gevallenOpgelost = gevallenOpgelost +1
Edited by BeerNutts
0

Share this post


Link to post
Share on other sites

I think you should explicitly declare your variable as global.

var = 0

def my_fun():
    global var
    var += 1

my_fun()
print var
Edited by Lysy
0

Share this post


Link to post
Share on other sites

Thanks for the reactions, but i still don't understand where i have to change it then in game(). Sorry BTW, but i'm learning python now and this is just to try something to understand how it works.

0

Share this post


Link to post
Share on other sites

Put this two:

gevallenOpgelost = 0
gevallenGefaald = 0

before any function (i.e just after imports), then in every function you have, in the first line of each of them, write:

global gevallenOpgelost
global gevallenGefaald

This should to the trick

0

Share this post


Link to post
Share on other sites

Just a couple little things I noted in the code ( not nit-picking )

 

 1: This kind of code can lead to program crashes when dealing with user inputs and explicitly converting variable types

            oplossingArm = input()
            oplossingArm = int(oplossingArm)

When dealing with user input, it is always a good idea to use exception handling in case of incorrect input [ LINK ]

 

2: To save some typing, you can use "+=" in cases like this

gevallenOpgelost = gevallenOpgelost +1 // gevallenOpgelost += 1 is faster to type

3: If you are going to use a variable globally it should be initialized globally, instead of inside of a function ( definition ). This saves a lot of confusion ( and unexpected execution ) as the case of this post.

1

Share this post


Link to post
Share on other sites

okay thanks, i don't understand it completely, but that will come after i learn some more.

THANKS

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0