Robert boy
Robert boy

Reputation: 51

My start screen wont start when I press Run! button

So ever since i have added my Start Screen for my game and when I would press Run!/Start button it wont, the screen would just freeze up and I cant press anything. My Sit/quit works perfectly fine but my Run/Start won't

https://gyazo.com/f0dfef5bc4be1875e6928d79a0b792fd

As you can see form my short demonstration, if I press Run/Start button my game will just freeze up and will stop working. I have tried rewriting my code but that still dose not work, I think I am missing something but I do not know what.

This is where I am having my problems

##############################################
# START MENUE
def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    #print(click)
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(window, ac,(x,y,w,h))

        if click[0] == 1 and action != None:
            action()         
    else:
        pygame.draw.rect(window, ic,(x,y,w,h))

    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    window.blit(textSurf, textRect)

def quitgame():
    pygame.quit()


def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        largeText = pygame.font.Font('freesansbold.ttf',95)
        TextSurf, TextRect = text_objects("Lava Runer", largeText)
        TextRect.center = ((500/2),(500/2))
        window.blit(TextSurf, TextRect)


        button("Run!",100,350,100,50,darkgreen,green,main_loop)
        button("Sit!",300,350,100,50,darkred,red,quitgame)

        pygame.display.update()
        clock.tick(15)

        bg = pygame.image.load("Sky2.jpg")
        window.blit(bg,(0,0))


        
############################################

This is my Full code

import pygame
pygame.font.init()
pygame.init()

#set screen
window = pygame.display.set_mode((500,500))

#set Name
pygame.display.set_caption("Noob")



class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.speed = 0
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.topleft = (self.x,self.y)

class Floor:
    def __init__ (self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


class Coin():
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)
    


white = (255,255,255)

green = (0,255,0)

red = (255,0,0)

darkred = (200,0,0)

darkgreen = (0,200,0)

black = (0,0,0)
 
player1 = player(50,400,40,40,white)

coin1 = Coin(100,300,30,30,red)

coin2 = Coin(200,300,30,30,red)

floor1 = Floor(0,0,1000,30,green)
floor2 = Floor(0,400,1000,30,green)


coins = [coin1,coin2]
floors = [floor1,floor2]




fps = (30)

clock = pygame.time.Clock()

##############################################
# START MENUE
def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    #print(click)
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(window, ac,(x,y,w,h))

        if click[0] == 1 and action != None:
            action()         
    else:
        pygame.draw.rect(window, ic,(x,y,w,h))

    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    window.blit(textSurf, textRect)

def quitgame():
    pygame.quit()


def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        largeText = pygame.font.Font('freesansbold.ttf',95)
        TextSurf, TextRect = text_objects("Lava Runer", largeText)
        TextRect.center = ((500/2),(500/2))
        window.blit(TextSurf, TextRect)


        button("Run!",100,350,100,50,darkgreen,green,main_loop)
        button("Sit!",300,350,100,50,darkred,red,quitgame)

        pygame.display.update()
        clock.tick(15)

        bg = pygame.image.load("Sky2.jpg")
        window.blit(bg,(0,0))


        
############################################

def main_loop():
    #window
    def redrawwindow():
        window.fill((0,0,0))    

        #draw plyer
        player1.draw()
        for Coin in coins:
            Coin.draw()

        for Floor in floors:
            Floor.draw()
    



        # the score draw it on the screen
        window.blit(text,textRect)


    # Font for coin   
    font  = pygame.font.Font("freesansbold.ttf",30)
    score = 0
    text = font.render("Coins = "+str(score),True,(255,255,255))
    textRect = text.get_rect()
    textRect.center = ((100,40))

                    
    run = True
    while run:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False


        # coin collisions
        for Coin in coins:
            for one in range(len(coins)-1-1-1):
                if player1.rect.colliderect(coins[one].rect):
                    del coins[one]
                    score += 1
                    text = pygame.font.Font("comicsansms",30)
                    textRect.center = (100,40)



        # Keys for player
        keys = pygame.key.get_pressed()

        if keys[pygame.K_a]and player1.x > player1.speed:
            player1.x -= player1.speed

        if keys[pygame.K_d]and player1.x <500 - player1.height - player1.speed:
            player1.x += player1.speed

        if keys[pygame.K_w]and player1.y > player1.speed:
            player1.y -= player1.speed

        if keys[pygame.K_s]and player1.y <500 - player1.width - player1.speed:
            player1.y += player1.speed


        # Fall
        if not player1.isJump:
            player1.y += player1.fall
            player1.fall += 1
            player1.isJump = False



            # Collide with Floor
            collide = False
            for Floor in floors:
                if player1.rect.colliderect(Floor.rect):
                    collide = True
                    player1.isJump = False
                    player1.y = Floor.rect.top - player1.height + 1
                    if player1.rect.right > Floor.rect.left and player1.rect.left > Floor.rect.right - player1.width:
                        player1.x = Floor1.rect.left - player1.width
                    if player1.rect.left < Floor.rect.right and player1.rect.right > Floor.rect.left + player1.width:
                        player1.x = Floor.rect.right


                if player1.rect.bottom >= 500:
                    collide = True
                    player1.isJump = False
                    player1.JumpCount = 10
                    player1.y = 500 - player1.height

                if collide:
                    if keys[pygame.K_SPACE]:
                        player1.isJump = True
                        player1.fall = 0

                else:
                    if player1.JumpCount >= 0:
                        player1.y -= (player1.JumpCount*abs(player1.JumpCount))*0.3
                        player1.JumpCount -= 1
                    else:
                        player1.JumpCount = 10
                        player1.isJump = False
                    
                    
        redrawwindow()
    pygame.display.update()
    pygame.quit()
game_intro()
main_loop()

Upvotes: 1

Views: 49

Answers (1)

Shehan Hasintha
Shehan Hasintha

Reputation: 1165

So this is where you are starting your game right.?

    button("Run!",100,350,100,50,darkgreen,green,main_loop)
    button("Sit!",300,350,100,50,darkred,red,quitgame)

I would say not to run main loop directly like this. ok... what you want to do instead you want to change an state in relation to the event has been triggered. otherwise you will overly run the main loop since you are al ready inside a while loop. you lead this function to a function it where changes the state of a global variable like this. and let the main function run based on that global variable's state.

button("Run!",100,350,100,50,darkgreen,green,main_loop_state_turn_on)

Then proceed to evaluate it's position in relation with the buttons.

event_i = False

def main_loop_state_turn_on():
    global event_i = true

def main_loop_state_turn_off():
    global event_i = false

inside the main loop make the way to run while loop if those global variables are true.

def main_loop():
      if event_i:
         print 'Begin'
         #your code is going on here

for more understanding you can look up this

Upvotes: 2

Related Questions