snake game s.o.s
soka2oo7 (10)Jan 6, 2009 at 1:05am UTC
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();
}
}
Zaita (1559)Jan 6, 2009 at 1:05am UTC
We need the code for your move() function. This is the function that is being called repeatedly so has your game logic in it.
soka2oo7 (10)Jan 6, 2009 at 1:05am UTC
here is the function1 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
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<dos.h>
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
class snake
{
public :
int length;
int newheadx,newheady;
int head_x;
int head_y;
int head_dir;
int tail_x;
int tail_y;
int tail_dir;
int bend_x[1000];
int bend_y[1000];
int bend_dir[1000];
void GameDate();
};
snake snake;
int score;
char scorestring[100];
//screen function
void screen()
{
score = 0;
setcolor(LIGHTRED);
line(10,30,10,470);
line(10,30,630,30);
line(630,30,630,470);
line(10,470,630,470);
settextstyle(3,0,2);
char Theta2[]=" High score : " ;
sprintf(scorestring,"score : %d" ,score);
outtextxy(1,0,scorestring);
outtextxy(300,0,Theta2);
}
//start function game Date
void GameDate()
{
snake.length = 50;
snake.head_x = 320;
snake.head_y = 240;
snake.head_dir = RIGHT;
snake.tail_x = snake.head_x - snake.length;
snake.tail_y = snake.head_y;
snake.tail_dir = snake.head_dir;
setcolor(LIGHTGREEN);
line(snake.tail_x,snake.tail_y,snake.head_x,snake.head_y);
}
//start funcyion movement
void move()
{
static int i = 0;
static int j = 0;
if (i > 100)
i = 0;
if (j > 100)
j = 0;
char arrow;
if (kbhit)
{
arrow = getch();
if (arrow == 77)
arrow = RIGHT;
if (arrow == 75)
arrow = LEFT;
if (arrow == 72)
arrow = UP;
if (arrow == 80)
arrow = DOWN;
if (arrow == RIGHT && snake.head_dir != LEFT && snake.head_dir != RIGHT)
{
snake.head_dir = RIGHT;
snake.newheadx= snake.bend_x[i] = snake.head_x;
snake.newheady= snake.bend_y[i] = snake.head_y;
snake.bend_dir[i] = RIGHT;
i++;
}
if (arrow == LEFT && snake.head_dir != RIGHT && snake.head_dir != LEFT)
{
snake.head_dir = LEFT;
snake.newheadx=snake.bend_x[i] = snake.head_x;
snake.newheady=snake.bend_y[i] = snake.head_y;
snake.bend_dir[i] = LEFT;
i++;
}
if (arrow == UP && snake.head_dir != DOWN && snake.head_dir != UP)
{
snake.head_dir = UP;
snake.newheadx= snake.bend_x[i] = snake.head_x;
snake.newheady= snake.bend_y[i] = snake.head_y;
snake.bend_dir[i] = UP;
i++;
}
if (arrow == DOWN && snake.head_dir != UP && snake.head_dir != DOWN)
{
snake.head_dir = DOWN;
snake.newheadx = snake.bend_x[i] = snake.head_x;
snake.newheady = snake.bend_y[i] = snake.head_y;
snake.bend_dir[i] = DOWN;
i++;
}
if (snake.tail_x == snake.bend_x[j] && snake.tail_y == snake.bend_y[j])
{
snake.tail_dir = snake.bend_dir[j];
j++;
}
}
if (snake.head_dir == LEFT)
{
snake.newheadx = snake.head_x--;
}
if (snake.head_dir == RIGHT)
{
snake.newheadx= snake.head_x++;
}
if (snake.head_dir == DOWN)
{
snake.newheady = snake.head_y++;
}
if (snake.head_dir == UP)
{
snake.newheady = snake.head_y--;
}
putpixel(snake.head_x,snake.head_y,LIGHTGREEN);
putpixel(snake.tail_x,snake.tail_y,0);
delay(50);
if (snake.tail_dir == LEFT)
{
snake.tail_x--;
}
if (snake.tail_dir == RIGHT)
{
snake.tail_x++;
}
if (snake.tail_dir == DOWN)
{
snake.tail_y++;
}
if (snake.tail_dir == UP)
{
snake.tail_y--;
}
}
int foodx;
int foody;
This topic is archived - New replies not allowed.