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
I’m going to implement some text overlays soon, but I’m not quite sure how to do that since PyGame doesn’t support multiline rendering of text. Luckily, there’s the library made just for this, but I can’t find it anymore. I found this function of the pygame mailing lists, but the indentation was mangled
def rendertext(font, text, pos=(0,0), color=(255,255,255), bg=(0,0,0)):
lines = text.splitlines()
#first we need to find image size...
width = height = 0
for l in lines:
width = max(width, font.size(l)[0])
height += font.get_linesize()
#create 8bit image for non-aa text..
img = pygame.Surface((width, height), 0, 8)
img.set_palette([bg, color])
#render each line
height = 0
for l in lines:
t = font.render(l, 0, color, bg)
img.blit(t, (0, height))
height += font.get_linesize()
return img
In other news, I haven’t gotten to the bottom of the angle bug yet, where if I click more than 180 degrees clockwise from the front of my character, the angle is wrong. That’s something that isn’t a huge deal, but it will have to get fixed by the time I want to share the game with other people.
I have three things I still feel like I need to implement before I can call it a game good enough to share: basic AI, walls/destruction, and healthbars. The health bar one would be the easiest one to do, with the AI being the hardest. The AI would be great help with homing missles, as well as NPCs, since it’d be almost the same thing, at the core of it, move towards the player at an angle. Oh, almost forgot, I haven’t started to work on the decoupling the framerate from the logic/render since it hasn’t really been an issue yet, but that is also something that needs to get done fairly soon.
—
The very first module I wrote for this game, which really should be put in the tools.py one, is LoadImages, which contains just two functions, loadImages and scaleImages. What it does is take a folder and a filetype as well as size as arguments, and globs through the folder looking for all the files that match the filetype and then scales each of the images to the size argument, and returned the list of the all the pygame surfaces that were found. This was great for a while, until I realized that it really doesn’t work that well for single images, and so I had to make some changes.
I’m proud of this, I think it’s because I took an old function, and reinvigorated it, rather than cutting it to pieces and just restarting. So the changes I made were very elementary: I just added a check to see if a period character was in the filetype, and if it was, treat the path I glob through as a single file, rather than a ‘*.png’ type, if that makes sense. It’s a small thing, but it boosts my confidence as a programmer, because I can see that I’m improving, however slowly.
Til next time!
Oh, I added a reCaptcha to the comments, so the spam would be cut down, there was about a spam post ever 6 hours or so. Sorry about that!
Ha, you sort your friends by their inherent internal anger magnitudes 😀
The Graphics look cool though, well done. Programmer art for the win!