gero
gero

Reputation: 119

adding an image with pygame

I am just programming a small game with python and the pygame library. The game's idea is to have a small charakter running around the floor avoiding to crash into a falling rectunglar. It all works out from the game logic side. But I still have problem when the game starts:

As I invert the player horizontally, depending on which direction it is heading to, I had to assign 2 images depending on the key pressed. Now, everytime I start the game and no key is pressed yet, there is only a "hidden/transparent" pygame.Surface()-object, because I needed it as a placeholder to assign the 2 images conditionally once a key(left or right) is pressed. Hence, the game starts and when not key is pressed no player shows up...

My question: How can I add a default (3rd) image before any key is actually pressed and not having the transparent Surface object hiding. I tried many things with the Surface.blit() or gameDisplay.blit() and thus loading another image, but it never showed up so far :(...

I guess it is a dumb thing to solve but I cannot figure it out on my own (+ google)..

Thanks in advance

my code:

import pygame
import time
import random

pygame.init() 


display_width = 800
display_height = 600

black = (0,0,0)
white = (255, 255, 255)
red = (200, 0,0 )
green = (0, 200, 0)
blue = (0,0, 255)
bright_red = (255, 0,0 )
bright_green = (0, 255, 0)

player_width = 100
player_height = 238
pause = False

gameDisplay = pygame.display.set_mode((display_width, display_height))          
pygame.display.set_caption('Gesi Run of her Life <3')
clock = pygame.time.Clock()

gesImg_left = pygame.image.load('yourimage.png')        
gesImg_right = pygame.transform.flip(gesImg_left, True, False)
background = pygame.image.load('yourbackground.jpg')


def things(thingx, thingy, thingw, thingh, color):

    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

def ges(player_img, x,y):

    gameDisplay.blit(player_img, (x,y))

def text_objects(text, font, col):

    textSurface = font.render(text, True, col)
    return textSurface, textSurface.get_rect()

def message_display(text, col):

    largeText = pygame.font.Font('freesansbold.ttf', 50)
    TextSurf, TextRect = text_objects(text, largeText, col)
    TextRect.center = ((display_width/2), (display_height/2))

    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()

    time.sleep(2)
    game_loop()

def crash():

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        #gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf', 60)
        TextSurf, TextRect = text_objects("A star was born!", largeText, black)
        TextRect.center = ((display_width/2), (display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Nochmal", 150, 450, 100, 50, green, bright_green, "play")
        button("quit", 550, 450, 100, 50, red, bright_red, "quit")

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

def button(msg, x, y, w, h,ic, ac, action = None):

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        if x+ w > mouse[0] > x and y + h > mouse[1] > y:
            pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
            if click[0] == 1 and action != None:
                if action == "play":
                    game_loop()
                elif action == "quit":
                    pygame.quit()
                    quit()
                elif action == "unpause":
                    global pause
                    pygame.mixer.music.unpause()
                    pause = False

        else:
            pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

        smallText = pygame.font.Font('freesansbold.ttf', 20)
        TextSurf, TextRect = text_objects(msg, smallText, black)
        TextRect.center = ((x+(w/2)),(y +(h/2)))
        gameDisplay.blit(TextSurf, TextRect)

def paused():

    pygame.mixer.music.pause()
    while pause:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        #gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf', 60)
        TextSurf, TextRect = text_objects("Paused", largeText, black)
        TextRect.center = ((display_width/2), (display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Continue", 150, 450, 100, 50, green, bright_green, "unpause")
        button("quit", 550, 450, 100, 50, red, bright_red, "quit")

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

def game_intro():

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

        gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf', 60)
        TextSurf, TextRect = text_objects("Gesi Super F/Run Game", largeText, black)
        TextRect.center = ((display_width/2), (display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        button("let's go", 150, 450, 100, 50, green, bright_green, "play")
        button("quit", 550, 450, 100, 50, red, bright_red, "quit")

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


def game_loop():

    global pause

    x = (display_width * 0.45)
    y = (display_height * 0.6)
    x_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 4
    thing_width = 100
    thing_height = 100

    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    color = (r, g, b)

    #player = pygame.Surface((x*0,y*0))
    player = pygame.Surface((x,y), pygame.SRCALPHA, 32)
    #player = pygame.Surface([x,y], pygame.SRCALPHA, 32)
    player = player.convert_alpha()
    #pygame.display.update()
    gameExit = False

    # game loop: the logic that happens if you not quit OR crash
    while not gameExit:

        for event in pygame.event.get():    # list of events per frame per secend (clicking etc.)
            # ask if user asks to quit
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    if bonus > 0:
                        x_change = -5*bonus
                    else:
                        x_change = -5
                    player = gesImg_left

                if event.key == pygame.K_RIGHT:
                    if bonus > 0:
                        x_change = 5*bonus
                    else:
                        x_change = 5
                    player = gesImg_right
                if event.key == pygame.K_p:
                    pause = True
                    paused()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    x_change = 0

                elif event.key == pygame.K_RIGHT:
                    x_change = 0
                    player = gesImg_right

            #print(event) # see all the interaction on the screen printed out on the terminal console

        x = x + x_change
        player_img  = player    

        gameDisplay.blit(background,(0,0))

        if dodged == 10 or dodged == 20 or dodged == 30:
            r = random.randint(0,255)
            g = random.randint(0,255)
            b = random.randint(0,255)
            color = (r, g, b)

        things(thing_startx, thing_starty, thing_width, thing_height, color)
        thing_starty = thing_starty + thing_speed + random.randrange(-1,1)

        ges(player_img, x,y)
        things_dodged(dodged)

        if x > display_width-player_width or x < 0:
            crash()

        if thing_starty > display_height:
            thing_starty =  0 - thing_height
            thing_startx = random.randrange(0, display_width)
            r = random.randint(0,255)
            g = random.randint(0,255)
            b = random.randint(0,255)
            color = (r, g, b)
            dodged = dodged + 1

    # managing the speed
            if dodged >= 17:
                thing_speed += random.uniform(-0.8,0.2)
            if dodged >= 27:
                thing_speed += random.uniform(0.8,1)
            if dodged >= 40:
                thing_speed += random.uniform(0.4,1.5)
            else:
                thing_speed += random.uniform(-0.4,0.9)

    # managing the width
            if dodged >= 19:
                thing_width += (dodged * random.uniform(-0.7,0))
            if dodged >= 35:
                thing_width += (dodged * random.uniform(0,1.1))
            if dodged >= 45:
                thing_width += (dodged * random.uniform(0,1.6))
            else:
                thing_width += (dodged * random.uniform(0.8,2))

    #managing the level ups 
        if dodged == 10:
            pygame.mixer.Sound.play(level_up)
            bonus = 1.5
        if dodged == 20:
            pygame.mixer.Sound.play(level_up)
            bonus = 2
        if dodged == 30:
            pygame.mixer.Sound.play(level_up)
            bonus = 2.4
        if dodged == 35:
            pygame.mixer.Sound.play(level_up)
            bonus = 2.8

    # crash condition and calling the crash() function once the crash happened
        if y + 38 < thing_starty + thing_height and (y + player_height)-15 > thing_starty + thing_height:
            if x>thing_startx and x<thing_startx+thing_width or x+player_width>thing_startx and x+player_width<thing_startx+thing_width:
                crash()

        pygame.display.update()
        #fps, running through the loop at a certain pace
        clock.tick(60)  

game_intro()
game_loop()
pygame.quit()
quit()

Upvotes: 2

Views: 181

Answers (1)

skrx
skrx

Reputation: 20438

Just assign one of the ges surfaces to player when the program starts instead of creating the transparent placeholder surface.

player = pygame.Surface((x,y), pygame.SRCALPHA, 32)
player = player.convert_alpha()

# Replace the lines above with this one.
player = gesImg_left

Upvotes: 1

Related Questions