[Tutor] Does Pygame allow background images?

Liam Clarke-Hutchinson Liam.Clarke-Hutchinson at business.govt.nz
Tue Nov 8 03:17:36 CET 2005


Oh dear, 

The full code would be (hopefully correct this time)

import sys, pygame
pygame.init()

ball = pygame.image.load("ball.bmp")
background = pygame.image.load("bckgrnd.bmp")
ballrect = ball.get_rect()
backgroundRect = background.get_rect()

size = (width, height) = background.get_size()
screen = pygame.display.set_mode(size)


while 1:
     for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = -speed[1]

	  screen.blit(background, backgroundRect)
        screen.blit(ball, ballrect)
        pygame.display.flip()



Liam Clarke-Hutchinson


-----Original Message-----
From: tutor-bounces+ml.cyresse=gmail.com at python.org
[mailto:tutor-bounces+ml.cyresse=gmail.com at python.org] On Behalf Of Liam
Clarke-Hutchinson
Sent: Tuesday, 8 November 2005 3:06 p.m.
To: 'Nathan Pinno'; Tutor Mailing List
Subject: Re: [Tutor] Does Pygame allow background images?


Hi Nathan,


Just create a surface the same size as your screen, and have a loop like so 

while True:
   <move rects>
   <draw hockey rink, which erases old hockey players>
   <blit hockey players onto moved rects, which are drawn on top of hockey
rink>
   <check for input>


Here's some rough code to add a background for that ball one I posted
yesterday. Bear in mind that I can't check this at work. Lines with !! are
new-

import sys, pygame
pygame.init()

!!background = pygame.image.load("bckgrnd.bmp")#Pygame can handle other
formats. !!backgrountRect = background.get_rect() !!size = (width, height) =
background.get_size() 
#Returns size of image as (x, y) so game screen will be as big as background
screen = pygame.display.set_mode(size)

ball = pygame.image.load("ball.bmp")
ballrect = ball.get_rect()

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

        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = -speed[1]

	  !!screen.blit(background, backgroundRect) #instead of
screen.fill()
        screen.blit(ball, ballrect)
        pygame.display.flip()


That's very basic, but it should work.

www.med.govt.nz 
-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
Of Nathan Pinno
Sent: Tuesday, 8 November 2005 1:24 p.m.
To: Tutor Mailing List
Subject: [Tutor] Does Pygame allow background images?


Hey all,

Does Pygame allow background images (i.e. a hockey rink for a hockey game)?

Thanks,
Nathan Pinno

A new monthly electronic newsletter covering all aspects of MED's work is
now available.  Subscribers can choose to receive news from any or all of
seven categories, free of charge: Growth and Innovation, Strategic
Directions, Energy and Resources, Business News, ICT, Consumer Issues and
Tourism.  See http://news.business.govt.nz for more details.




http://www.govt.nz - connecting you to New Zealand central & local
government services

Any opinions expressed in this message are not necessarily those of the
Ministry of Economic Development. This message and any files transmitted
with it are confidential and solely for the use of the intended recipient.
If you are not the intended recipient or the person responsible for delivery
to the intended recipient, be advised that you have received this message in
error and that any use is strictly prohibited. Please contact the sender and
delete the message and any attachment from your computer.
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

A new monthly electronic newsletter covering all aspects of MED's work is now available.  Subscribers can choose to receive news from any or all of seven categories, free of charge: Growth and Innovation, Strategic Directions, Energy and Resources, Business News, ICT, Consumer Issues and Tourism.  See http://news.business.govt.nz for more details.




http://www.govt.nz - connecting you to New Zealand central & local government services

Any opinions expressed in this message are not necessarily those of the Ministry of Economic Development. This message and any files transmitted with it are confidential and solely for the use of the intended recipient. If you are not the intended recipient or the person responsible for delivery to the intended recipient, be advised that you have received this message in error and that any use is strictly prohibited. Please contact the sender and delete the message and any attachment from your computer.


More information about the Tutor mailing list