-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
96 lines (84 loc) · 2.44 KB
/
main.cpp
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
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <iostream>
#include <string>
#include <time.h>
#include "piece.h"
#include "snake.h"
#include "point.h"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_DEPTH 32
using namespace std;
SDL_Surface * loadbg(string name){
SDL_Surface * oldimg = IMG_Load(name.c_str());
if (oldimg == NULL){
cout << "Error to load background : " << SDL_GetError()<<endl;
return NULL;
}
SDL_Surface * bg = SDL_DisplayFormat(oldimg);
SDL_FreeSurface(oldimg);
return bg;
}
int main() {
int px,py;
srand(time(NULL));
SDL_Surface * screen = 0;
bool running = true;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE | SDL_DOUBLEBUF);
Snake * snk = new Snake();
Point * punto = new Point("point.png");
SDL_Surface * bg = loadbg("black_background.bmp");
bool pointexist = true;
SDL_Event * event = new SDL_Event();
while(!snk->getEnd()){
while(SDL_PollEvent(event)){
if(event->type == SDL_QUIT)
snk->setEnd(true);
else if(event->type == SDL_KEYDOWN){
if(event->key.keysym.sym == SDLK_UP)
snk->setDirection(UP);
if(event->key.keysym.sym == SDLK_DOWN)
snk->setDirection(DOWN);
if(event->key.keysym.sym == SDLK_LEFT)
snk->setDirection(LEFT);
if(event->key.keysym.sym == SDLK_RIGHT)
snk->setDirection(RIGHT);
}
}
SDL_BlitSurface(bg, NULL, screen, NULL);
snk->move();
snk->draw(screen);
if(pointexist){
if( (snk->getTesta())->getPosX() == punto->getPosX() && (snk->getTesta())->getPosY() == punto->getPosY()){
snk->pushPiece();
snk->addPoint(punto->getPoint());
delete punto;
pointexist = false;
}
punto->draw(screen);
}else{
px = (rand() % 40) * 16;
py = (rand() % 30) * 16;
punto = new Point(px, py, "point.png");
while(snk->isThereOnAPoint(punto)){
delete punto;
px = (rand() % 40) * 16;
py = (rand() % 30) * 16;
punto = new Point(px, py, "point.png");
}
pointexist = true;
}
if(SDL_Flip(screen) == -1)
return 1;
SDL_Delay(100);
}
cout << "Punteggio totale: "<<snk->getPoints()<<endl;
delete snk;
delete punto;
SDL_FreeSurface(screen);
SDL_FreeSurface(bg);
SDL_Quit();
return 0;
}