Pygame: Please why is my Background so small


SnakesBite

Still Fresh
Joined
Nov 10, 2012
Messages
3
Hi, I am new to pygame and trying to start off simple. The background image only covers a very small amount of the screen at the top left. In other small programs it has covered the whole screen. Can anyone help to see the problem in my code affecting my background please.


import pygame, sys, random


from pygame.locals import *


pygame.init()


FPS = 30


fpsClock = pygame.time.Clock()


gameSurface = pygame.display.set_mode((640, 480))


pygame.display.set_caption('beginner')


bgImg = pygame.image.load("space_invaders\space_image.png").convert()


shipImg = pygame.image.load('ship.bmp').convert()


position = shipImg.get_rect().move(random.randrange(640), 479)


while True:


gameSurface.blit(bgImg, (0,0))


for event in pygame.event.get():


if event.type == QUIT:


pygame.quit()


sys.exit()


if event.type == KEYDOWN:


# keyboard inputs


if event.key == K_LEFT:


position = position.move(-1, 0)


if event.key == K_RIGHT:


position = position.move(1, 0)


gameSurface.blit(shipImg, position)


pygame.display.update()


fpsClock.tick(FPS)
 
Last edited by a moderator:
Assuming you're on a Pandora / using Linux, you want space_invaders/space_image.png, not space_invaders\space_image.png.


But if that's the case, I'm surprised it doesn't just exit with the error that it can't find it.
 
im on windows vista. ive tried making the path both '\' and '/'. but get the same results.
 
Oh. It may be that your error is specific to Window's implementation of pygame/sdl (I don't have Windows, so I can't test).


Maybe try doing "pygame.display.update()" after "gameSurface.blit(bgImg, (0,0))"
 
im on windows vista. ive tried making the path both '\' and '/'. but get the same results.

This forum is for a particular GNU/Linux handheld (about the size of a DS), not generically Python or Pygame. In the future, try the Pygame mailing list (or the IRC channel, #pygame, on irc.freenode.net). Also, as you know, indentation matters in Python, so it's best to use something like PasteBin to preserve the indentation.


In any case:


First off, it's a bad practice to write paths manually. It's better to use os.path.join, which will ensure that the program works on any platform by always choosing the correct path separator for the system.


I tried your program (after fixing the paths, since I'm on GNU/Linux) with a 640x480 ugly background and a 32x32 "ship". The background displayed just fine. The only thing that didn't display normally was the ship, and the reason for that is simple: it's placed well below the bottom of the screen. In fact, only one pixel of it is within the boundaries of the Pygame window.


What I would do is ditch your use of Rect's move method. Use the normal Rect attributes instead (e.g. left, right, centerx, top); this is easier anyway. Here's my version:


http://pastebin.com/nUgQK4CE


One other thing of note, the ship only moves when the key is pressed (not while it's held) since you're using the keydown event. You might want to change it so that pressing the key starts movement and releasing the key stops movement. Or you could just check if the key is held every step with pygame.key.get_pressed() (if you do this, make sure you still get or at least pump the events).


If indeed the background is only covering a part of the screen for you, I have no idea what could be wrong there, because there's nothing wrong with that part of your code.
 
Last edited by a moderator:
thanks guys. i didnt know this was for a specific platform, sorry about that. Also thanks for the detailed help onpon4, i really appreciate you writting your own version and explaining the key events to me.
 
Back
Top