Category Archives: Python

Added Graphics!

I’ve recently made some progress for my shooter game! I’ve found the equation I’ve needed to find the point where the slope of the direction crosses the boundaries of the rect for the players, with the intention of having a good starting point for bullets to spawn from. It’s also a great way to determine all the points in the perimeter, obviously. But anyways, here’s the code:

#----------------------------------------------------------------------
def sign(x):
    """returns the sign of x: -1, 0, or 1"""

    return cmp(x, 0)

#----------------------------------------------------------------------
def intersect_perimeter(x, y, w, h):
     """finds the intersection in a rectangle"""

     #if check which side to put the point on
     if abs(y*w) > abs(x*h):
         return (int(0.5*h * x/abs(y)), int(0.5*h * sin(y)))
     else:
         return (int(0.5*w * sin(x)), int(0.5*w * y/abs(x)))

I’ve also had the chance to implement some cheap graphics, including bullets, explosions, and a house, drawn by me, but also a background image, drawn by the most angry of my friends. The results are pretty nice, definitely an improvement over the old off-white background. You can see the player, flames behind him, the place holder house, and the background

Continue reading Added Graphics!

Quick Update

I drew some flames, and posted to /r/gamedev, so I figured I’d mirror it here

http://imgur.com/WggC5

Just added some flames to the dudes. It’s gonna be a topdown shooter ala Geometry Wars and Gatling Gears.

This week I’m going to try to add a background, put shooting back in and scoring.

 

In related news, I’ve made the game redraw and reload the sprites every frame, which comes out to about 60 times a second, if that’s not a drain on performance, I don’t know what it. Luckily, or perhaps not, my PC is tougher than my 4 year old laptop, so it’s taking the load happily.

I’m going to rewrite the shooting this week, like I mentioned, but I’d really like to add seeker missiles, but I’m not quite sure yet how to make the missiles turn gradually. I don’t think it will be that tough, just rotate the sprite a bit every time and adjust the speed, but I’m going to have to take a look at the trig math behind it to choose a function that gradually speeds up and slows down.

Anyways, hopefully I’ll have the time to write a more full post later.

Rewriting Project Shooter !

After a good discussion with a friend of mine, I decided to take a stab at rewriting, once again, Project Squares, not renamed Project Shooter. So far it’s only ~600 lines, including white space and comments, but it’s quite a bit cleaner, neater, and more compact, not to mention commented to bits!

What it is right now is one character instance moving around, but only in one direction. The direction can be changed to west or southwest right now, but as soon as I get back to it, I think I’ll be able to make mouse control a possibilty, allowing for 360degree movement, which will be really nice. It was really important for me to get that character rotation down, as my old code had really gone to hell, and so I wasn’t able to go through it and make changes to it without a ton of headaches. Rewriting and refactoring was definitely the right choice here for me.

I have several things to implement left though, until I’m back to where I was, pre-rewrite: mouse movement, shooting component, which contains hp, damage dealing, kill tracking among other stats. I have to re-implement a display for the frames per second, incidentally, I created a question over at /r/learnpython and got some great responses, as usual. There’s also the AI component I have to re-implement; all that was was a timer that went off every 1/2 second and either moved or rotated the NPCs. If it saw the player during the rotation phase, it would stop rotating and start firing until the next movement phase. There was still never really a high risk of being hit though, since the enemies still fired in a straight line ahead of themselves, unless there was a high amount of enemies. Addionally, I had made use of a module called txtlib which allows PyGame to print on multiline, as well as color, if I’m not mistaken.

I used the builtin glob package, which crawls along folders, but my understanding of it is embarrassingly limited, but I assume should the need to crawl my folders arise again, I will be able to quickly gain a better understanding.

New in this iteration is the simple function that gives each instance of a character a random name, chosen randomly, using the random module, from a list of American Male Names. It’s only a few lines long, but I still feel pretty smart writing it.

I’ll end this entry with a before and after picture of my shooter.

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