user14777932
user14777932

Reputation:

Why do I have to close pygame window again and again?

I made a car game and here is my code:-

import pygame
import random
from pathlib import Path
import os
os.chdir(Path(__file__).parent)

win_w = 300
win_h = 480
pygame.init()
win = pygame.display.set_mode((win_w, win_h))
#icon = pygame.image.load('icon.png')
#pygame.display.set_icon(icon)
pygame.display.set_caption('Game')

def main():
    blue = (51, 51, 255)
    grey = (128, 128, 128)
    light_grey = (200, 200, 200)
    orange = (255, 175, 26)
    white = (255, 255, 255)
    black = (0, 0, 0)
    green = (0, 255, 0)
    red = (255, 71, 26)
    car_speed = 15
    score = 0
    time = 85
    
    class mainCar(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('car.xcf')
            self.rect = self.image.get_rect()
        def draw(self):
            win.blit(self.image, (self.rect.x, self.rect.y))
        def update(self):
            if driving:
                key = pygame.key.get_pressed()

                if key[pygame.K_LEFT]:
                    car.rect.x += -car_speed

                if key[pygame.K_RIGHT]:
                    car.rect.x += car_speed
                
                if key[pygame.K_a]:
                    car.rect.x += -car_speed

                if key[pygame.K_d]:
                    car.rect.x += car_speed

                if car.rect.x >= win_w - 38:
                    car.rect.x = win_w - 38
                
                if car.rect.x <= 0:
                    car.rect.x = -2
            

    class enemyCar1(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('enemy1.xcf')
            self.rect = self.image.get_rect()
        def draw(self):
            win.blit(self.image, (self.rect.x, self.rect.y))

    class enemyCar2(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('enemy2.xcf')
            self.rect = self.image.get_rect()
        def draw(self):
            win.blit(self.image, (self.rect.x, self.rect.y))

    class enemyCar3(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load('enemy3.xcf')
            self.rect = self.image.get_rect()
        def draw(self):
            win.blit(self.image, (self.rect.x, self.rect.y))

    road_y1 = 10
    road_y2 = 280
    road_y3 = 430
    road_y4 = 140

    def road_draw():
        #line 1
        pygame.draw.rect(win, white, (76, road_y1, 13, 90)) #x, y, width, height
        pygame.draw.rect(win, white, (76, road_y2, 13, 90))  
        pygame.draw.rect(win, white, (76, road_y3, 13, 90)) 
        pygame.draw.rect(win, white, (76, road_y4, 13, 90)) 
        #line 2
        pygame.draw.rect(win, white, (205, road_y1, 13, 90))
        pygame.draw.rect(win, white, (205, road_y2, 13, 90)) 
        pygame.draw.rect(win, white, (205, road_y3, 13, 90))  
        pygame.draw.rect(win, white, (205, road_y4, 13, 90))  

    a = 600
    b = 710
    c = 8
    d = 390

    car = mainCar()
    car.rect.x = 165
    car.rect.y = 385

    car2 = enemyCar1()
    car2.rect.x = random.randint(0, 52)
    car2.rect.y = random.randint(a, b)

    car3 = enemyCar2()
    car3.rect.x = random.randint(54, 180)
    car3.rect.y = random.randint(a, b)

    car4 = enemyCar3()
    car4.rect.x = random.randint(182, 265)
    car4.rect.y = random.randint(a, b)

    def game_over():
        class gameOver(pygame.sprite.Sprite):
            def __init__(self):
                pygame.sprite.Sprite.__init__(self)
                self.image = pygame.image.load('gameover_text.png')
                self.rect = self.image.get_rect()
            def draw(self):
                win.blit(self.image, (50, win_h // 2))
        
        gameOver = gameOver()
        if driving == False:
            gameOver.draw()
            key = pygame.key.get_pressed()
            if key[pygame.K_SPACE]:
                main()

    def updateEnemies():
        if driving:
            car2.rect.y += 13
            car3.rect.y += 16
            car4.rect.y += 10
            
            if car2.rect.y >= win_h:
                car2.rect.y = random.randint(0, 10)
                car2.rect.x = random.randint(0, 56)
                
            if car3.rect.y >= win_h:
                car3.rect.y = random.randint(0, 10)
                car3.rect.x = random.randint(69, 178)
                
            if car4.rect.y >= win_h:
                car4.rect.y = random.randint(0, 10)
                car4.rect.x = random.randint(194, 273)

    def redraw():
        win.fill(grey)
        road_draw()
        car2.draw()
        car3.draw()
        car4.draw()
        car.draw()
        car.update()
        updateEnemies()
        game_over()
        win.blit(score_text, (10, 0))
        win.blit(score_number, (76, 0))

    dead = False
    playing = True
    driving = True

    while playing:
        pygame.time.delay(time)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                playing = False
    
        if driving:
            road_y1 += 10
            road_y2 += 10
            road_y3 += 10
            road_y4 += 10

        if road_y1 >= win_h:
            road_y1 = -60

        if road_y2 >= win_h:
            road_y2 = -60

        if road_y3 >= win_h:
            road_y3 = -60

        if road_y4 >= win_h:
            road_y4 = -60
        
        score_font = pygame.font.Font("freesansbold.ttf", 18)
        score_text = score_font.render(format("Score: "), False, orange)
        win.blit(score_text, (10, 0))
        score_number = score_font.render(format(score), False, orange)
        win.blit(score_number, (76, 0))

        if driving:
            score += 1

        if score >= 150:
            time = 70
        if score >= 250:
            time = 58
        if score >= 400:
            time = 40

        if car.rect.colliderect(car2.rect):
            driving = False
        if car.rect.colliderect(car3.rect):
            driving = False
        if car.rect.colliderect(car4.rect):
            driving = False

        redraw()

        pygame.display.update()

main()
pygame.quit

Now when ever I lose, I press spacebar to start again. For eg. I did 4 tries and now I m tired. So I want to quit the game. As I tried 4 times I will have to click X 4 times!

I want to close it all in 1 time only...

This is the first try...

Try - 2

Now when I click close button in window then it pops up again and again till how much times I retried. If I retried 3 times then I have to close window 3 time, why?

I just want it to close only in 1st time... I hope you are understanding my words!

Please help me that when I close window 1 time everything closes If you don't understand my thing than you may try the code and retry 2 - 3 times and see...

THANK YOU!!!

Upvotes: 1

Views: 50

Answers (1)

Rabbid76
Rabbid76

Reputation: 210946

You're calling main recursively. In fact, you are not restarting the game, but you are starting a completely new game within the game. When you want to quit, you have to close all the games you've started. Write a function or method that resets all game-related variables. Call this function instead of main.

Upvotes: 1

Related Questions