• 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
K1NNY

While loop trouble

12 posts in this topic

<p>I&#39;m making a simulation game where you control a nation and watch as it progresses through the years and you can make decisions when certain things happen.<br />
<br />
I basically have a while loop to handle the main part of the game (which is normal, i think). But in my while loop i have it set to where when a variable reaches a certain point, it goes to another while loop handling a seperate part of the game that gives you two options (to help your nation out or not). This works perfectly except that in this second while loop, i&#39;m trying to make it where once the user chooses an option it will go back to the first while loop, making the changes the user selected and run the same way it did before.<br />
<br />
The problem i&#39;m having is that the program doesn&#39;t seem to even pick up the user hitting the key, it doesn&#39;t hang or close, it just sits there running the second while loop.<br />
<br />
Here is the two while loops (i can add the whole code if needed, but its pretty big and would take up a lot of space):<br />
<br />
[source lang=&quot;python&quot;]#################################Game loop##################################<br />
elect_new = False<br />
<br />
<br />
while game:<br />
x = random.randint(1, 10)<br />
y = random.randint(1, 3)<br />
<br />
<br />
<br />
show_year = fontObj1.render(str(year), True, BLACK)<br />
show_pop = fontObj1.render(str(pop), True, BLACK)<br />
<br />
<br />
DISPLAY.blit(gbg, (0, 0))<br />
DISPLAY.blit(controls_1, (225, 50))<br />
DISPLAY.blit(controls_2, (225, 66))<br />
DISPLAY.blit(controls_3, (225, 82))<br />
DISPLAY.blit(controls_4, (225, 100))<br />
<br />
<br />
for event in pygame.event.get():<br />
<br />
#the pplrty determiner<br />
if event.type == pygame.KEYDOWN:<br />
if event.key == pygame.K_RETURN:<br />
year += 1<br />
<br />
DISPLAY.blit(show_year, (563, 400))<br />
DISPLAY.blit(show_pop, (400, 400))<br />
<br />
if x == 1:<br />
DISPLAY.blit(events_1, (45, 50))<br />
DISPLAY.blit(events_1_2, (45, 66))<br />
DISPLAY.blit(events_1_3, (45, 82))<br />
p += 2<br />
if x == 2:<br />
DISPLAY.blit(events_2, (45, 50))<br />
DISPLAY.blit(events_2_2, (45, 66))<br />
DISPLAY.blit(events_2_3, (45, 82))<br />
p -= 2<br />
if x == 3:<br />
DISPLAY.blit(events_3, (45, 50))<br />
DISPLAY.blit(events_3_2, (45, 66))<br />
DISPLAY.blit(events_3_3, (45, 82))<br />
p -= 3<br />
if x == 4:<br />
DISPLAY.blit(events_4, (45, 50))<br />
DISPLAY.blit(events_4_2, (45, 66))<br />
DISPLAY.blit(events_4_3, (45, 82))<br />
DISPLAY.blit(events_4_4, (45, 98))<br />
DISPLAY.blit(events_4_5, (45, 114))<br />
DISPLAY.blit(events_4_6, (45, 130))<br />
p -= 5<br />
if x == 5:<br />
DISPLAY.blit(events_5, (45, 50))<br />
DISPLAY.blit(events_5_2, (45, 66))<br />
p -= 5<br />
if x == 6:<br />
DISPLAY.blit(events_6, (45, 50))<br />
DISPLAY.blit(events_6_2, (45, 66))<br />
DISPLAY.blit(events_6_3, (45, 82))<br />
DISPLAY.blit(events_6_4, (45, 98))<br />
DISPLAY.blit(events_6_5, (45, 114))<br />
p -= 4<br />
if x == 7:<br />
DISPLAY.blit(events_7, (45, 50))<br />
DISPLAY.blit(events_7_2, (45, 66))<br />
DISPLAY.blit(events_7_3, (45, 82))<br />
DISPLAY.blit(events_7_4, (45, 98))<br />
p -= 4<br />
if x == 8:<br />
DISPLAY.blit(events_8, (45, 50))<br />
DISPLAY.blit(events_8_2, (45, 66))<br />
p -= 2<br />
if x == 9:<br />
DISPLAY.blit(events_9, (45, 50))<br />
DISPLAY.blit(events_9_2, (45, 66))<br />
DISPLAY.blit(events_9_3, (45, 82))<br />
DISPLAY.blit(events_9_4, (45, 98))<br />
p -= 6<br />
if x == 10:<br />
DISPLAY.blit(events_10, (45, 50))<br />
DISPLAY.blit(events_10_2, (45, 66))<br />
DISPLAY.blit(events_10_3, (45, 82))<br />
p += 0<br />
<br />
if p &gt;= 50 and p &lt; 60:<br />
DISPLAY.blit(pplrty_decent, (225, 400))<br />
elif p &gt;= 60 and p &lt; 85:<br />
DISPLAY.blit(pplrty_good, (230, 400))<br />
elif p &gt;= 85 and p &lt;= 100:<br />
DISPLAY.blit(pplrty_great, (230, 400))<br />
elif p &gt;= 25 and p &lt; 50:<br />
DISPLAY.blit(pplrty_bad, (225, 400))<br />
elif p &lt; 25:<br />
DISPLAY.blit(pplrty_awful, (225, 400))<br />
<br />
pygame.display.update()<br />
<br />
if event.type == QUIT:<br />
pygame.quit()<br />
sys.exit()<br />
[/source]</p>
<p> </p>
<p>It wouldn&#39;t let me put i into one code segment so here it is continued:</p>
<pre>
[source lang=&quot;python&quot;] #this is where i handle whether the loop needs to break and move on
#to the &quot;elect_new&quot; loop
if p &lt;= 15 and y == 1:
game = False
elect_new = True


while elect_new:
DISPLAY.blit(help_bg, (0, 0))
DISPLAY.blit(elect_1, (165, 75))
DISPLAY.blit(elect_2, (165, 93))
DISPLAY.blit(elect_3, (165, 111))
DISPLAY.blit(yes, (285, 350))
DISPLAY.blit(no, (335, 350))
DISPLAY.blit(under, (285, 350))
DISPLAY.blit(under, (335, 350))
pygame.display.update()

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
p += 30
elect_new = False
game = True

if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
[/source]</pre> Edited by K1NNY
0

Share this post


Link to post
Share on other sites
What is the scope of this code? That is, is it in a function? Script only? Also,where is game declared because if it is initialized as false then it will skip the first while loop.
You are also only rendering in the first loop whenever the user presses enter as seen below:
[source lang="python"] for event in pygame.event.get():

#the pplrty determiner
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
year += 1
# Drawing/Reaction code here...
pygame.display.update()[/source]
Is this intended?
Does it catch any of the inputs? That is, have you tried this:
[source lang="python"]for event in pygame.event.get():

#the pplrty determiner
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
print "RETURN Key Hit."[/source]
0

Share this post


Link to post
Share on other sites
[quote name='slicksk8te' timestamp='1350060450' post='4989509']
What is the scope of this code? That is, is it in a function? Script only? Also,where is game declared because if it is initialized as false then it will skip the first while loop.
You are also only rendering in the first loop whenever the user presses enter as seen below:
[source lang="python"] for event in pygame.event.get():

#the pplrty determiner
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
year += 1
# Drawing/Reaction code here...
pygame.display.update()[/source]
Is this intended?
Does it catch any of the inputs? That is, have you tried this:
[source lang="python"]for event in pygame.event.get():

#the pplrty determiner
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
print "RETURN Key Hit."[/source]
[/quote]

The entire source code is too big to put on here but game is initialized in a while loop before it that controls the starting screen of the game. And yes that is intentional because otherwise certain things are not drawn to the screen correctly.

And yes it does catch input, i had it print "x" earlier everytime the user hit enter and it printed everytime. Edited by K1NNY
0

Share this post


Link to post
Share on other sites
It is possible that it is always going to the second while loop because of this:

[source lang="python"]if p < 15 and y == 1:
game = False
elect_new = True[/source]
(There is a weird glitch with this code block, if i use "p <= 15" it fails to display so assume it is "p <= 15" instead of "p < 15")


Is this code supposed to be evaluated every loop iteration?
What is the initial value of p? This code is evaluated every iteration and if p is initialized as <= 15 then this will evaluate true almost immediately because y is determined by:
[source lang="python"]y = random.randint(1, 3)[/source]
Which is also evaluated every loop and because the loop time is really fast, as in < 10ms (this may be faster because you have no sleep/fps timing), y will be 1 in about 3-4 loops. so it would take about 30-40ms to evaluate true and jump to the second loop. Edited by slicksk8te
0

Share this post


Link to post
Share on other sites
[quote name='slicksk8te' timestamp='1350061736' post='4989519']
It is possible that it is always going to the second while loop because of this:

[source lang="python"]if p < 15 and y == 1:
game = False
elect_new = True[/source]
(There is a weird glitch with this code block, if i use "p <= 15" it fails to display so assume it is "p <= 15" instead of "p < 15")


Is this code supposed to be evaluated every loop iteration?
What is the initial value of p? This code is evaluated every iteration and if p is initialized as <= 15 then this will evaluate true almost immediately because y is determined by:
[source lang="python"]y = random.randint(1, 3)[/source]
Which is also evaluated every loop and because the loop time is really fast, as in < 10ms (this may be faster because you have no sleep/fps timing), y will be 1 in about 3-4 loops. so it would take about 30-40ms to evaluate true and jump to the second loop.
[/quote]

The initial value of p is 50 and is changed according to x (as shown in the "game" loop). Yes it is supposed to be evaluated every loop because when i tried doing it otherwise it didn't work (x stayed one number the whole time).

Is it possible that the reason it refuses to return to the "game" loop from the "new_elect" loop because the "new_elect" loop is at a lower position in the text than the "game" loop? so by the time it sees to return to the "game" loop it can't? Edited by K1NNY
0

Share this post


Link to post
Share on other sites
This was an accidental post and i cant delete it. Edited by K1NNY
0

Share this post


Link to post
Share on other sites
From what I understand now both while loops are not inside an outer loop are they? If they are, ensure that the code loops back around to the beginning and that p is above 15. If not you need a while loop that wraps around both inner while loops.
The reason why I suspect this is because you have no indent in front of your while loops in your pasted code. Did you remove the indentation when you pasted the code?
0

Share this post


Link to post
Share on other sites
They are not in a while a loop, they are on their own. So i should put them in a while loop? Should i put them in my main loop? because it is just sitting at the bottom doing nothing.
0

Share this post


Link to post
Share on other sites
Yes you need to follow this format for this to work correctly:
[source lang="python"]#Initialize here

while True:
#Some code can go here
while game:
# First while loop code here
# Transition code here
while elect_new:
# Second while loop code here
# More Transition code here[/source]

If you do not have an outer while loop, it will not return back to the first loop.
0

Share this post


Link to post
Share on other sites
This worked perfectly until i tried implementing a new part of the game. I need it to be able to control how many times it can access "elect_new". I tried this:

[source lang="python"]help_credits = 5

while True:
while game:

if help_credits >= 1:
help_events = True

elif help_credits == 0:
help_events = False

[/source]

[source lang="python"] while help_events:

if p < 15 and y == 1:
game = False
elect_new = True
elif p < 15 and y == 2:
game = False
lower_int = True
elif p < 15 and y == 3:
game = False
cut_taxes = True[/source]

Of course i cut out most of the code only leaving in the new parts i added. Using this, the program just hangs and becomes unresponsive. (sorry for it being in two parts again, look at it like one piece of code because the code segment insert thing isn't working for me at all)

CORRECTION: The program doesn't go to the "elect_new" loop at all now using this. Edited by K1NNY
0

Share this post


Link to post
Share on other sites
I'm Unable to see the second part of the code. I had this problem before, try an replace all <= into < because the code tags break when they see <= for some reason. It is also better for help_credits to be compared to zero like this: help_credits <= 0 because this is safer than help_credits == 0. I'm going to have to see the rest of the code before I can tell why it is unresponsive.
0

Share this post


Link to post
Share on other sites
Here is the full code (excluding all the text surfaces) all >= have been replaced with >:

[source lang="python"]import pygame, random
import sys
from pygame.locals import *

pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

#variables
year = 2011
p = 50
pop = 100000
help_credits = 5

#definition time



#get colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
DARKRED = (128, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)

#get display stuff
DISPLAY = pygame.display.set_mode((640,480))
pygame.display.set_caption('UTOPIA')
pygame.mouse.set_visible(True)

#create fonts
fontObj1 = pygame.font.Font('freesansbold.ttf', 16)
fontObj2 = pygame.font.Font('freesansbold.ttf', 12)


#make words


#get images
title = pygame.image.load('title.png')
stitle = pygame.transform.scale(title, (264, 64))

bg = pygame.image.load('bg.png')
sbg = pygame.transform.scale(bg, (640, 480))
gbg = pygame.image.load('Game_bg.png')
help_bg_root = pygame.image.load('help_bg.png')
help_bg = pygame.transform.scale(help_bg_root, (640, 480))



#Menu screen
start_screen = True

while start_screen:
DISPLAY.blit(sbg, (0, 0))
DISPLAY.blit(stitle, (188, 10))
DISPLAY.blit(textObj1, RectObj1)


for event in pygame.event.get():

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_RETURN:
start_screen = False
instructions = True

if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()





#################################Game loop######################################



#Main Loop


while True:
if help_credits > 1:
help_events = True

elif help_credits == 0:
help_events = False

while instructions:
DISPLAY.blit(sbg, (0, 0))
DISPLAY.blit(instructions_1, (100, 36))
DISPLAY.blit(instructions_2, (9, 54))
DISPLAY.blit(instructions_3, (175, 72))
DISPLAY.blit(instructions_4, (85, 100))
DISPLAY.blit(instructions_5, (78, 118))
DISPLAY.blit(instructions_6, (40, 136))
DISPLAY.blit(instructions_7, (260, 154))
DISPLAY.blit(instructions_8, (310, 154))
DISPLAY.blit(instructions_9, (350, 154))


for event in pygame.event.get():

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_RETURN:
instructions = False
game = True

if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

elect_new = False
lower_int = False
cut_taxes = False


while game:
x = random.randint(1, 10)
y = random.randint(1, 3)



show_year = fontObj1.render(str(year), True, BLACK)
show_pop = fontObj1.render(str(pop), True, BLACK)


DISPLAY.blit(gbg, (0, 0))
DISPLAY.blit(controls_1, (225, 50))
DISPLAY.blit(controls_2, (225, 66))
DISPLAY.blit(controls_3, (225, 82))
DISPLAY.blit(controls_4, (225, 100))




for event in pygame.event.get():

#the pplrty determiner
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
year += 1
print year
print x
print p
DISPLAY.blit(show_year, (563, 400))
DISPLAY.blit(show_pop, (400, 400))

if x == 1:
DISPLAY.blit(events_1, (45, 50))
DISPLAY.blit(events_1_2, (45, 66))
DISPLAY.blit(events_1_3, (45, 82))
p += 2
if x == 2:
DISPLAY.blit(events_2, (45, 50))
DISPLAY.blit(events_2_2, (45, 66))
DISPLAY.blit(events_2_3, (45, 82))
p -= 2
if x == 3:
DISPLAY.blit(events_3, (45, 50))
DISPLAY.blit(events_3_2, (45, 66))
DISPLAY.blit(events_3_3, (45, 82))
p -= 3
if x == 4:
DISPLAY.blit(events_4, (45, 50))
DISPLAY.blit(events_4_2, (45, 66))
DISPLAY.blit(events_4_3, (45, 82))
DISPLAY.blit(events_4_4, (45, 98))
DISPLAY.blit(events_4_5, (45, 114))
DISPLAY.blit(events_4_6, (45, 130))
p -= 5
if x == 5:
DISPLAY.blit(events_5, (45, 50))
DISPLAY.blit(events_5_2, (45, 66))
p -= 5
if x == 6:
DISPLAY.blit(events_6, (45, 50))
DISPLAY.blit(events_6_2, (45, 66))
DISPLAY.blit(events_6_3, (45, 82))
DISPLAY.blit(events_6_4, (45, 98))
DISPLAY.blit(events_6_5, (45, 114))
p -= 4
if x == 7:
DISPLAY.blit(events_7, (45, 50))
DISPLAY.blit(events_7_2, (45, 66))
DISPLAY.blit(events_7_3, (45, 82))
DISPLAY.blit(events_7_4, (45, 98))
p -= 4
if x == 8:
DISPLAY.blit(events_8, (45, 50))
DISPLAY.blit(events_8_2, (45, 66))
p -= 2
if x == 9:
DISPLAY.blit(events_9, (45, 50))
DISPLAY.blit(events_9_2, (45, 66))
DISPLAY.blit(events_9_3, (45, 82))
DISPLAY.blit(events_9_4, (45, 98))
p -= 6
if x == 10:
DISPLAY.blit(events_10, (45, 50))
DISPLAY.blit(events_10_2, (45, 66))
DISPLAY.blit(events_10_3, (45, 82))
p += 0

if p > 50 and p < 60:
DISPLAY.blit(pplrty_decent, (225, 400))
pop += 10000
elif p > 60 and p < 85:
DISPLAY.blit(pplrty_good, (230, 400))
pop += 30000
elif p > 85 and p < 100:
DISPLAY.blit(pplrty_great, (230, 400))
pop += 50000
elif p > 25 and p < 50:
DISPLAY.blit(pplrty_bad, (225, 400))
pop -= 10000
elif p < 25:
DISPLAY.blit(pplrty_awful, (225, 400))
pop -= 30000


pygame.display.update()


if event.type == QUIT:
pygame.quit()
sys.exit()

while help_events:

if p < 15 and y == 1:
game = False
elect_new = True
elif p < 15 and y == 2:
game = False
lower_int = True
elif p < 15 and y == 3:
game = False
cut_taxes = True


while elect_new:
DISPLAY.blit(help_bg, (0, 0))
DISPLAY.blit(elect_1, (165, 75))
DISPLAY.blit(elect_2, (165, 93))
DISPLAY.blit(elect_3, (165, 111))
DISPLAY.blit(yes, (300, 350))
DISPLAY.blit(under, (300, 350))
pygame.display.update()

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
DISPLAY.blit(enter, (285, 370))
p += 30
help_credits -= 1
elect_new = False
game = True


if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

while lower_int:
DISPLAY.blit(help_bg, (0, 0))
DISPLAY.blit(elect_1, (165, 75))
DISPLAY.blit(lower_int_1, (165, 93))
DISPLAY.blit(lower_int_2, (165, 111))
DISPLAY.blit(lower_int_3, (165, 129))
DISPLAY.blit(yes, (300, 350))
DISPLAY.blit(under, (300, 350))
pygame.display.update()

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
DISPLAY.blit(enter, (285, 370))
p += 30
help_credits -= 1
lower_int = False
game = True


if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

while cut_taxes:
DISPLAY.blit(help_bg, (0, 0))
DISPLAY.blit(elect_1, (165, 75))
DISPLAY.blit(cut_taxes_1, (165, 93))
DISPLAY.blit(cut_taxes_2, (165, 111))
DISPLAY.blit(cut_taxes_3, (165, 129))
DISPLAY.blit(yes, (300, 350))
DISPLAY.blit(under, (300, 350))



for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
DISPLAY.blit(enter, (285, 370))
p += 30
help_credits -= 1
cut_taxes = False
game = True


if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

for event in pygame.event.get():

if event.type == QUIT:
pygame.quit()
sys.exit()

pygame.display.update()
[/source]
0

Share this post


Link to post
Share on other sites
I thought hard on it overnight and fixed it this morning. Thanks for the help! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
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