0

Basically i have been stuck for hours trying to create a blank template when the user clicks new game. I tell the code to then cancel the while loop and use the Background.fill() function to create a blank screen but it doesn't work. Here is my code.

import pygame
import random
import sys
from pygame import *

def text():
    font = pygame.font.SysFont("monospace", 40)
    surfacefont = font.render("Tower Defense", True, black)
    surfaceR = surfacefont.get_rect()
    surfaceR.center = (400,50)
    Background.blit(surfacefont, surfaceR)

    text = font.render("Start game", True, black)
    textpos = surfacefont.get_rect()
    textpos.center = (425,200)
    Background.blit(text, textpos)

    text = font.render("Exit Game", True, black)
    textpos = surfacefont.get_rect()
    textpos.center = (435,260)
    Background.blit(surfacefont, surfaceR)
    Background.blit(text, textpos)
    pygame.display.update()

def button():
    click = pygame.mouse.get_pressed()
    pos = pygame.mouse.get_pos()
    if 285+205 > pos[0] > 285 and 175+50 > pos[1] > 175:
        pygame.draw.rect(Background, (100,100,100),(263,175,250,50))
        if click[0] == 1:
            cancelLoop()
    else:
        pygame.draw.rect(Background, white,(285,175,205,50))

    if 278+220 > pos[0] > 278 and 234+50 > pos[1] > 234: 
        pygame.draw.rect(Background, (100,100,100),(278,234,220,50))
        if click[0] == 1:
            pygame.quit()
            sys.exit()
    else:
        pygame.draw.rect(Background, white,(278,234,220,50))

def cancelLoop():
    global intro
    intro = 0
    runGame()

def runGame():
    Background.fill(red)

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
Background = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Tower Defense")
intro = 1
while intro == 1:
    Background.fill(white)
    button()
    text()
    for event in pygame.event.get():
        if event.type == quit:
            pygame.quit()
            sys.exit()
    pygame.display.update()
    pygame.display.flip()
  • Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with more details. – Joe C Nov 20 '16 at 19:02
  • You only should use one of either `pygame.display.update()` or `pygame.display.flip()` because they do basically the same thing. Either one works but `update()` is optimized for software displays and thus recommended for this case. – Ted Klein Bergman Nov 23 '16 at 09:53

1 Answers1

0

The problem is that you are effectively exiting your game when you call intro == 0, since you cancelled your while loop. As a result the program will just end after the intro. Instead of making a while loop for just your intro you could make one main loop, where the runGame() function is also called, or you could make separate loops for your intro and the actual game. This would be how I would personally write it:

import pygame
import random
import sys
from pygame import *

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
Background = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Tower Defense")

def text():
    font = pygame.font.SysFont("monospace", 40)
    surfacefont = font.render("Tower Defense", True, black)
    surfaceR = surfacefont.get_rect()
    surfaceR.center = (400,50)
    Background.blit(surfacefont, surfaceR)

    text = font.render("Start game", True, black)
    textpos = surfacefont.get_rect()
    textpos.center = (425,200)
    Background.blit(text, textpos)

    text = font.render("Exit Game", True, black)
    textpos = surfacefont.get_rect()
    textpos.center = (435,260)
    Background.blit(surfacefont, surfaceR)
    Background.blit(text, textpos)
    pygame.display.update()

def button():
    click = pygame.mouse.get_pressed()
    pos = pygame.mouse.get_pos()
    if 285+205 > pos[0] > 285 and 175+50 > pos[1] > 175:
        pygame.draw.rect(Background, (100,100,100),(263,175,250,50))
        if click[0] == 1:
            runGame()
    else:
        pygame.draw.rect(Background, white,(285,175,205,50))

    if 278+220 > pos[0] > 278 and 234+50 > pos[1] > 234: 
        pygame.draw.rect(Background, (100,100,100),(278,234,220,50))
        if click[0] == 1:
            pygame.quit()
            sys.exit()
    else:
        pygame.draw.rect(Background, white,(278,234,220,50))

def runGame():
    Background.fill(red)

intro = 1
while intro == 1:
    Background.fill(white)
    button()
    text()
    for event in pygame.event.get():
        if event.type == quit:
            pygame.quit()
            sys.exit()
    pygame.display.update()
    pygame.display.flip()
ModoUnreal
  • 642
  • 5
  • 15