[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [pygame]
> > I am trying to start coding to make games. How do you get the
> > background pic to scroll? please give example.
We've been playing with scrolling code in #pygame this morning, and
found out a few things.
First, it's very important that you .convert() any images you load. I
originally forgot to do this; after Pete rebuked me, my framerate
quadrupled :)
Second, you'll find that scrolling by 1 pixel at a time is unnecessary
- I tried scrolling in steps of four pixels, and it still looked
perfectly smooth.
Here's some code. This doesn't include the display-display blit
optimization Pete mentioned, but it still gets 80 fps unaccelerated in
X, and a whopping 550 fps fullscreen in Windows.
You'll find the image I used, twister.jpg, at:
http://pygame.seul.org/ftp/contrib/twister.jpg
Hope this helps.
<--------------------------------code---------------------------->
#! /usr/bin/env python
import pygame, pygame.image
pygame.init()
# create my display
display = pygame.display.set_mode((640, 480), pygame.HWSURFACE)
# load the background
background = pygame.image.load("twister.jpg").convert()
# create the viewport and boundary rects
backrect = pygame.Rect(background.get_rect())
viewport = pygame.Rect((0, 0, 640, 480))
# main loop
dirty = 1
while 1:
framestart = pygame.time.get_ticks()
# process input
if pygame.event.peek(pygame.QUIT):
break
input = pygame.key.get_pressed()
if input[pygame.K_ESCAPE]:
break
if input[pygame.K_LEFT]:
if viewport.left > backrect.left:
viewport.left -= 1
dirty = 1
if input[pygame.K_RIGHT]:
if viewport.right < backrect.right:
viewport.right += 1
dirty = 1
if input[pygame.K_UP]:
if viewport.top > backrect.top:
viewport.top -= 1
dirty = 1
if input[pygame.K_DOWN]:
if viewport.bottom < backrect.bottom:
viewport.bottom += 1
dirty = 1
# draw the image, if necessary
if dirty:
display.blit(background, (0, 0), viewport)
pygame.display.update()
dirty = 0
frameend = pygame.time.get_ticks()
fps = 1000 / ((frameend - framestart) or 1)
print "FPS = " + str(fps)
<----------------------------code ends-------------------------->
--
David "Futility" Clark | How can this mean anything to me, when |
silenus@telus.net | I really don't feel anything at all? |
------------------------------------------------------------------------
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org