Help with a snake game?

This is a discussion on Help with a snake game? within the Game Programming forums, part of the General Programming Boards category; Okay, so I am not quite making a game. I am trying to write some code that will play a ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11

    Help with a snake game?

    Okay, so I am not quite making a game. I am trying to write some code that will play a snake game for me. Yes, cheating, thats right.

    So, there is this stupid flash snake game that I play on some site. Being the curious computer science student that I am, I am trying to write a programming that will play the game.

    At this point, it works to move the snake around the grid and even manage to eat a couple of apples. But I am just doing this by trying to time everything perfectly and by starting the program at exactly the right time ...with little success.

    Heres what I have so far.
    Code:
    #include <windows.h>
    #include <iostream>
    
    
    const static double LONG_WAYS = 3937.5;
    const static double SHORT_WAYS = 1950;
    
    using namespace std;
     
    void snake()
    {
    	cout << "Go!" << endl;
    
    	for(int j = 0; j < 410; j++)
    	{
    		for(int i = 0; i < 15; i++)
    		{
    			cout << i << endl;
    
    			keybd_event(VK_DOWN,0x28,0 , 0); // Down Arrow Press
    			keybd_event(VK_DOWN,0x28,KEYEVENTF_KEYUP,0); // Down Arrow Release
    			keybd_event(VK_RIGHT,0x27,0 , 0); // Right Arrow Press
    			keybd_event(VK_RIGHT,0x27,KEYEVENTF_KEYUP,0); // Right Arrow Release
    
    			Sleep(LONG_WAYS);
    
    			keybd_event(VK_DOWN,0x28,0 , 0); // Down Arrow Press
    			keybd_event(VK_DOWN,0x28,KEYEVENTF_KEYUP,0); // Down Arrow Release
    			keybd_event(VK_LEFT,0x25,0 , 0); // Left Arrow Press
    			keybd_event(VK_LEFT,0x25,KEYEVENTF_KEYUP,0); // Left Arrow Release
    
    			Sleep(LONG_WAYS);
    		}
    
    		keybd_event(VK_DOWN,0x28,0 , 0); // Down Arrow Press
    		keybd_event(VK_DOWN,0x28,KEYEVENTF_KEYUP,0); // Down Arrow Release
    		keybd_event(VK_RIGHT,0x27,0 , 0); // Right Arrow Press
    		keybd_event(VK_RIGHT,0x27,KEYEVENTF_KEYUP,0); // Right Arrow Release
    
    		Sleep(LONG_WAYS+62.5);
    
    		keybd_event(VK_UP,0x26,0 , 0); // Up Arrow Press
    		keybd_event(VK_UP,0x26,KEYEVENTF_KEYUP,0); // Up Arrow Release
    
    		Sleep(SHORT_WAYS);
    
    		keybd_event(VK_LEFT,0x25,0 , 0); // Left Arrow Press
    		keybd_event(VK_LEFT,0x25,KEYEVENTF_KEYUP,0); // Left Arrow Release
    
    		Sleep(LONG_WAYS+62.5);
    	}
    }
    void main()
    {
    	system("PAUSE");
    
    	cout << endl << "10" << endl;
    	Sleep(1000);
    	cout << "9" << endl;
    	Sleep(1000);
    	cout << "8" << endl;
    	Sleep(1000);
    	cout << "7" << endl;
    	Sleep(1000);
    	cout << "6" << endl;
    	Sleep(1000);
    	cout << "5" << endl;
    	Sleep(1000);
    	cout << "4" << endl;
    	Sleep(1000);
    	cout << "3" << endl;
    	Sleep(1000);
    	cout << "2" << endl;
    	Sleep(1000);
    	cout << "1" << endl;
    	Sleep(1000);
    
    	snake();
    }
    At this point, I'm a little stuck.

    Does anyone have any ideas/ suggestions that might help get me going again?

    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Sleep Function (Windows)
    Remarks

    This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds. The system clock "ticks" at a constant rate. If dwMilliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on. To increase the accuracy of the sleep interval, call the timeGetDevCaps function to determine the supported minimum timer resolution and the timeBeginPeriod function to set the timer resolution to its minimum. Use caution when calling timeBeginPeriod, as frequent calls can significantly affect the system clock, system power usage, and the scheduler. If you call timeBeginPeriod, call it one time early in the application and be sure to call the timeEndPeriod function at the very end of the application.
    If the system clock is say 10ms, then anything that ends in say 7.5 is going to miss the mark.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11
    Okay thanks.

    And is there any way to make my application wait for a specific key press even if i am not focused on the output window? This would help me coordinate the start of the snake game and my application.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ snake
    By nwasiq in forum C++ Programming
    Replies: 10
    Last Post: 11-20-2010, 10:35 AM
  2. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  3. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  4. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-25-2006, 11:14 PM
  5. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 11:12 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21