Code snippets |
Top Previous Next |
Invaders strikes back We define the ship as a panel, but we don’t set its VISIBLE flags yet. We have similar definitions for enemy, rocket (fired by player) and fractal (fired by enemy). function main() restart (); // initialize the game WAITT 16 waits for the level to load; otherwise we can’t set the camera position and angles. I have added a simple wmb sky that rotates using the ent_rotate action included in the template files. function move_ship () } If the left arrow key is pressed and ship’s POS_X is greater than 40, we move the ship to the left; the same thing goes for the right key (ship’s POS_X must be below 600). function move_enemy () If enemy’s POS_X is greater than 40 and enemy_flag = 1, the enemy will move to the left. When the enemy’s POS_X = 40, the enemy will approach the player using the WHILE (counter < 40) loop. Since enemy_flag = 0 now, the enemy will move to the right. When enemy’s POS_X = 580, the enemy will approach the ship again. The enemy will approach the ship as long as the enemy’s POS_Y is below 350. function move_rocket () If the space key is pressed and the ship isn’t hit, we show the rocket and move it upwards, towards the enemy. We are using 18 = (ship sprite width / 2) - (rocket sprite width / 2) as an offset on X because we want to fire the rocket from the middle of the ship sprite. function move_fractal () The enemy fires fractals all the time; as soon as a fractal disappears, it fires a brand new one. We are using 10 = (enemy sprite width / 2) - (fractal sprite width / 2) and 16 = enemy sprite height / 2 as offsets on X and Y because we want to fire the fractal from the center of the enemy. The functions used for collision detection and for restarting the game are pretty simple to understand, we let them as an exercise for the user. Laser guns – how bad do we need them? This was one of the most requested wdl snippets, so here it is. This laser example is using particles and works fine. The idea is to shoot the wall, get the distance between the player and the wall and then generate particles to cover that distance. The particles are created fast, but we have to remove them even faster, otherwise we get three or four parallel laser beams when strafing. The laser can be improved: you can use bitmap particles, attach it to a gun, etc. Here’s the code: ACTION laser_beam // attach it to any entity, it will become invisible SHOOT player.POS,laser_temp; pos2.X = PLAYER.X; laser_particle(); WAIT 1; The function laser_particle is derived from particle_line in template and will generate a “line” of particles between pos1 (hit point) and pos2 (the player). function particle_start() In this action we destroy the particle as soon as it is created. MY_SIZE could be changed depending on the distance so that the particles that are closer to the player are smaller.
|