Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- #define RIGHE 20
- #define COLONNE 20
- using namespace std;
- struct blocco{
- int x;
- int y;
- struct blocco * prossimo;
- };
- struct coordinata{
- int x;
- int y;
- };
- void inizializza(int griglia[RIGHE][COLONNE]){
- for(int i=0;i<RIGHE;i++)
- for(int k=0;k<COLONNE;k++)
- griglia[i][k]=0;
- return;
- }
- void print(int griglia[RIGHE][COLONNE]){
- for(int i=0;i<RIGHE;i++){
- for(int k=0;k<COLONNE;k++)
- cout << griglia[i][k];
- cout << endl;
- }
- cout << endl;
- return;
- }
- bool death(struct blocco* snake){
- struct blocco* p = snake;
- struct blocco* q = snake;
- while(p != NULL){
- q=p;
- p = p-> prossimo;
- }
- if(q->x == 0 || q->y == 0 || q->x == COLONNE - 1 || q->y == RIGHE - 1)
- return true;
- p=snake;
- q=snake;
- while(p!=NULL){
- q = p->prossimo;
- while(q!=NULL){
- if(p->x == q->x && p->y == q->y)
- return true;
- q=q->prossimo;
- }
- p=p->prossimo;
- }
- return false;
- }
- void make_griglia(int griglia[][COLONNE],struct blocco* snake,int mela_x,int mela_y){
- griglia[mela_x][mela_y]=2;
- struct blocco* p = snake;
- while(p != NULL){
- if(p->prossimo != NULL)
- griglia[p->x][p->y]=1;
- else
- griglia[p->x][p->y]=3;
- p = p-> prossimo;
- }
- return;
- }
- int main(){
- srand(time(NULL));
- struct blocco* snake = new struct blocco;
- snake->x = RIGHE/2;
- snake->y = COLONNE/2;
- snake->prossimo = NULL;
- struct coordinata velocita;
- velocita.x=1;
- velocita.y=0;
- struct coordinata mela;
- mela.x = rand() % (RIGHE-1) +1;
- mela.y = rand() % (COLONNE-1) +1;
- int griglia[RIGHE][COLONNE];
- inizializza(griglia);
- bool game_over;
- while(death(snake)==false){
- //MOSSA
- char move;
- cin >> move;
- if(move == 's'){
- if(velocita.x != -1)
- velocita.x = 1;
- velocita.y = 0;
- }
- if(move == 'w'){
- if(velocita.x != 1)
- velocita.x = -1;
- velocita.y = 0;
- }
- if(move == 'a'){
- velocita.x = 0;
- if(velocita.y != 1)
- velocita.y = -1;
- }
- if(move == 'd'){
- velocita.x = 0;
- if(velocita.y != -1)
- velocita.y = 1;
- }
- struct blocco* old_head = snake;
- while(old_head -> prossimo != NULL)
- old_head = old_head -> prossimo;
- struct blocco* n_head = new struct blocco;
- n_head -> x = old_head -> x + velocita.x;
- n_head -> y = old_head -> y + velocita.y;
- n_head -> prossimo = NULL;
- old_head -> prossimo = n_head;
- if(n_head -> x == mela.x && n_head -> y == mela.y){
- mela.x = rand() % (RIGHE-1) +1;
- mela.y = rand() % (COLONNE-1) +1;
- }
- else
- snake = snake -> prossimo;
- game_over = death(snake);
- inizializza(griglia);
- make_griglia(griglia,snake,mela.x,mela.y);
- print(griglia);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement