starship01


This was the first major version of a little game/demo I made using PyGame in 2009. The code shows how a simple game is made. I’ll post the images that go with the code at some point.

from pygame.locals import *
import math
import pygame

screensize = (512, 768) 
deadlyobjects = pygame.sprite.Group()


def getIsOutOfBounds(spriteobj, arena_size):
    if (spriteobj.rect.center[0] > arena_size[0] or spriteobj.rect.center[0] < 0 or spriteobj.rect.center[1] > arena_size[1] or spriteobj.rect.center[1] < 0):
        return True
    return False

def cullOutOfBoundsObjects(spritegroup, arena_size):
    for s in spritegroup:
        if getIsOutOfBounds(s,arena_size):
            spritegroup.remove(s)        

class Starfield:
    def __init__(self,starfieldtile):
        self.starfieldtile = starfieldtile
        self.speed = 30
        w = screensize[0]
        h = screensize[1]
        self.tileside = self.starfieldtile.get_height()
        self.counter = 0
        self.stars = pygame.Surface((w, h + self.tileside)).convert()
        self.lastupdatetime = 0
        for x in range(w/self.tileside + 1):
            for y in range(h/self.tileside + 1):
                self.stars.blit(self.starfieldtile, (x*self.tileside, y*self.tileside))
    def update(self,pygame_ticks):
        t_fact = pygame_ticks-self.lastupdatetime
        self.lastupdatetime = pygame_ticks
        self.counter = (self.counter - self.speed*(t_fact/1000.0)) % self.tileside
    def draw(self, screen):
        screen.blit(self.stars, (0, 0), (0, self.counter, screensize[0], screensize[1]))

class Player(pygame.sprite.Sprite):
    def __init__(self,initpos, pygame_ticks, surface=None):
        pygame.sprite.Sprite.__init__(self)
        if surface:
            self.image = surface
        else:
            self.image = pygame.Surface([30, 30])
            self.image.fill((255,255,255))
        self.rect = self.image.get_rect()
        self.rect.topleft = initpos
        self.speed = (0,0)
        self.lastfiretime = 0
        self.lastupdatetime = pygame_ticks
        self.isfiring = False
        self.mymissiles = []
    def draw(self,screen):
        screen.blit(self.image, self.rect)
    def update(self,pygame_ticks,arena_size):
        global deadlyobjects
        t_fact = pygame_ticks-self.lastupdatetime
        self.lastupdatetime = pygame_ticks
        if self.speed != (0,0):
            if self.rect.topleft[0] > arena_size[0]:
                self.rect.topleft = (0,self.rect.topleft[1])
            elif self.rect.topleft[0] < 0:
                self.rect.topleft = (arena_size[0],self.rect.topleft[1])
            self.rect.topleft = (self.rect.topleft[0]+(self.speed[0]*(t_fact/1000.0)),
                                 self.rect.topleft[1]+self.speed[1]*(t_fact/1000.0))
        if self.isfiring and pygame_ticks-self.lastfiretime > 500:
            self.lastfiretime = pygame_ticks
            m = Missile((self.rect.center[0],self.rect.center[1]),0,100,pygame_ticks,self.image) #nasty clone-gun 😛
            self.mymissiles.append(m)
#            print "added missile at:",self.rect.center
            deadlyobjects.add(m)

class Missile(pygame.sprite.Sprite):
    def __init__(self, initpos, angle, speed, pygame_ticks, surface=None):
        pygame.sprite.Sprite.__init__(self)
        if surface:
            self.image = surface
        else:
            self.image = pygame.Surface([30, 30])
            self.image.fill((255,255,255))
        self.rect = self.image.get_rect()
        self.rect.center = initpos
        self.initpos = initpos
        self.speed = speed
        self.angle = 0
        self.distancefromcenter = 0
        self.lastupdatetime = pygame_ticks
        if angle:
            self.rotate(angle)
#        print "missile created at",self.rect.center,initpos
    def update(self,pygame_ticks):
        t_fact = pygame_ticks-self.lastupdatetime
        self.lastupdatetime = pygame_ticks
        self.distancefromcenter += self.speed*(t_fact/1000.0)
#        self.initpos[0]+
        self.rect.center = (math.cos(self.angle)*self.distancefromcenter,
                             math.sin(self.angle)*self.distancefromcenter)
    def rotate(self,angle):
        self.angle = angle
        self.image = pygame.transform.rotate(self.image, self.angle)
        self.rect = self.image.get_rect()
    def draw(self,screen):
        screen.blit(self.image, self.rect)

def main():
    global deadlyobjects
    pygame.init()
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode(screensize)

    sftile = pygame.image.load("pixmaps/starfield02.png").convert()
    ship01 = pygame.image.load("pixmaps/ship01.png").convert()

    starfield = Starfield(sftile)
    player = Player((screensize[0]/2,screensize[1]-ship01.get_height()),pygame.time.get_ticks(),ship01)
    deadlyobjects.add(Missile((10,10),20,80,pygame.time.get_ticks(),ship01))
    rp = pygame.sprite.RenderPlain()
    mtest = None
    while 1:
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    return
                elif event.key == K_LEFT:
                    player.speed = (player.speed[0]-100,0)
                elif event.key == K_RIGHT:
                    player.speed = (player.speed[0]+100,0)
                elif event.key == K_x:
                    player.isfiring = True
            elif event.type == KEYUP:
                if event.key == K_x:
                    player.isfiring = False
#                if event.key == K_LEFT:
#                    player.speed = (player.speed[0]+100,0)
#                elif event.key == K_RIGHT:
#                    player.speed = (player.speed[0]-100,0)
            if pygame.key.get_pressed()[K_SPACE]:
                deadlyobjects.add(Missile((200,200),360,80,pygame.time.get_ticks(),ship01))
                print deadlyobjects.sprites()
                print "SPACE"

        if pygame.sprite.spritecollide(player, deadlyobjects, False):
            print "you got hit"

        starfield.update(pygame.time.get_ticks())
        rp.update()
        player.update(pygame.time.get_ticks(),screensize)
        cullOutOfBoundsObjects(deadlyobjects,screensize)
        deadlyobjects.update(pygame.time.get_ticks())

        starfield.draw(screen)
        player.draw(screen)
        deadlyobjects.draw(screen)


        rp.draw(screen)
        pygame.display.flip()
        clock.tick(30)

if __name__ == '__main__':
    main()
    pygame.quit()

Posted in: Code by nsundin
Copyright © 2011-2025 Programmatic Verse · RSS Feed
Built on Skeleton
Powerered by Wordpress