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
|
#include <cstdlib>
#include<iostream>
#include "SDL/SDL.h"
#include <string>
#include <ctime>
#include <vector>
SDL_Surface *screen;
int showmenu(){
std::cout<<"what game would you like to play"<<std::endl;
std::string game;
std::cin>>game;
}
void borders(int x, int y, Uint32 color,int w,int h){
SDL_Rect Tmp = {x,y,w,h};
SDL_FillRect(screen,&Tmp,color);
}
bool coll(SDL_Rect rect){
rect.x;
rect.y;
if(10==rect.x){
return false;
}
else if (10==rect.y){
return false;
}
else if (630==rect.x){
return false;
}
else if (470==rect.y){
return false;
}
else{
return true;
}
}
int main(int argc, char** argv)
{
srand(time(0));
int width=640;
int height=480;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
bool running = true;
int FPS = 80;
Uint32 start;
bool b[4]={0,0,0,0};
SDL_Rect rect;
rect.x = 30;
rect.y = 30;
rect.w = 20;
rect.h = 20;
int foodX =rand()%600+10;
int foodY =rand()%430+10;
// Snake and Screen colors
Uint32 backRound = SDL_MapRGB(screen->format, 0xff,0xff,0xff);
std::vector<Uint32>snakeBody[4];
snakeBody[1] = SDL_MapRGB(screen->format, 0,0,0xff);
//snakeBody[1] = borders(rect.x,rect.y,SDL_MapRGB(screen->format, 0,0,0xff),rect.w,rect.h);
while(running) {
start = SDL_GetTicks();
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_UP:
b[0]=1;
b[1]=0;
b[2]=0;
b[3]=0;
break;
case SDLK_LEFT:
b[0]=0;
b[1]=1;
b[2]=0;
b[3]=0;
break;
case SDLK_DOWN:
b[0]=0;
b[1]=0;
b[2]=1;
b[3]=0;
break;
case SDLK_RIGHT:
b[0]=0;
b[1]=0;
b[2]=0;
b[3]=1;
break;
}
break;
}
}
int points=0;
//snake eats
if (foodY>=rect.y+rect.h);
else if (foodX >=rect.x+rect.w);
else if (foodY+rect.h<=rect.y);
else if (foodX+rect.w<=rect.x);
else{
foodX = rand()%620+10;
foodY = rand()%460+10;
int i=0;
++i;
points += 1;
FPS+=10;
//snakeBody(i)=borders(rect.x-50,rect.y,SDL_MapRGB(screen->format,0,0,0xff),rect.w,rect.h);
}
//moving snake
if(b[0] ){
rect.y--;
}
else if (b[1] ){
rect.x--;
}
else if (b[2] ){
rect.y++;
}
else if (b[3] ){
rect.x++;
}
SDL_FillRect(screen, &screen->clip_rect, backRound);
SDL_FillRect(screen, &rect, snake);
//Snake Food
borders(foodX,foodY,SDL_MapRGB(screen->format,0,0xff,0),rect.w,rect.h);
//border
borders(0,0,SDL_MapRGB(screen->format,0xff,0,0),width,10);
borders(0,0,SDL_MapRGB(screen->format,0xff,0,0),10,height);
borders(0,height -10,SDL_MapRGB(screen->format,0xff,0,0),width-10,10);
borders(width-10,0,SDL_MapRGB(screen->format,0xff,0,0),10,height);
SDL_Flip(screen);
if(1000/FPS > SDL_GetTicks()-start) {
SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
}
if (coll(rect)==false){
SDL_Quit();
}
}
SDL_Quit();
return 0;
}
|