why is the exit window button work but the exit button in the game does not work?
The main loop runs as long as playing
is True
.
playing = True
while playing:
# [...]
When the pygame.QUIT
event is handled, playing
is set False
the main loop condition is fails:
if event.type == pygame.QUIT:
playing = False
# [...]
Note, pygame.quit()
doesn't terminate the loop, but it uninitialize all pygame modules, which will cause an exception in the following, if it is done in the middle of the application.
If you want to quit the application by the keypad enter key pygame.K_KP_ENTER
, the you've to do the same when the pygame.KEYDOWN
event is handled:
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_KP_ENTER:
playing = False
Or you've to send a pygame.QUIT
event by pygame.event.post()
:
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_KP_ENTER:
pygame.event.post(pygame.event.Event(pygame.QUIT))
Window closes only after clicking exit button multiple times?
Those two loops will not exit just because you clicked exit:
while (accumulator >= deltaTime) {
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
gameLoopRunning = false;
}
accumulator -= deltaTime;
}
}
The accumulator loop is especially likely to run for a while if you game is behind, in game time. You should make it explicit that you want to break out of those loops, and not just wait later:
while (accumulator >= deltaTime && gameLoopRunning) {
while (SDL_PollEvent(&event) && gameLoopRunning) {
switch(event.type) {
case SDL_QUIT:
gameLoopRunning = false;
}
accumulator -= deltaTime;
}
}
Notice the extra && gameLoopRunning
.
Exit and Play button don't register well
I am not really sure if I understand well your problem, but in your example it seems like isExitButtonPressed
is always true at the end.
You're setting it to false at the begining and then setting it to true in the if statement which has always a fulfilled condition giving the fact that you set isExitButtonPressed
to false beforehand !
Exit button doesn´t work - wxpython
As per my understanding of your question, your application is not closing when you click on the close button (The red button with X on the right top corner.)
By default when you click the close button your application should close. In your case it seems to me that you have bind the EVT_CLOSE
to some method, which has no code in it to close the app window.
For eg. consider the code snippet below, I have intentionally bind the EVT_CLOSE
event to a method named as closeWindow()
. This method does nothing that is why I have the pass
keyword there. Now if you execute the code snippet below you can see that the app window won't close.
Code:
import wx
class GUI(wx.Frame):
def __init__(self, parent, id, title):
screenWidth = 500
screenHeight = 400
screenSize = (screenWidth,screenHeight)
wx.Frame.__init__(self, None, id, title, size=screenSize)
self.Bind(wx.EVT_CLOSE, self.closeWindow) #Bind the EVT_CLOSE event to closeWindow()
def closeWindow(self, event):
pass #This won't let the app to close
if __name__=='__main__':
app = wx.App(False)
frame = GUI(parent=None, id=-1, title="Problem Demo-PSS")
frame.Show()
app.MainLoop()
So, in order to close the app window you need to change the closeWindow()
. For eg: Following code snippet will use the Destroy() close the app window when you click on the close button.
import wx
class GUI(wx.Frame):
def __init__(self, parent, id, title):
screenWidth = 500
screenHeight = 400
screenSize = (screenWidth,screenHeight)
wx.Frame.__init__(self, None, id, title, size=screenSize)
self.Bind(wx.EVT_CLOSE, self.closeWindow) #Bind the EVT_CLOSE event to closeWindow()
def closeWindow(self, event):
self.Destroy() #This will close the app window.
if __name__=='__main__':
app = wx.App(False)
frame = GUI(parent=None, id=-1, title="Problem Demo-PSS")
frame.Show()
app.MainLoop()
I hope it was useful.
Make my turtle-based game exit button work
The problem seems to be that you didn't break
out of the while True:
loop once you setup the exit button (moving on to a mainloop()
call). Below is my rework of your code that fixes this problem and addresses a number of other issues:
from turtle import Screen, Turtle
from math import sqrt
from random import randint
WIDTH, HEIGHT = 780, 630
SMALL_FONT = ('Arial', 25, 'normal')
LARGE_FONT_SIZE = 70
LARGE_FONT = ('Arial', LARGE_FONT_SIZE, 'normal')
def forward():
player1.fd(speed1)
player2.fd(speed2)
def turn_right():
player1.rt(45)
def turn_left():
player1.lt(45)
def turn_right2():
player2.rt(45)
def turn_left2():
player2.lt(45)
def button_click(x, y):
if -150 < x < 150 and -100 < y < 0:
exit()
def exit_button():
button.begin_fill()
for _ in range(2):
button.forward(300)
button.left(90)
button.forward(100)
button.left(90)
button.end_fill()
button_text.write("Exit", align='center', font=LARGE_FONT)
screen.onscreenclick(button_click)
screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.bgcolor('#202020')
screen.title("SpaceWar")
border = Turtle()
border.hideturtle()
border.color('white')
border.speed('fastest')
border.pensize(3)
border.penup()
border.setposition(15 - WIDTH/2 - 4, 15 - HEIGHT/2 + 4) # 4 = adjustment for "chrome"
border.pendown()
for side in range(2):
border.forward(WIDTH - 30)
border.left(90)
border.forward(HEIGHT - 30)
border.left(90)
score = 0
text2 = Turtle()
text2.hideturtle()
text2.color('white')
text2.penup()
text2.setposition(70, 200)
scorestring = "Score: %s" % score
text2.write(scorestring, align='center', font=SMALL_FONT)
text3 = Turtle()
text3.hideturtle()
text3.color('red')
button = Turtle()
button.hideturtle()
button.color('white')
button.speed('fastest')
button.pensize(4)
button.penup()
button.goto(-150, -100)
button_text = Turtle()
button_text.hideturtle()
button_text.penup()
button_text.goto(0, -50 + -LARGE_FONT_SIZE/2)
booster1 = Turtle()
booster1.shape('circle')
booster1.color('#1c77d9')
booster1.speed('fastest')
booster1.penup()
booster1.setposition(randint(-250, 250), randint(-250, 250))
booster2 = Turtle()
booster2.shape('circle')
booster2.color('#d6281c')
booster2.speed('fastest')
booster2.penup()
booster2.setposition(randint(-250, 250), randint(-250, 250))
decreaser = Turtle()
decreaser.shape('circle')
decreaser.color('green')
decreaser.speed('fastest')
decreaser.penup()
decreaser.setposition(randint(-250, 250), randint(-250, 250))
speed1 = 3
speed2 = 3
player1 = Turtle()
player1.shape('triangle')
player1.color('#1c77d9')
player1.shapesize(1.5)
player1.speed('fastest')
player1.penup()
player1.setx(-330)
player2 = Turtle()
player2.shape('triangle')
player2.color('#d6281c')
player2.shapesize(1.5)
player2.speed('fastest')
player2.penup()
player2.setheading(180)
player2.setx(330)
screen.onkeypress(turn_left, 'a')
screen.onkeypress(turn_right, 'd')
screen.onkey(turn_left2, 'Left')
screen.onkey(turn_right2, 'Right')
screen.listen()
while True:
forward()
a = sqrt(pow(player1.xcor() - player2.xcor(), 2) + pow(player1.ycor() - player2.ycor(), 2))
b = sqrt(pow(player1.xcor() - booster1.xcor(), 2) + pow(player1.ycor() - booster1.ycor(), 2))
c = sqrt(pow(player2.xcor() - booster2.xcor(), 2) + pow(player2.ycor() - booster2.ycor(), 2))
d = sqrt(pow(player1.xcor() - decreaser.xcor(), 2) + pow(player1.ycor() - decreaser.ycor(), 2))
e = sqrt(pow(player2.xcor() - decreaser.xcor(), 2) + pow(player2.ycor() - decreaser.ycor(), 2))
if a < 20:
player1.setx(-330)
player1.setheading(0)
player2.setx(330)
player2.setheading(180)
score += 1
text2.undo()
scorestring = "Score: %s" % score
text2.write(scorestring, align='center', font=SMALL_FONT)
if b < 20:
speed1 += 1
booster1.setposition(randint(-250, 250), randint(-250, 250))
if c < 20:
speed2 += 1
booster2.setposition(randint(-250, 250), randint(-250, 250))
if d < 20:
speed1 -= 1
decreaser.setposition(randint(-250, 250), randint(-250, 250))
if e < 20:
speed2 -= 1
decreaser.setposition(randint(-250, 250), randint(-250, 250))
if speed1 == 7:
speed1 -= 1
if speed2 == 7:
speed2 -= 1
if not -375 <= player1.xcor() <= 375:
score += 1
player1.setx(-330)
player1.setheading(0)
text2.undo()
scorestring = "Score: %s" % score
text2.write(scorestring, align='center', font=SMALL_FONT)
if not -300 <= player1.ycor() <= 300:
score += 1
player1.setx(-330)
player1.setheading(0)
text2.undo()
scorestring = "Score: %s" % score
text2.write(scorestring, align='center', font=SMALL_FONT)
if not -375 <= player2.xcor() <= 375:
player2.right(180)
if not -300 <= player2.ycor() <= 300:
player2.right(180)
if score == 2:
text3.write("Red Won!", align='center', font=LARGE_FONT)
booster1.hideturtle()
booster2.hideturtle()
decreaser.hideturtle()
player1.hideturtle()
player2.hideturtle()
exit_button()
break
screen.mainloop()
I hope this gives you some useful ideas. The next thing I'd do is eliminate the while True:
call, which has no place in an event-driven world like turtle, and make the body of it a function invoked by ontimer()
.
how do i make a window quit in pygame?
Well you do so by implementing below code snippet in your code:
running = True
while running:
# other code
event = pygame.event.wait ()
if event.type == pygame.QUIT:
running = False # Be interpreter friendly
pygame.quit()
Make sure that you call pygame.quit() before you exit your main function
You can also reference this thread
Pygame escape key to exit
Using Exit button to close a winform program
this.Close();
Closes the form programmatically.
Related Topics
How to Interact with the Recaptcha Audio Element Using Selenium and Python
How to Remove Specific Tag/Sticker/Object from Images Using Opencv
Pyplot Move Alternative Y Axis to Background
Add Column with Constant Value to Pandas Dataframe
How Does Python's "Super" Do the Right Thing
Pyeval_Initthreads in Python 3: How/When to Call It? (The Saga Continues Ad Nauseam)
Python - Using the Multiply Operator to Create Copies of Objects in Lists
Child Processes Created with Python Multiprocessing Module Won't Print
Python Regular Expression Pattern * Is Not Working as Expected
How to Do Row-To-Column Transposition of Data in CSV Table
Replace Invalid Values with None in Pandas Dataframe
Pythonic Way to Combine For-Loop and If-Statement
Pygame Tic Tak Toe Logic? How Would I Do It
Sample Each Group After Pandas Groupby
Passing a Data Frame Column and External List to Udf Under Withcolumn