Main Unit
The main unit of program Invader
program Invader; { Copyright (c) 2011 Steven Binns Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License, as described at http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ } {$apptype gui} uses CRT, SysUtils, SDL, SDL_GFX, SDL_TTF,SDL_MIXER, enemies, Checks, globals; const AUDIO_FREQUENCY : integer = 22050; AUDIO_FORMAT : word = AUDIO_S16; AUDIO_CHANNELS : integer = 2; AUDIO_CHUNKSIZE : integer = 4096; var level, mousex, mousey : integer; timer : integer; scoretext : string; font : pointer; colour, colour2 : pSDL_COLOR; music : pMIX_MUSIC = nil; spawn : Boolean = false; destrect : pSDL_Rect; begin //Initialise sound, video and assign some variables space and values SDL_INIT(SDL_INIT_VIDEO); SDL_INIT(SDL_INIT_AUDIO); if MIX_OPENAUDIO(AUDIO_FREQUENCY, AUDIO_FORMAT, AUDIO_CHANNELS, AUDIO_CHUNKSIZE) <> 0 then halt; screen := SDL_SETVIDEOMODE(400, 400, 32, SDL_SRCALPHA); fontface := SDL_SETVIDEOMODE(400, 400, 32, SDL_SRCALPHA); if screen = nil then halt; if TTF_INIT = -1 then halt; new(test_event); new(colour); new(colour2); tick := 0; score := 1; music := MIX_LOADMUS('John Stump - Death Waltz.mid'); MIX_VOLUMEMUSIC(20); font := TTF_OPENFONT('C:\Windows\Fonts\Arial.ttf', 20); colour^.r := 0; colour^.g := 255; colour^.b := 0; colour2^.r := 0; colour2^.g := 0; colour2^.b := 0; level := 5; timer := (level * 300) + 5000; MIX_PLAYMUSIC(music, -1); //Begin the main loop. while stop = false do begin //Set the mousex and mousey variables to your mouse's x and y positions. mousex := test_event^.motion.x; mousey := test_event^.motion.y; scoretext := 'SCORE: ' + inttostr(score); if timer = (level * 300) + 4000 then spawn := true; fontface := TTF_RENDERTEXT_SOLID(font, PChar(@scoretext), colour^); SDL_BLITSURFACE(fontface2, nil, screen, nil); SDL_BLITSURFACE(fontface, nil, screen, nil); //draws the score in the top left of the screen if SDL_POLLEVENT(test_event) = 1 then begin if test_event^.type_ = SDL_KEYDOWN then begin if test_event^.key.keysym.sym = 27 then halt; //If esc key is pressed, stop the game end; end; if timer = 0 then begin level := level + 5; timer := (level * 300) + 5000; //after the timer runs out, advance the level end; //begin moving the enemies enemies.TICK_ENEMIES(mousex, mousey); //draw the character FILLEDCIRCLECOLOR(screen, mousex, mousey, level, $FF99006F); FILLEDCIRCLECOLOR(screen, mousex, mousey, level DIV 5, $0000FFFF); //begin drawing enemies enemies.DRAW_ENEMIES(screen, level); FILLEDCIRCLECOLOR(screen, mousex, mousey, level, $000000FF); if spawn = true then begin if random(100) < 10 then enemies.CREATE_ENEMY(level); //Check collisions between enemies and the character. Checks.CHECK_COLLISIONS(screen, level, mousex, mousey); end; inc(tick); if tick = 10 then begin inc(score); tick := 0; end; //Clear the score before the next frame. scoretext := 'SCORE: '; fontface2 := TTF_RENDERTEXT_SHADED(font, PChar(@scoretext), colour2^, colour2^); timer := timer - 10; end; //Freeze enemies on the screen. for i := 0 to 20 do begin if mobs[i, 0] = 1 then begin FILLEDCIRCLECOLOR(screen, mobsx[i], mobsy[i], mobs[i, 3] DIV 2, $00FF00FF); if mobs[i, 4] = 0 then begin FILLEDCIRCLECOLOR(screen, mobsx[i], mobsy[i], mobs[i, 3], $FFFFFF6F); end else if mobs[i, 4] = 1 then begin FILLEDCIRCLECOLOR(screen, mobsx[i], mobsy[i], mobs[i, 3], $9600006F); end; end; end; //Create a destination rectangle for the final message. new(destrect); destrect^.x := 50; destrect^.y := 50; destrect^.h := 50; destrect^.w := 50; timer := 100; //It should fade the text in... //It didn't work... while timer > -255 do begin fontface := TTF_RENDERTEXT_BLENDED(font, 'You were assimilated...', colour^); SDL_BLITSURFACE(fontface, nil, screen, destrect); SDL_FLIP(screen); SDL_DELAY(10); timer := timer - 1; end; SDL_FREESURFACE(screen); SDL_QUIT; end.
Remarks
Could you make such an effective game using SDL?