1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#include <iostream>
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
using namespace std;
vector<SDL_Texture*> players;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *ball;
SDL_Texture *wall;
SDL_Surface* ballTemp;
SDL_Surface* wallTemp;
SDL_Rect* ballRecSrc;
SDL_Rect* ballRecDst;
SDL_Rect* wallRecSrc;
SDL_Rect* wallRecDst;
SDL_Event event;
const int SCREEN_WIDTH = 670;
const int SCREEN_HEIGHT = 600;
bool ballUp,ballDown,ballLeft,ballRight;
bool isCollisionUp,isCollisionDown;
int PlayerCount = 0;
bool gameInit(){
if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
cout << "error" << endl;
return false;
}
if(IMG_Init(IMG_INIT_PNG) < 0){
cout << "error" << endl;
return false;
}
window = SDL_CreateWindow("The GTG game",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_RESIZABLE);
renderer = SDL_CreateRenderer(window,-1,0);
ballTemp = IMG_Load("BALL.png");
wallTemp = IMG_Load("WALL.png");
ball = SDL_CreateTextureFromSurface(renderer,ballTemp);
wall = SDL_CreateTextureFromSurface(renderer,wallTemp);
SDL_SetRenderDrawColor(renderer,255,255,255,255);
ballRecDst = new SDL_Rect;
wallRecDst = new SDL_Rect;
ballRecDst->h = 80;
ballRecDst->w = 80;
ballRecDst->x = 450;
ballRecDst->y = 0;
wallRecDst->h = 90;
wallRecDst->w = 95;
wallRecDst->x = 500;
wallRecDst->y = 100;
ballUp = false;
ballDown = true;
ballLeft = false;
ballRight = false;
isCollisionUp = false;
isCollisionDown = false;
return true;
}
void render(){
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,ball,NULL,ballRecDst);
SDL_RenderCopy(renderer,wall,NULL,wallRecDst);
for(int i = 0; i < players.size(); i++){
SDL_RenderCopy(renderer,players.at(i),NULL,destRecs.at(i));
}
SDL_RenderPresent(renderer);
}
void updateScreen(){
for(int i = 0; i < destRecs.size();i++){
if(destRecs.at(i)->y > SCREEN_HEIGHT-destRecs.at(i)->h){
up[i] = true;
down[i] = false;
}
if(destRecs.at(i)->y < 1){
up[i] = false;
down[i] = true;
}
if(down[i]){
destRecs.at(i)->y += 5;
SDL_Delay(5);
}
if(up[i]){
destRecs.at(i)->y -= 5;
SDL_Delay(5);
}
}
}
bool collision(){
if(ballRecDst->y + ballRecDst->h <= wallRecDst->y){
cout << "3:: :::: false" << endl;
return false;
}
// if(ballRecDst->x >= wallRecDst->x + wallRecDst->w) commented out lines
// return false; commented out until I get top and bottom working
// if(ballRecDst->x + ballRecDst->w <= wallRecDst->x)
// return false;
if(ballRecDst->y >= wallRecDst->y + wallRecDst->h -10){
cout << " 3:: low false" << endl;
return false;
}
if(ballUp){
cout << " 3:: COLLISION: UP" << endl;
isCollisionUp = true;
}
if(ballDown){
isCollisionDown = true;
}
return true;
}
void ballMove(){
if(ballRecDst->y == SCREEN_HEIGHT - ballRecDst->h){
ballDown = false;
ballUp = true;
}
if(ballRecDst->y == 0){
ballDown = true;
ballUp = false;
}
if(ballRecDst->x == 0){
ballLeft = false;
ballRight = true;
}
if(ballRecDst->x > SCREEN_WIDTH-ballRecDst->w){
ballRight = false;
ballLeft = true;
}
if(ballDown){
if(collision() && !isCollisionUp && isCollisionDown){
cout << "4:: down colission" << endl;
ballDown = false;
ballUp = true;
ballRecDst->y-=10;
isCollisionDown = false;
return;
}
ballRecDst->y+=5;
}
if(ballUp){
if(collision() && !isCollisionDown){
ballUp = false;
ballDown = true;
cout << "4 :: up collison" << endl;
return;
}
cout << "ball up" << endl;
ballRecDst->y-=5;
}
if(ballLeft){
ballRecDst->x-=5;
}
if(ballRight){
ballRecDst->x+=5;
}
}
| |