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

Re: [pygame] pgreloaded 2.0.0-alpha5 released



Hi Gregor,

On, Fri Jul 23, 2010, Gregor Lingl wrote:

> 
> Hi Marcus,
> > Hi Gregor,
> >   
> ...
> >> It seems to me that there is a certain lack of examples and ported
> >> games to pygame2. Perhaps because porting seems to be quite tedious.
> >>     
> >
> > It's absolutely not tedious, but there is a decent lack of resources and
> > time. pygame2 is currently developed by one person (me), who has a funny
> > and incomplete time sharing model for free time to spend :-D.
> >   
> Would your time sharing model allow for an easy and "asolutely not 
> tedious" converting the appended
> script in a "quasi state of the art" way? It could serve me as a model 
> or pattern for trying to convert one
> of my games, just as a first step to dive into pygame2.
[...]

Here we go with the "state of the art" conversion of your script :-).
As you can see, it is mostly about exchanging the modules and certain
API calls as well as changing some arguments (especially colors).  I
left out the time-based update, you are using with Clock.tick() and
applied a static update cycle. To use an approach matching Clock.tick(),
you could use sdl.time.get_ticks() and calculate the update delta
yourself.

Regards
Marcus
SCREEN_SIZE = (800, 600)

import math
import random

import pygame2
import pygame2.sprite
import pygame2.sdl
import pygame2.sdl.constants as sdlconst
import pygame2.sdl.video as sdlvideo
import pygame2.sdl.wm as sdlwm
import pygame2.sdl.event as sdlevent
import pygame2.sdl.time as sdltime
import pygame2.sdlext.draw as sdlextdraw

class Ball(pygame2.sprite.Sprite):

    def __init__(self, screen, pos):
        pygame2.sprite.Sprite.__init__(self)
        self.pos = pos
        self.r = random.randint(10, 25)
        self.image = sdlvideo.Surface((2*self.r, 2*self.r)).convert()
        self.image.set_colorkey(pygame2.Color(0, 0, 0))
        sdlextdraw.circle(self.image, 
                          pygame2.Color (random.randint(0,255),
                                         random.randint(0,255),
                                         random.randint(0,255)),
                          (self.r, self.r), self.r)
        self.rect = pygame2.Rect (self.image.size)
        phi = 2 * math.pi * random.random()
        v = random.randint(290, 305)#(50, 400)
        self.vx = v * math.cos(phi)
        self.vy = v * math.sin(phi)

    def update(self, t):
        x, y = self.pos
        x += t * self.vx
        y += t * self.vy
        if x + self.r > SCREEN_SIZE[0]:
            x = x - 2 * (x + self.r - SCREEN_SIZE[0])
            self.vx = - self.vx
        elif x < self.r:
            x = 2 * self.r - x
            self.vx = - self.vx
        if y + self.r > SCREEN_SIZE[1]:
            y = y - 2 * (y + self.r - SCREEN_SIZE[1])
            self.vy = - self.vy
        elif y < self.r:
            y = 2 * self.r - y
            self.vy = - self.vy
        self.rect.center = self.pos = (x, y)
        

def main():
    sdlvideo.init ()

    screen = sdlvideo.set_mode(SCREEN_SIZE)
    sdlwm.set_caption("move some circles")
    background = sdlvideo.Surface(SCREEN_SIZE).convert()
    background.fill(pygame2.Color(128, 128, 128))
    screen.blit(background, (0, 0))

    balls = pygame2.sprite.Group()
    for i in range(12):
        balls.add(Ball(screen, (random.randint(20,SCREEN_SIZE[0]-25),
                                random.randint(20,SCREEN_SIZE[1]-25))))

    #clock = pygame.time.Clock()
    running = True

    while running:
        #t = clock.tick(60)
        
        for event in sdlevent.get():
            if event.type == sdlconst.QUIT:
                running = False
            # add balls by left-clicking
            # remove balls by right-clicking
            if event.type == sdlconst.MOUSEBUTTONDOWN:
                if event.button == 1:
                    balls.add(Ball(screen, event.pos))
                elif event.button == 3:
                    sprites = balls.sprites()
                    if sprites:
                        balls.remove(sprites[0])

        balls.clear(screen, background)
        balls.update(0.016) # ~ 16/1000
        balls.draw(screen)
        screen.flip()
        sdltime.delay (16) # ~ 60 fps

if __name__ == "__main__":
    main()
                    

Attachment: pgpYe0aBRRlxg.pgp
Description: PGP signature