Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <ctime>
- #include "SDL/SDL.h"
- #include "classes.h"
- SDL_Surface *screen = NULL;
- SDL_Surface *background = NULL;
- SDL_Surface *ship = NULL;
- SDL_Surface *astorider = NULL;
- void quit();
- void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
- void move();
- void game();
- void astroider();
- SDL_Event event;
- //variabler
- Uint8 *key;
- int main(int argc, char *args []) {
- player.down = false;
- player.up = false;
- player.mid = true;
- player.Y = 240;
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_WM_SetCaption("Yeda - SHMUP", NULL);
- screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
- background = SDL_LoadBMP("image/background.bmp");
- ship = SDL_LoadBMP("image/ship.bmp");
- astorider = SDL_LoadBMP("image/asteroids.bmp");
- apply_surface(0, 0, background, screen);
- apply_surface(0, player.Y, ship, screen);
- SDL_Flip(screen);
- game();
- SDL_Delay(2000);
- quit();
- }
- void quit() {
- SDL_FreeSurface(background);
- SDL_FreeSurface(ship);
- SDL_Quit();
- }
- void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface(source, NULL, destination, &offset);
- }
- void move() {
- if( SDL_PollEvent (&event)) {
- if(event.type == SDL_KEYDOWN) {
- switch(event.key.keysym.sym) {
- case SDLK_DOWN:
- //up
- if(player.up == true) {
- player.up = false;
- player.mid = true;
- player.Y = 240;
- }
- //mid
- else if(player.mid == true) {
- player.mid = false;
- player.down = true;
- player.Y = 280;
- }
- break;
- case SDLK_UP:
- //down
- if(player.down == true) {
- player.down = false;
- player.mid = true;
- player.Y = 240;
- }
- //mid
- else if(player.mid == true) {
- player.mid = false;
- player.up = true;
- player.Y = 200;
- }
- break;
- case SDLK_ESCAPE:
- quit();
- }
- }
- }
- }
- void game() {
- while(true) {
- apply_surface(0, 0, background, screen);
- apply_surface(0, player.Y, ship, screen);
- astroider();
- move();
- SDL_Flip(screen);
- }
- }
- void astroider() {
- srand(time(NULL));
- ast.X = 640;
- while(true) {
- ast.Y = rand() % ast.down + ast.mid + ast.up;
- ast.speed = rand() % 10 + 6;
- switch (ast.Y) {
- case ast.down:
- ast.Y = 280;
- break;
- case ast.mid:
- ast.y = 240;
- break;
- case ast.up:
- ast.Y = 200;
- break;
- }
- apply_surface(ast.X, ast.Y, astroider, screen);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment