-1

Using Python (and Pygame), I have been creating a short one-screen game and I code each part in a different window. In my Home Screen, when I blit the play button onto the screen, it doesn't appear. I am new to Python and Pygame. This is my code:

import pygame, sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((1352,638))
pygame.display.set_caption("Termination: Part 1")
bg = True
playButton = pygame.image.load("Play Button.png")
mouse = pygame.mouse.get_pos()

def playButtonFunction():
    if background == pygame.image.load("Home Screen.png"):
        background.blit(playButton(533.5,278))

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

        if event.type == KEYDOWN and event.key == K_SPACE:
            bg = False

    screen.blit(background,(0,0))
    if bg:
        background = pygame.image.load("Intro Screen.png")
    else:
        background = pygame.image.load("Home Screen.png")
    playButtonFunction()

    pygame.display.update()
Jetter126
  • 23
  • 6

1 Answers1

2

As Frédéric Hamidi already said in a comment, the line

if background == pygame.image.load("Home Screen.png")

will not work as you probably expect.

You should probabkly pass a flag to that method or don't call that function at all when you don't want to show that playButton image.


Also, the line

background.blit(playButton(533.5,278))

will throw an exception, it should look like

background.blit(playButton, (533, 278))

So, change your code to

...

if bg:
    background = pygame.image.load("Intro Screen.png")
else:
    background = pygame.image.load("Home Screen.png")

screen.blit(background,(0,0))    
if !bg:
    background.blit(playButton, (533, 278))

...

Another issue that you load the images from disk every iteration of your game loop (with pygame.image.load). It's enough to load each image once.

sloth
  • 99,095
  • 21
  • 171
  • 219