All posts by Tankor Smash

Pong clone – pre post

Thanks to the efforts with the Particle Simulator I had made earlier, I have successfully made a working pong clone. There’s a score board that even moves along with the mouse. I’m pretty excited about that. I’ve drank a pot and a half of coffee, so I blame the progress on that.

What I want to tackle next with the pong app is debris that breaks of from the paddles as there is a collision, since that would help out nicely in my shooter game that I’m building up to. I’ve made some good progress as far as understanding vectors. I can use them a bit more confidently now, which pays in dividends pretty quickly.

Unfortunately, I’ll need to get a handle on delta time now, in order to unlock the game from the framerate, which is actually pretty great on my new PC, over my old laptop.

I’ll have a video of the pong up later tonight.

Particle Simulator

Well I got it under control. The reason it was all crazy acting, is because of the collision detection I had going, which is shown in the code at the bottom of this post. Check out the short video here

The problem was the I was testing the instances rect against all the rects on the screen, including itself, so it always registered a collision, even if it was alone, which was how I managed to debug it. Pretty strange out bugs turn out to be simple in that they’re one-line changes, but the behaviour can change significantly.

I really like the list comprehensions, now that I have a better grasp on the concept. Here’s the comprehension if you have a hard time finding it:

 #a new list without self in it:
listWithoutSelf = [i for i in listParticlesRect if i != self.blitted]

Continue reading Particle Simulator

Particle Simulator

Back from the break.

I decided to downgrade my project once again, because I guess my steps weren’t of a baby sized variation. In case you’re keeping track, it’s gone from the world’s best Python Roguelike, to a top down shooter, to a pong clone, and now finally, a particle simulator! I’m pretty sure this is a normal progression, because all it means is I’m learning what I’m capable of.

So anyways, I’m looking to create a simple physics simulator to fool around with, and I got stuck on what was a simple case of not using the right variables. Typical stuff, and then tonight I got stuck on inter-particle collisions, because once I got the wall collisions working I felt pretty good about it. Little did I know, life was going to get more complicated.

Pretty much we’re back to the beginning with the endless wall bouncing, and I’m not sure why exactly. The thing that seemed to cause it, is I’m resetting the list that contains all the rects of the particles, but as far as I know, the only thing that needs to use that list is the collision functions. The reason I wanted to reset that list every frame was just because I wasn’t updating the rects in it, so every collision test was testing against the original positions of the particles. Empyting and re-filling it every frame seemed to do the trick at first, but in the end, it seems like it didn’t really help at all.

Oh well, another night I’ll work through it and get it nice and functioning.

On another note, I’m going to try to write at least 200 words 3 times a week in order to build up my writing chops, and hopefully get a bit of a helping crowd going.

Talk to you Thursday!

Pygame Rotation Tutorial

Hey there,

So what I was recently having some trouble with was with Python’s PyGame Rotate function. I was having trouble understanding what some of from the comments found here meant, or were trying to illustrate, let alone the documentation given, so I resolved to figure it out , once and for all, so hopefully this post will be enough to get someone else on track. I think I was getting caught up on the different ways to deal with blitting and surfaces and rects.

Continue reading Pygame Rotation Tutorial

Brainstorm: Grids

Hey there, this post is going to be a brainstorm point in which I just type out my thoughts as they come to me about how to implement a simple grid into my pygame program.

Alright, so first off, I need a grid because I imagine it will help out in the long run for things like movement, object creation and saving/loading mechanisms. As the game’s set up for now, movement is based on pixels, so they path-finding is quite a bit slow, with 250k (500hx500w) nodes at the moment and that’s really exceptionally high and python’s not built for speed… So anyways I need to split up the screen for that reason first.

So what is a grid then? A grid, as far as I understand it, is a way to divide the game’s surface into smaller, more manageable sections. With these sections I could possibly isolate and create new arrays specific for each section which would each contain the items I’d have laying in it. You can draw images per grid section and use that to draw your game world. There’s always more.

So in order to implement the grid into my game, I need to figure out a way to code them in. So, they’re squares that need to be adjacent to each other. They should each be in an instance of some generic Grid class that will contain basic details, like vision, allow movement or not, contain items, elevation, that sort of thing.

If I were to create it I think I should create them in a “for” loop, so something like for w in screen_width, and for h in screen_height, create Grid instance. I guess that’s good for now, what if I want to create a game world that is bigger than one single screen? I’d need to create a new variable like world_width instead and use that to create a grid.

How would I store them? I used the roguebasin tutorial for python+libtcod a while back, they appended each tile to a list within a list so if you wanted a tile at co-ords: 10,10 you’d look up gridList[10][10], accessing the width list, and then inside it, the heightlist.

That reminds me though, say I do want to create an object at co-ords 100,200; how would I figure out what grid section I’m clicking in? Would each click’s co-ords need to search through the entire list of all grids, or would I append the co-ords that each grid contains as an attribute to a grid. If I do that then, what will I do when I start scrolling the screen? Wouldn’t I need to change then all the pixels inside each Grid.coords’s list? I guess I’ll have to worry about that later.

So what I’m going to do now is to create a new small script that creates that dummy grid instance for every 5 pixels which will bring down the work the path-finding has to do five times, to 50k pixels it could have to look through. I’m not sure if that’s the best way to do it for now, but it’s never written in stone.

Until next time.

UPDATE

I’ve tried doing a few things:

        for w in xrange(0, 500 +1):
            for h in xrange(0, 500 +1):
                if h % 5 == 0:
                    #every 5 pixels wide:
                    if w % 5 == 0:
                    #if newGridStart:
                        g = Grid()
                        g.coords.append((w, h))

                        #print 'created grid at ', w, h

                    else:
                        print w, h
                elif w % 5 == 4 and h % 5 == 4:
                    self.listOfGrids.append(g)
                    print 'appended', w, h

and tried to clean it up to make it look like this:

        for w in xrange(0, 500 +1):
            for h in xrange(0, 500 +1):

                if w % 5 == 0 and h % 5 == 0:
                    g =  Grid()
                    print 'new grid started at', w, h
                elif w % 5 == 4 and h % 5 == 4:
                    self.listOfGrids.append(g)
                    print 'appended grid at', w, h

                else:
                    g.coords.append((w, h))
                    print 'appended coords at', w, h

but that added way too many coords to each grid section or too little depending on which I ran.

So I thought about it some more and I’m going to go over the pixels twice. Once to get all the topleft corners of each grid, then I’m going to fill the grids up with coords based on the topleft corner co-ord. I’ll update this later, when I write out that code.

UPDATE 2

Alright, awesome, I’m pretty happy, I managed to work it out, using pretty much what I had described above. If you try to run the code yourself, you might want to comment out that print statement that shows all the co-ords in a given grid section
    def createGrid(self):
        self.listOfGrids = []

        #go through all the pixels in a game screen
        for w in xrange(0, 500 +1):
            for h in xrange(0, 500 +1):
                #if the co-ords are both muliples of 5:
                if w % 5 == 0 and h % 5 == 0:
                    g =  Grid()
                    g.origin = w, h
                    self.listOfGrids.append(g)
                    #print 'new grid started at', w, h

        print 'done gridding, now adding all the points to each grid'

        #for each grid in list, go through at add all the pixels it covers
        for grid in self.listOfGrids:
            origin =  grid.origin #origin is the top left pixel of the grid
            for w in xrange(0, 5):
                for h in xrange(0, 5):
                    x, y =  map(sum,zip(origin,(w, h)))
                    #what this is is:
                    #zipped= zip(origin, (w,h) )
                    #map(sum, zipped)
                    new_coord =  (x, y)
                    grid.coords.append(new_coord)
                    #print 'added {0} to {1} grid'.format(new_coord, origin)
            print grid.coords, 'length :', len(grid.coords)
        print 'done adding all co-ords'

Update 3

Thanks to a commenter, Matt, I have further optimized the grid code to the following which is about five(!) times less code:

    def createGrid(self):
        self.listOfGrids = []
        
        #go through all the pixels in a game screen
        for w in xrange(0, 500 +1, 5):
            for h in xrange(0, 500 +1, 5):
                g =  Grid()
                g.origin = w, h
                self.listOfGrids.append(g)
                                       
        print 'done gridding, now adding all the points to each grid'
        
        #for each grid in list, go through at add all the pixels it covers
        for grid in self.listOfGrids:
            origin =  grid.origin  #origin is the top left pixel of the grid
            for w in xrange(0, 5):
                for h in xrange(0, 5):
                     grid.coords.append((origin[0] + w, origin[1] + h))
                    
                  
        print 'done adding all co-ords'