473,507 Members | 12,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help in snake game

1 New Member
Hi. I am beginner and I am making a snake game in C... i am having trouble in moving the snake automatically. that is after every one move, the program stops for further directions. i have seen a lot of questions online regarding my problem but i am unable to understand the answers ppl have posted since I am only a beginner... in ur replies kindly also mention the technique u r using(for xample threads) so that if i havent studied it i can focus on that technique only....
here is my code

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
#include <time.h>

void GotoXY( HANDLE StdOut, SHORT x, SHORT y )
{
COORD Cord;
Cord.X = x;
Cord.Y = y;
SetConsoleCursorPosition( StdOut, Cord );
}



int main ()
{
printf("You have 3 SECONDS to catch the *");
getch();
system("cls");

HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE );

int cursorx=5, cursory=5, temp;
int score=0;

while (1)
{

HANDLE dot=GetStdHandle( STD_OUTPUT_HANDLE );
srand( time( NULL ) );
int dotx = 10+rand() % 40;
int doty = 2+rand()%25;
int playagain= 0;
COORD Cord;
Cord.X = dotx;
Cord.Y = doty;

SetConsoleCursorPosition(dot, Cord );
printf("*");

time_t start=time(NULL);

while(!(dotx==cursorx && doty==cursory))
{
system("cls");
printf("Your score is: %d", score);

SetConsoleCursorPosition(dot, Cord );
printf("*");


GotoXY(hStdout,cursorx,cursory);

printf("O");

{
temp= getch();
if (temp=='d')
cursorx++;


if (temp=='a')
cursorx--;


if (temp=='w')
cursory--;


if (temp=='s')
cursory++;
}
time_t end=time(NULL);

if( (end - start) > 3)
{
system("cls");
printf("TIME IS UP\n\n");
printf("your score is : %d\n", score);
score=-1;
printf("press 1 and Enter to play again\n");

while(playagain!=1)
scanf("%d", &playagain);
break;

}
}

system("cls");
score++;
printf("\a\a");
}
}
Apr 21 '12 #1
1 2428
johny10151981
1,059 Top Contributor
i guess(seeing conio.h, you probably using TC3.0) you wont get support of thread function, you better look at kbhit funtion. follow the link
Apr 21 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1548
by: Claude Vernier | last post by:
Hello, I'm a C# programmer. I have two projects. First, write an action scrolling game in C#, (portable to Windows Mobile eventually...), This game will look like The Adventure of Link from...
15
4449
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
1
4086
by: yourun | last post by:
hi frens i have to submit my project work on this topi please help me
6
1430
by: Thily | last post by:
hi, i'm doing c++ and i need to create a game for my final project. i have no idea where to start ,so i need some help,mostly example and tutorials that can help me tx
19
4379
by: foolsmart2005 | last post by:
I have written a snake game. There are 2 levels in the game(I finished 1st level). It can run in VC++ without problem but, when I run it on the dev C++ 4.9.9.2, it cannot run. I want to...
7
2363
by: Benjamin Vigneaux | last post by:
Well, I'm very interested in game development, I'm just starting out though, browsing here and there for tutorials, references, etc.. and learning about the game development industry... What...
5
4176
by: av3rage | last post by:
I have never done any programming in my life but I have decided to go into engineering and in doing so we have to take this intro to programming course and I am pretty clueless. I am starting to get...
2
2813
by: gwbob | last post by:
ok so im working on a project in my independent study in computer programming and i made a snake game what moves with the arrows i know the code to get it to move but it doesent, the arrow buttons...
1
1227
by: bravesjohn | last post by:
I am an AP computer science student and am in the process of making a modified version of the game Stratego as a project for my class. What I am trying to do is find a good way to place the pieces...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.