[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Two questions regarding a simple space shooter



On Wed, 2006-05-24 at 10:52 +0200, Kai Kuehne wrote:
> I'm pondering how to handle the stars. For now, I just use
> screen.set_at and "overdraw" the old star with the new one.

> Another question is how to handle the keyboard input.
> I wanted to control the ship by the left and right arrow keys and
> as long I press $the button, the ship shall fly into this direction. :-)


I'd recommend a look through the pygame examples. There is a starfield
example as well as the "aliens" one where you drive a car left and right
with the arrow keys.

In Solarwolf I use a "starfield" class, which works out better than a
big collection of Star classes. The Solarwolf stars a little more
complicated because it will dynamically add or reduce the number of
stars based on the current framerate. The Solarwolf stars also draws two
independent starfields on alternating frames. This gives them a flicker
appearance.


#player ship class

import pygame
from pygame.locals import *
import game, gfx, math
from random import randint
import gameinit

class Stars:
    def __init__(self):
        stars = []
        scrwide, scrhigh = gfx.rect.size
        self.maxstars = 800
        for x in range(self.maxstars):
            val = randint(1, 3)
            color = val*40+60, val*35+50, val*22+100
            speed = -val, val
            rect = Rect(randint(0, scrwide), randint(0, scrhigh), 1, 1)
            stars.append([rect, speed, color])
        half = self.maxstars / 2
        self.stars = stars[:half], stars[half:]
        self.numstars = 50
        self.dead = 0
        self.odd = 0


    def recalc_num_stars(self, fps):
        if isinstance(game.handler, gameinit.GameInit):
            #don't change stars while loading resources
            return
        change = int((fps - 35.0) * 1.8)
        change = min(change, 12) #limit how quickly they can be added
        numstars = self.numstars + change
        numstars = max(min(numstars, self.maxstars/2), 0)
        if numstars < self.numstars:
            DIRTY, BGD = gfx.dirty, self.last_background
            for rect, vel, col in self.stars[self.odd][numstars:self.numstars]:
                DIRTY(BGD(rect))
        self.numstars = numstars
        #print 'STAR:', numstars, fps, change


    def erase_tick_draw(self, background, gfx):
        R, B = gfx.rect.bottomright
        FILL, DIRTY = gfx.surface.fill, gfx.dirty
        for s in self.stars[self.odd][:self.numstars]:
            DIRTY(background(s[0]))
        self.odd = not self.odd
        for rect, (xvel, yvel), col in self.stars[self.odd][:self.numstars]:
            rect.left = (rect.left + xvel) % R
            rect.top = (rect.top + yvel) % B
            DIRTY(FILL(col, rect))
        self.last_background = background


    def eraseall(self, background, gfx): #only on fullscreen switch
        R, B = gfx.rect.bottomright
        FILL = gfx.surface.fill
        for s in self.stars[0][:self.numstars]:
            background(s[0])
        for s in self.stars[1][:self.numstars]:
            background(s[0])