TerraceMason
TerraceMason

Reputation: 93

Python - Game is not responding?

Trying to create a new game instance so that I can create a main menu that shows up before the actual game occurs.

I am attempting to make it so that when you press a on the keyboard, the game will start and the main menu will disappear, but the game loops some kind of code and makes it crash; not responding.

import pygame
import random
import time
pygame.init()

#Setting Variables
screenW = 1020
screenH = 630
x = 125
y = 164
width = 50
height = 50
velocity = 5
wave = 3
GOLD = (255,215,0)
BLACK = (0, 0, 0)

class ZombieChars():
    def __init__(self):
        self.y = 164
        self.vel = 5
        self.x_change = random.randrange(1,3)
        self.y_change = 1
        self.height = random.randrange(35, 60)
        self.width = random.randrange(60, 70)
        self.color = random.sample(range(250), 4)
        self.image = pygame.Surface([self.width, self.height], pygame.HWSURFACE, 32)
        self.rect = self.image.get_rect(topleft = (random.randrange(700, 1200), 550))
        self.image.fill(self.color)
        #pygame.draw.rect(self.image, (self.color), (self.x, self.y, self.width, self.height))

    def draw(self):
        #print(self.rect.x)
        window.blit(self.image, self.rect.topleft)

    def update(self):
        if self.rect.x >= 220:
            self.rect.x -= self.x_change
        else:
            self.rect.x -= 0


    def death(self):
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = event.pos
            if self.rect.collidepoint(mouse_pos):
                self.rect.x = -500
                print ("Square Clicked")
                print(self.rect.x)

#FPS
clock = pygame.time.Clock()
clock.tick(60)

#Screen
window = pygame.display.set_mode((screenW,screenH))
pygame.display.set_caption(("Zombie Shooter"))
bg = pygame.image.load("bg.jpg")
mainmenu = pygame.image.load("mainmenu.jpg")
zombies = ZombieChars()

my_list = []
for sanjh in range(wave):
    my_object = ZombieChars()
    my_list.append(my_object)
def text_objects(text, font):
    textSurface = font.render(text, True, BLACK)
    return textSurface, textSurface.get_rect()

smallText = pygame.font.Font('freesansbold.ttf', 30)
TextSurf, TextRect = text_objects("Welcome to Zombie Shooter Alpha", smallText)
TextRect.center = ((1020 / 2), (50))

TextSurf2, TextRect2 = text_objects("Shoot the zombies before they arrive at your fortress!", smallText)
TextRect2.center = ((1020 / 2 - 80), (100))

TextSurf3, TextRect3 = text_objects("Wave: " + str(wave), smallText)
TextRect3.center = ((1020 / 2), (50))

#Main Loop
run = True
mainMenu = True
keys = pygame.key.get_pressed()
global event
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            mainMenu = False
    while mainMenu == True:
        window.blit(mainmenu, (0,0))
        pygame.display.flip()
        if keys[pygame.K_a]:
            mainMenu = False
            print ("yeah i clicked")
        while mainMenu == False:
            window.blit(bg, (0,0))
            window.blit(TextSurf, TextRect)
            window.blit(TextSurf2, TextRect2)
            pygame.time.delay(25)

            for zombie in my_list:
                zombie.draw()
                zombie.update()
                zombie.death()

            pygame.display.flip()

    #Drawing

If anyone could identify what is breaking my game that would be amazing.

Upvotes: 0

Views: 91

Answers (1)

The Big Kahuna
The Big Kahuna

Reputation: 2110

You have a game loop inside a game loop inside a game loop. no no no. you have while mainMenu == True then inside that loop you have a while mainMenu == False so when it is mainmenu it is looping through big loop, then when its false, it loops through small one and never goes out.

you can have multiple game loops but not in each other

while mainMenu == True:
    window.blit(mainmenu, (0,0))
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False   #if x is pressed dont run game
            mainMenu = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        mainMenu = False  #if a is pressed run game
        print ("yeah i clicked")

while run:
    window.blit(bg, (0,0))
    window.blit(TextSurf, TextRect)
    window.blit(TextSurf2, TextRect2)
    pygame.time.delay(25)

    for zombie in my_list:
        zombie.draw()
        zombie.update()
        zombie.death()

    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

Upvotes: 1

Related Questions