Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include <iostream>
- #include "classes.h"
- #include "surfaces.h"
- using namespace std;
- int screen_width = 640;
- int screen_height = 480;
- int screen_bpp = 32;
- SDL_Surface *screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE);
- bool gameLoop = true;
- void loadVar();
- void game();
- void collision();
- 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 load_sprite(int x, int y, int bx, int by, int width, int height, SDL_Surface *source, SDL_Surface *destination) {
- SDL_Rect rect;
- SDL_Rect offset;
- rect.x = bx;
- rect.y = by;
- rect.w = width;
- rect.h = height;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface(source, &rect, destination, &offset);
- }
- int main(int argc, char *args[]) {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_WM_SetCaption("Kaktus - PickItUp", NULL);
- Uint32 colorkey = SDL_MapRGB( tileset->format, 255, 0, 255);
- SDL_SetColorKey( tileset, SDL_SRCCOLORKEY, colorkey );
- loadVar();
- game();
- SDL_FreeSurface(tileset);
- cout << "Program exit!" << endl;
- SDL_Quit();
- }
- void loadVar() {
- cPlayer.pos = 1;
- cPlayer.y = (480-32-10);
- }
- void game() {
- Uint8 *key;
- int x[3];
- x[0] = 32 + 10;
- x[1] = (640/2)-16;
- x[2] = 640-32-10-32;
- double ballX[3];
- ballX[0] = x[0];
- ballX[1] = x[1];
- ballX[2] = x[2];
- double ballY[3];
- ballY[0] = 0;
- ballY[1] = -32;
- ballY[2] = -72;
- double ballYspeed[3];
- for(int i=0; i<3; i++) {
- ballYspeed[i] = 0.5;
- }
- cPlayer.x = x[1];
- while(gameLoop) {
- SDL_PumpEvents();
- key = SDL_GetKeyState(NULL);
- if((key[SDLK_LEFT]) && cPlayer.pos == 1) {
- cPlayer.pos = 0;
- cPlayer.x = x[0];
- SDL_Delay(100);
- } else if((key[SDLK_LEFT]) && cPlayer.pos == 2) {
- cPlayer.pos = 1;
- cPlayer.x = x[1];
- SDL_Delay(100);
- }
- else if((key[SDLK_RIGHT]) && cPlayer.pos == 0) {
- cPlayer.pos = 1;
- cPlayer.x = x[1];
- SDL_Delay(100);
- } else if((key[SDLK_RIGHT]) && cPlayer.pos == 1) {
- cPlayer.pos = 2;
- cPlayer.x = x[2];
- SDL_Delay(100);
- }
- for(int i = 0; i<3; i++) {
- ballY[i] += ballYspeed[i];
- }
- if(key[SDLK_ESCAPE]) {
- gameLoop = false;
- }
- for(int i = 0; i<3; i++) {
- if(ballX[i] + 32 > cPlayer.x && ballX[i] < cPlayer.x + 32)
- if(ballY[i]+32 > cPlayer.y && ballY[i]+32 < cPlayer.y + 32) {
- ballY[i] = 0 - 32;
- ballYspeed[i] += 0.23;
- }
- }
- for(int i = 0; i<3; i++) {
- if(ballY[i] > 480) {
- ballY[i] = 0 - 32;
- }
- }
- apply_surface(0, 0, background, screen);
- load_sprite(ballX[0], ballY[0], 32+2, 1, 32, 32, tileset, screen);
- load_sprite(ballX[1], ballY[1], 32+2, 1, 32, 32, tileset, screen);
- load_sprite(ballX[2], ballY[2], 32+2, 1, 32, 32, tileset, screen);
- load_sprite(cPlayer.x, cPlayer.y, 1, 1, 32, 32, tileset, screen);
- SDL_Flip(screen);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment