cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : snake game
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  snake game

soka2oo7 (10)
i'm trying to make a snake game & i can't make the lenght of the snake increase & it's score & the fruit appears one time only & i have to press on the arrow all the time please help me in this code today please.
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
//start food function
void food()
{
     int difrencex = 10 - 630;
     int difrencey = 30 - 470;
     int salma = 0;
     int foodx;
     int foody;
     time_t t;
     srand((unsigned) time(&t));
     for(int i=0;i<salma;i++)
     rand();
     foodx = (rand() % difrencex);
     foody = (rand() % difrencey); 
     for(int i=0; 630>foodx>10 && 470>foody>30;i++) 
     {
     foodx = (rand() % difrencex);
     foody = (rand() % difrencey);    
     }       
     putpixel(foodx,foody,15);
     salma++; 
     static int foodcount = 0;
     score = 0;
     int nextx;
     int nexty;
     nextx = snake.head_x;
     nexty = snake.head_y;
     if(snake.head_dir == RIGHT)
     {
                       nextx++;
     }
     if(snake.head_dir == LEFT)
     {
                       nextx--;
     }
     if(snake.head_dir == UP)
     {
                       nexty--;
     }
     if(snake.head_dir == DOWN)
     {
                       nexty++;
     }
     int nextpixel;
     nextpixel = getpixel(nextx,nexty);
     if(nextpixel==15) 
     {
                  
     if (snake.tail_dir == UP)
           {
             for ( int i = 0; i<101;i++) 
             putpixel (snake.tail_x,snake.tail_y+i,15); 
             snake.tail_y +=100;
           }
        if (snake.tail_dir == DOWN)
          {
            for (int i = 0; i<101;i++) 
            putpixel (snake.tail_x,snake.tail_y-i,15); 
            snake.tail_y -=100;
          } 
        if (snake.tail_dir == LEFT)
          {
            for (int i = 0; i<101;i++)
            putpixel (snake.tail_x+i,snake.tail_y,15); 
            snake.tail_x +=100;
            //printf ("LEFT");
          }
        if (snake.tail_dir == RIGHT)
          {
             for ( int i = 0; i<101;i++)
             putpixel (snake.tail_x-i,snake.tail_y,15); 
             snake.tail_x -=100;
             //printf ("RIGHT");
          } 
   

                  foodcount--;
                  score+=10;
                  settextstyle(3,0,2);
                  sprintf(scorestring,"score : %d ",score);
                  outtextxy(1,0,scorestring);
     }
}

//start function bouns
/*void bouns()
{
     int difrencex = 620 - 20;
     int difrencey = 460 - 40;
     int salma = 0;
     int bounsx;
     int bounsy;
     time_t t;
     srand((unsigned) time(&t));
     for(int i=0;i<salma;i++)
     rand();
     bounsx = (rand() % difrencex);
     bounsy = (rand() % difrencey);
     int j = 20;
     setcolor(LIGHTBLUE);
     circle(bounsx+j,bounsy+j,1);
     salma++;
}*/

//Game Over
void GameOver()
{
     if(snake.head_x <= 10 || snake.head_x >= 630 || snake.head_y <= 30 || snake.head_y >= 470)
     {
                     settextstyle(3,0,2);
                     outtextxy(290,220,"Game Over");
                     delay(3000);
                     exit(1);
     }
     int nextx;
     int nexty;
     nextx = snake.head_x;
     nexty = snake.head_y;
     if(snake.head_dir == RIGHT)
     {
                       nextx++;
     }
     if(snake.head_dir == LEFT)
     {
                       nextx--;
     }
     if(snake.head_dir == UP)
     {
                       nexty--;
     }
     if(snake.head_dir == DOWN)
     {
                       nexty++;
     }
     int nextpixel;
     nextpixel = getpixel(nextx,nexty);
     if(nextpixel == LIGHTGREEN)
     {
                 settextstyle(3,0,2);
                 outtextxy(290,220,"Game Over");
                 delay(3000);
                 exit(1);
     }
}

//start function Game
void Game()
{
     food();
     //bouns();
     while(1)
     {
             move();
             GameOver();
     }
}
|
cyberpirate (33)
I would suggest making a variable for the length of the snake. then U can just add to it.
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us