pygame time.sleep prioritised over the rest of the function
up vote
0
down vote
favorite
I am trying to create a quiz app where you press either one of two buttons. This is the MVC example. The main issue is with the 'correct' function. I intended to have it set question to false (which didn't solve the issue), fill the game display, then print out a message to the screen, and update the display, to have it wait 3 seconds before doing anything else. But instead, it waits 3 seconds, flashes the intended screen for a single tick then instantly goes back to the question function. Isn't python supposed to read the function line by line from top to bottom? Because right now it prioritises time.sleep over the rest of the instructions in the function
import time
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
def question():
global question
question = True
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(cyan)
button('text', 150, 500, 100, 50, darkYellow, yellow, action = 'correct')
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
pygame.display.update()
if click[0] == 1 and action != None:
if action == 'correct':
question = False
correct()
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
pygame.display.update()
def correct():
question = False
gameDisplay.fill(cyan)
message_to_screen('Both of these licenses allow for distribution of software', black)
pygame.display.update()
time.sleep(3)
question()
python python-3.x time pygame
add a comment |
up vote
0
down vote
favorite
I am trying to create a quiz app where you press either one of two buttons. This is the MVC example. The main issue is with the 'correct' function. I intended to have it set question to false (which didn't solve the issue), fill the game display, then print out a message to the screen, and update the display, to have it wait 3 seconds before doing anything else. But instead, it waits 3 seconds, flashes the intended screen for a single tick then instantly goes back to the question function. Isn't python supposed to read the function line by line from top to bottom? Because right now it prioritises time.sleep over the rest of the instructions in the function
import time
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
def question():
global question
question = True
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(cyan)
button('text', 150, 500, 100, 50, darkYellow, yellow, action = 'correct')
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
pygame.display.update()
if click[0] == 1 and action != None:
if action == 'correct':
question = False
correct()
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
pygame.display.update()
def correct():
question = False
gameDisplay.fill(cyan)
message_to_screen('Both of these licenses allow for distribution of software', black)
pygame.display.update()
time.sleep(3)
question()
python python-3.x time pygame
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to create a quiz app where you press either one of two buttons. This is the MVC example. The main issue is with the 'correct' function. I intended to have it set question to false (which didn't solve the issue), fill the game display, then print out a message to the screen, and update the display, to have it wait 3 seconds before doing anything else. But instead, it waits 3 seconds, flashes the intended screen for a single tick then instantly goes back to the question function. Isn't python supposed to read the function line by line from top to bottom? Because right now it prioritises time.sleep over the rest of the instructions in the function
import time
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
def question():
global question
question = True
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(cyan)
button('text', 150, 500, 100, 50, darkYellow, yellow, action = 'correct')
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
pygame.display.update()
if click[0] == 1 and action != None:
if action == 'correct':
question = False
correct()
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
pygame.display.update()
def correct():
question = False
gameDisplay.fill(cyan)
message_to_screen('Both of these licenses allow for distribution of software', black)
pygame.display.update()
time.sleep(3)
question()
python python-3.x time pygame
I am trying to create a quiz app where you press either one of two buttons. This is the MVC example. The main issue is with the 'correct' function. I intended to have it set question to false (which didn't solve the issue), fill the game display, then print out a message to the screen, and update the display, to have it wait 3 seconds before doing anything else. But instead, it waits 3 seconds, flashes the intended screen for a single tick then instantly goes back to the question function. Isn't python supposed to read the function line by line from top to bottom? Because right now it prioritises time.sleep over the rest of the instructions in the function
import time
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
def question():
global question
question = True
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(cyan)
button('text', 150, 500, 100, 50, darkYellow, yellow, action = 'correct')
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
pygame.display.update()
if click[0] == 1 and action != None:
if action == 'correct':
question = False
correct()
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
pygame.display.update()
def correct():
question = False
gameDisplay.fill(cyan)
message_to_screen('Both of these licenses allow for distribution of software', black)
pygame.display.update()
time.sleep(3)
question()
python python-3.x time pygame
python python-3.x time pygame
asked Nov 12 at 1:34
Normy Haddad
295
295
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Because right now it prioritises time.sleep over the rest of the instructions in the function
I don't know exactly what you expect what time.sleep
does. It will simply halt your game, and during this time, basically nothing happens. 'Nothing happens' includes drawing and event handling. Nothing is "prioritized".
Remember theses simple rules:
- Whenever you use
time.sleep
in a game, it's probably wrong - Whenever you call
pygame.display.update
multiple times, it's probably wrong
You have to keep track of the state of your game, and each frame decide what to do based on tis state. In the simplest form, this state is simple one or more variables.
Here's a simple, runnable and generic example:
import time
import pygame
import pygame.freetype
pygame.init()
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
font = pygame.freetype.SysFont(None, 20)
def question():
global question
question = True
state = None
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(pygame.Color('cyan'))
result = button('text', 150, 500, 100, 50, pygame.Color('grey'), pygame.Color('yellow'), action = correct)
if result: state = result
if state: question = state()
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1 and action != None:
return action()
return None
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
def message_to_screen(text, color):
font.render_to(gameDisplay, (100, 100), text, color)
def correct():
start = pygame.time.get_ticks()
def func():
cur = pygame.time.get_ticks()
message_to_screen('Both of these licenses allow for distribution of software', pygame.Color('black'))
return cur - start < 3000
return func
question()
We pass the button
function another function (in this case, correct
), which will be called if the player clicks the button.
The result of this, which in turn is also a function, is then stored in the variable state
and supposed to return True
to keep the game running or False
to end the game. This is done by setting question
to the result of that function call.
Of course there are endless ways to to something like this, but this double function approach has the advantage to keep the button
function generic and encapsulate the state ("how long are we going to display the message") in the correct
function. You'll get the idea.
That doesn't sound right. I have a different project where I have multiple time.sleep statements in one function and it works exactly as intended. I expect python to read through the instructions line by line, and for the time.sleep function to simply halt the game and then continue with the instructions below it
– Normy Haddad
Nov 12 at 19:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254970%2fpygame-time-sleep-prioritised-over-the-rest-of-the-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Because right now it prioritises time.sleep over the rest of the instructions in the function
I don't know exactly what you expect what time.sleep
does. It will simply halt your game, and during this time, basically nothing happens. 'Nothing happens' includes drawing and event handling. Nothing is "prioritized".
Remember theses simple rules:
- Whenever you use
time.sleep
in a game, it's probably wrong - Whenever you call
pygame.display.update
multiple times, it's probably wrong
You have to keep track of the state of your game, and each frame decide what to do based on tis state. In the simplest form, this state is simple one or more variables.
Here's a simple, runnable and generic example:
import time
import pygame
import pygame.freetype
pygame.init()
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
font = pygame.freetype.SysFont(None, 20)
def question():
global question
question = True
state = None
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(pygame.Color('cyan'))
result = button('text', 150, 500, 100, 50, pygame.Color('grey'), pygame.Color('yellow'), action = correct)
if result: state = result
if state: question = state()
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1 and action != None:
return action()
return None
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
def message_to_screen(text, color):
font.render_to(gameDisplay, (100, 100), text, color)
def correct():
start = pygame.time.get_ticks()
def func():
cur = pygame.time.get_ticks()
message_to_screen('Both of these licenses allow for distribution of software', pygame.Color('black'))
return cur - start < 3000
return func
question()
We pass the button
function another function (in this case, correct
), which will be called if the player clicks the button.
The result of this, which in turn is also a function, is then stored in the variable state
and supposed to return True
to keep the game running or False
to end the game. This is done by setting question
to the result of that function call.
Of course there are endless ways to to something like this, but this double function approach has the advantage to keep the button
function generic and encapsulate the state ("how long are we going to display the message") in the correct
function. You'll get the idea.
That doesn't sound right. I have a different project where I have multiple time.sleep statements in one function and it works exactly as intended. I expect python to read through the instructions line by line, and for the time.sleep function to simply halt the game and then continue with the instructions below it
– Normy Haddad
Nov 12 at 19:51
add a comment |
up vote
2
down vote
accepted
Because right now it prioritises time.sleep over the rest of the instructions in the function
I don't know exactly what you expect what time.sleep
does. It will simply halt your game, and during this time, basically nothing happens. 'Nothing happens' includes drawing and event handling. Nothing is "prioritized".
Remember theses simple rules:
- Whenever you use
time.sleep
in a game, it's probably wrong - Whenever you call
pygame.display.update
multiple times, it's probably wrong
You have to keep track of the state of your game, and each frame decide what to do based on tis state. In the simplest form, this state is simple one or more variables.
Here's a simple, runnable and generic example:
import time
import pygame
import pygame.freetype
pygame.init()
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
font = pygame.freetype.SysFont(None, 20)
def question():
global question
question = True
state = None
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(pygame.Color('cyan'))
result = button('text', 150, 500, 100, 50, pygame.Color('grey'), pygame.Color('yellow'), action = correct)
if result: state = result
if state: question = state()
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1 and action != None:
return action()
return None
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
def message_to_screen(text, color):
font.render_to(gameDisplay, (100, 100), text, color)
def correct():
start = pygame.time.get_ticks()
def func():
cur = pygame.time.get_ticks()
message_to_screen('Both of these licenses allow for distribution of software', pygame.Color('black'))
return cur - start < 3000
return func
question()
We pass the button
function another function (in this case, correct
), which will be called if the player clicks the button.
The result of this, which in turn is also a function, is then stored in the variable state
and supposed to return True
to keep the game running or False
to end the game. This is done by setting question
to the result of that function call.
Of course there are endless ways to to something like this, but this double function approach has the advantage to keep the button
function generic and encapsulate the state ("how long are we going to display the message") in the correct
function. You'll get the idea.
That doesn't sound right. I have a different project where I have multiple time.sleep statements in one function and it works exactly as intended. I expect python to read through the instructions line by line, and for the time.sleep function to simply halt the game and then continue with the instructions below it
– Normy Haddad
Nov 12 at 19:51
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Because right now it prioritises time.sleep over the rest of the instructions in the function
I don't know exactly what you expect what time.sleep
does. It will simply halt your game, and during this time, basically nothing happens. 'Nothing happens' includes drawing and event handling. Nothing is "prioritized".
Remember theses simple rules:
- Whenever you use
time.sleep
in a game, it's probably wrong - Whenever you call
pygame.display.update
multiple times, it's probably wrong
You have to keep track of the state of your game, and each frame decide what to do based on tis state. In the simplest form, this state is simple one or more variables.
Here's a simple, runnable and generic example:
import time
import pygame
import pygame.freetype
pygame.init()
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
font = pygame.freetype.SysFont(None, 20)
def question():
global question
question = True
state = None
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(pygame.Color('cyan'))
result = button('text', 150, 500, 100, 50, pygame.Color('grey'), pygame.Color('yellow'), action = correct)
if result: state = result
if state: question = state()
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1 and action != None:
return action()
return None
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
def message_to_screen(text, color):
font.render_to(gameDisplay, (100, 100), text, color)
def correct():
start = pygame.time.get_ticks()
def func():
cur = pygame.time.get_ticks()
message_to_screen('Both of these licenses allow for distribution of software', pygame.Color('black'))
return cur - start < 3000
return func
question()
We pass the button
function another function (in this case, correct
), which will be called if the player clicks the button.
The result of this, which in turn is also a function, is then stored in the variable state
and supposed to return True
to keep the game running or False
to end the game. This is done by setting question
to the result of that function call.
Of course there are endless ways to to something like this, but this double function approach has the advantage to keep the button
function generic and encapsulate the state ("how long are we going to display the message") in the correct
function. You'll get the idea.
Because right now it prioritises time.sleep over the rest of the instructions in the function
I don't know exactly what you expect what time.sleep
does. It will simply halt your game, and during this time, basically nothing happens. 'Nothing happens' includes drawing and event handling. Nothing is "prioritized".
Remember theses simple rules:
- Whenever you use
time.sleep
in a game, it's probably wrong - Whenever you call
pygame.display.update
multiple times, it's probably wrong
You have to keep track of the state of your game, and each frame decide what to do based on tis state. In the simplest form, this state is simple one or more variables.
Here's a simple, runnable and generic example:
import time
import pygame
import pygame.freetype
pygame.init()
clock = pygame.time.Clock()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
font = pygame.freetype.SysFont(None, 20)
def question():
global question
question = True
state = None
while question:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(pygame.Color('cyan'))
result = button('text', 150, 500, 100, 50, pygame.Color('grey'), pygame.Color('yellow'), action = correct)
if result: state = result
if state: question = state()
pygame.display.update()
clock.tick(100)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1 and action != None:
return action()
return None
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
def message_to_screen(text, color):
font.render_to(gameDisplay, (100, 100), text, color)
def correct():
start = pygame.time.get_ticks()
def func():
cur = pygame.time.get_ticks()
message_to_screen('Both of these licenses allow for distribution of software', pygame.Color('black'))
return cur - start < 3000
return func
question()
We pass the button
function another function (in this case, correct
), which will be called if the player clicks the button.
The result of this, which in turn is also a function, is then stored in the variable state
and supposed to return True
to keep the game running or False
to end the game. This is done by setting question
to the result of that function call.
Of course there are endless ways to to something like this, but this double function approach has the advantage to keep the button
function generic and encapsulate the state ("how long are we going to display the message") in the correct
function. You'll get the idea.
answered Nov 12 at 7:26
sloth
72.6k14127168
72.6k14127168
That doesn't sound right. I have a different project where I have multiple time.sleep statements in one function and it works exactly as intended. I expect python to read through the instructions line by line, and for the time.sleep function to simply halt the game and then continue with the instructions below it
– Normy Haddad
Nov 12 at 19:51
add a comment |
That doesn't sound right. I have a different project where I have multiple time.sleep statements in one function and it works exactly as intended. I expect python to read through the instructions line by line, and for the time.sleep function to simply halt the game and then continue with the instructions below it
– Normy Haddad
Nov 12 at 19:51
That doesn't sound right. I have a different project where I have multiple time.sleep statements in one function and it works exactly as intended. I expect python to read through the instructions line by line, and for the time.sleep function to simply halt the game and then continue with the instructions below it
– Normy Haddad
Nov 12 at 19:51
That doesn't sound right. I have a different project where I have multiple time.sleep statements in one function and it works exactly as intended. I expect python to read through the instructions line by line, and for the time.sleep function to simply halt the game and then continue with the instructions below it
– Normy Haddad
Nov 12 at 19:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254970%2fpygame-time-sleep-prioritised-over-the-rest-of-the-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown