2

I'm very new to pygame, and am using the book "Beginning Game developement with Python and Pygame". I have pretty much typed the example code and keep getting the same "pygame error: file not a windows.bmp file" and would like to be able to load jpg/png as in the example in the book. I'm pretty sure I'm in the right directory for mine to work and the images I wanted to use are the same format as in the example. I have also searched for solutions but none of the answers seemed to work for me. The code from the book is as follows (I have python 2.7.4, Ubuntu 13.04 and (I think) pygame 1.2.15):

background_image_filename = 'sushiplate.jpg'
mouse_image_filename = 'fugu.png'

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

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

    screen.blit(background, (0,0))
    x, y = pygame.mouse.get_pos()
    x-= mouse_cursor.get_width() / 2
    y-= mouse_cursor.get_height() / 2
    screen.blit(mouse_cursor, (x, y))

    pygame.display.update()

my version of the code so far:

import os.path
background = os.path.join('Documents/Python/Pygame','Background.jpg')
cursor_image = os.path.join('Documents/Python/Pygame','mouse.png')

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background).convert()
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()
while True:
    for event in pygame.event.get():
            if event.type == QUIT:
                    exit()
    screen.blit(background, (0,0))
    x, y = pygame.mouse.get_pos()
    x-= mouse_cursor.get_width() / 2
    y-= mouse_cursor.get_height() / 2
    screen.blit(mouse_cursor, (x, y))
    pygame.display.update()

Thanks for your help :)

user3376851
  • 149
  • 10
  • 1
    What does `pygame.image.get_extended()` return? – Drewness Mar 03 '14 at 23:08
  • How is this different than the other questions? Which answers have you tried? – Bartlomiej Lewandowski Mar 03 '14 at 23:10
  • possible duplicate of [How to load PNG files with pygame.image?](http://stackoverflow.com/questions/19882491/how-to-load-png-files-with-pygame-image) – Bartlomiej Lewandowski Mar 03 '14 at 23:14
  • ah thanks Bartlomiej, I'll have a look through those now and see if I can solve it :) – user3376851 Mar 03 '14 at 23:20
  • This looks like a path error. What's your directory structure, and how are you running your code? Your code expects a subdirectory structure beginning at the location of your .py file. – John Lyon Mar 03 '14 at 23:35
  • @user3376851 Maybe you could try to confirm your directory structure is correct in python. By the way, neither jpg nor png worked in your program? – Peihui Mar 04 '14 at 15:59
  • @jozzas I'm running it in python via the terminal on ubuntu by importing it and I sys.path.append('Documents/Python/Pygame/') etc because the .py file is saved there as well. – user3376851 Mar 04 '14 at 21:21
  • @Peihui I believe the directory structure is correct and the reason I put the os.path thingy in was to try and solve that problem (as it was suggested on another page for a similar question). and yes when I just try and get the png image it doesn't work either – user3376851 Mar 04 '14 at 21:21
  • @Drewness it just returns 0. – user3376851 Mar 04 '14 at 21:23

1 Answers1

1

I can almost guarantee that you're getting your paths wrong. In your code, you've put in relative paths, meaning that pygame is looking for your assets in subfolders of the working directory (the directory where you execute your code).

A demo of how I think you would have to have things laid out and where your code is looking is below - in this example you would have a command prompt open in /home/your_username/Documents/my_games (or ~/Documents/my_games) and you'd be running python your_game_script.py.

|---home
   |---your_username
       |---Documents
           |---some_subfolder
           |---my_games
               |---your_game_script.py
               |---Documents
                   |---Python
                       |---Pygame
                           |---Background.jpg
                           |---mouse.png

This would work, but I suspect you don't have your folders set up this way, and that's the reason it's not working. If you run an interactive python prompt in the same folder as your game script, try the following:

import os
os.path.isfile('Documents/Python/Pygame/Background.jpg') 
os.path.isfile('Documents/Python/Pygame/mouse.png') 

I suspect the result will be false for both - meaning the files couldn't be found at that subfolder location. I would recommend that you have the following structure for your game files:

|---my_game
    |---your_game_script.py
    |---images
        |---Background.jpg
        |---mouse.png

Then in your_game_script.py you can load the files in the following way:

background = 'images/Background.jpg' #relative path from current working dir
cursor_image = 'images/mouse.png'    #relative path from current working dir

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background).convert()
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()
John Lyon
  • 11,180
  • 4
  • 36
  • 44