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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"
#include <iostream>
#include <sstream>
using namespace std;
string convertInt(int h);
class bar{
int x,y,xvel,life;
SDL_Rect rt;
SDL_Texture *pic = nullptr;
bool dead;
public:
void init(SDL_Surface* my,SDL_Renderer* scre)
{
dead = false;
x = 250;
life = 3;
y = 470;
xvel = 0;
pic = SDL_CreateTextureFromSurface(scre,my);
}
SDL_Rect GetP()
{
rt.x = x;
rt.y = y;
rt.w = 50;
rt.h = 15;
return rt;
}
void Hevent(int h)
{
if(h == 0)
xvel = 0;
else if(h == 1) // left
xvel = -9;
else if(h == 2)
xvel = 9;
}
SDL_Texture* Get(){return pic;}
void move()
{
x += xvel;
if(x < 0)
{
x = 0;
}
else if(x > 450)
{
x = 450;
}
}
bool isDead(){kill(); return dead;}
void kill()
{
if(life == 0)
dead = true;
}
};
class ball{
int x,y,xvel,yvel;
SDL_Rect rt;
SDL_Texture *pic = nullptr;
bool Under;
public:
void init(SDL_Surface* va,SDL_Renderer* my)
{
x = 250;
y = 150;
xvel = 3;
yvel = 3;
pic = SDL_CreateTextureFromSurface(my,va);
Under = false;
}
SDL_Rect* GetP()
{
rt.x = x;
rt.y = y;
rt.w = 25;
rt.h = 25;
return &rt;
}
void reset()
{
x = 250;
y = 150;
xvel = 10;
yvel = 13;
Under = false;
}
bool Hit(SDL_Rect bp)
{
if((x > bp.x) && (x < bp.x + 50) && (y>bp.y) && (y < bp.y + 15))
{
return true;
}
else
return false;
}
bool isUnder(){return Under;}
SDL_Texture* Get(){return pic;}
void move(SDL_Rect hy)
{
x += xvel;
y += yvel;
x < 250 ? x -= 1 : x += 1;
if(Hit(hy) == true)
{
y = hy.y - 50;
yvel *= -1;
}
if(y > 475)
{
reset();
Under = true;
}
else if( y < 0)
{
y = 0;
yvel *= -1;
}
if(x < 0)
{
x = 0;
xvel *= -1;
}
else if(x > 475)
{
x = 475;
xvel *= -1;
}
}
};
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
TTF_Init();
SDL_Color col = {255,255,255,255};
SDL_Event event;
SDL_Rect tpos,t;
SDL_Window *win;
SDL_Texture *time;
string timet = "";
int timecount = 0,pcount = 0,ht = 0;
bool quit = false;
TTF_Font *myfont;
SDL_Surface* m,*tm;
SDL_Renderer *screen;
bar mbar;
ball tball;
win = SDL_CreateWindow("Jumping Ball",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,500,500,SDL_WINDOW_SHOWN);
screen = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(screen,0,0,0,255);
tpos.x = 400;
tpos.y = 35;
tpos.w = 30;
tpos.h = 35;
m = IMG_Load("bar.png");
tm = IMG_Load("ball.png");
mbar.init(m,screen);
tball.init(tm,screen);
timecount = SDL_GetTicks();
myfont = TTF_OpenFont("arial.ttf",28);
timet = convertInt(timecount/1000);
time = SDL_CreateTextureFromSurface(screen,TTF_RenderText_Solid(myfont,timet.c_str(),col));
SDL_RenderClear(screen);
SDL_RenderCopy(screen,tball.Get(),NULL,tball.GetP());
t = mbar.GetP();
SDL_RenderCopy(screen,mbar.Get(),NULL,&t);
SDL_RenderCopy(screen,time,NULL,&tpos);
SDL_RenderPresent(screen);
while(quit == false)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
quit = true;
else if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_LEFT)
{
ht = 1;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
ht = 2;
}
else if(event.key.keysym.sym == SDLK_r)
{
}
}
else if(event.type == SDL_KEYUP)
{
ht = 0;
}
}
mbar.Hevent(ht);
mbar.move();
t = mbar.GetP();
tball.move(t);
SDL_DestroyTexture(time);
time = nullptr;
timecount = SDL_GetTicks();
timet = convertInt(timecount/1000);
time = SDL_CreateTextureFromSurface(screen,TTF_RenderText_Solid(myfont,timet.c_str(),col));
SDL_RenderClear(screen);
SDL_RenderCopy(screen,tball.Get(),NULL,tball.GetP());
t = mbar.GetP();
SDL_RenderCopy(screen,mbar.Get(),NULL,&t);
SDL_RenderCopy(screen,time,NULL,&tpos);
SDL_RenderPresent(screen);
SDL_Delay(15); // Delay frames
}
SDL_DestroyWindow(win);
SDL_DestroyRenderer(screen);
SDL_DestroyTexture(time);
IMG_Quit();
TTF_Quit();
SDL_Quit();
return 0;
}
string convertInt(int h)
{
stringstream ht;
ht << h;
return ht.str();
}
|