Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyGame Not Quiting
#1
Hi ive recently started coding something in pygame and have been following along with a set of tutorial but recently whenever you hover over the close button in the pygame window it just freaks out and closes I have recently converted it from python to visual basic

If it helps here is my code:

import pygame as pg
import random
from Settings import *
class Game:
    def __init__(self):
        #Initialases Game window
        pg.init()
        pg.mixer.init()
        self.screen= pg.display.set_mode ((width, height))
        pg.display.set_caption(Title)
        self.clock=pg.time.Clock()
        self.running = True

    def new (self):
        #Starts game upon death
        self.all_sprites = pg.sprite.Group()
        self.run


    def run(self):
       #Game Loop
        self.playing = True
        while self.playing:
            self.clock.tick (fps)
            self.events()
            self.update()
            self.draw()
            


    def update(self):
        #Game loop Update
       self.all_sprites.update()

    def events(self):
        #Game loop events
        for event in pg.event.get():
            if event.type == pg.QUIT:
                if self.playing:
                    self.playing = False
                self.running=False

    def draw(self):
        #Game loop draw
        self.screen.fill(Black)
        self.all_sprites.draw(self.screen)
        pg.display.flip()

    def show_start_screen(self):
        #Show The start Screen
        pass

    def show_go_screen(self):
        #Shows The Game Over Screen
        pass

g = Game()
g.show_start_screen()
while g.running:
    g.new()
    g.show_go_screen()

pg.quit()


#And here is my Settings Script That it imports

Title = "Platform Adventure"
width = 480
height = 600
fps = 60

#Colours
White = (255, 255, 255)
Black = (0, 0, 0)
Red = (255, 0, 0)
Light_Blue = (0, 255, 255)
Reply
#2
Your code just looks rather weird. I mean the part related to "quitting" and your placement of while loops. You lack a sys.exit() statement too.

I really think you read this article and focus on how the quitting was handled. It's a bit too long to explain here. Pygame Tutorial.
Reply
#3
Okay thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020