CobraPy? CobraPew! Pew!
It has been a week and I had not really touched CobraPy my 80s-style python environemnt at all. UNTIL YESTERDAY.
So, how is it looking now? Like this, using assets from the AWESOME kenney.nl:
I have added:
- Support for music (you are listening to 80616-lunchip.xm)
- Support for sounds
- Some improvements to the sprite engine (reusing textures and such)
- Better keyboard support, or at least one that's more useful for games
- Fixed a bunch of bugs
- Added collision detection (did not make it to the video but it's there)
So let's go over the whole source code for that "game" and see how it works? Keep in mind that this code will look bizarre for your standard Python programmer because it's deliberately done in a ... "basic" style? Even in a BASIC style. Which means it aims to be more "structured programming" than what you are used to seeing.
Also, there are no guarantees that any of the APIs exposed will even exist tomorrow, since this is still in a prototyping phase and things will change.
We created the intrepid player's ship, and put it "somewhere". Sprites are identified by a name. All sprite operations will use that name to know what sprite should be affected.
Again, just a few ships with different looks.
A function to move the fleet as a whole, and some info about it, location and speed.
Same for our lonely bullet, but we have a couple of new things. With load_sound
we load into the program a
sound (surprise!) and with play_sound
it's played. Just like sprites, they are identified by name.
We keep track of when we shoot because this is a ship, not a machinegun, Jim!
Collision detection! If the bullet hits an invader ... well, we can't make it explode yet, but we move the bullet out and allow the user to shoot again, at least.
Just load and play a nice chiptune! As usual, music is identified by a label, so we can have more than one and play the one we want.
The gameloop
function is special. Whatever you put here CobraPy will try to run it 60 times a second.
No faster, maybe slower if your gameloop
takes too long. So this is where you do things like update
the screen and interact with the user. You know ... a game loop.
User interaction! Basically is_pressed
takes a key identifier (nice constants coming soonish) and tells you
if it's pressed or not. So, if right-arrow is pressed, increase x
(within limits), if left-arrow is, decrease it (within limits). If "a" is
pressed and you have not shot for a second, shoot.
Well, that's why we increased / decreased it, right?
The fleet moves to the right, then to the left, and at every turn, down. Does it remind you of any game you've seen?
Bullet go up.
Enemy go boom. Eventually.