Jump to content
  • Advertisement

CreativeMFS

Member
  • Content Count

    9
  • Joined

  • Last visited

Community Reputation

103 Neutral

About CreativeMFS

  • Rank
    Newbie
  1. CreativeMFS

    Snake - check it out

    Added 7 designed levels to modern mode!
  2. CreativeMFS

    Snake - check it out

    Thank you, and I definitely would like to add 'designed' levels. I just finished it and was not in the mood to either manually fill the grid or write the loops. but I will.
  3. CreativeMFS

    Snake - check it out

    Thanks guys, I really appreciate the positive feedback!
  4. CreativeMFS

    Snake - check it out

  5. CreativeMFS

    Snake - check it out

    Check it out guys, my first game. Snake - for windows. I think it is rather well done. I do not use any 3rd party libs, just my own code and Win API functions. The link is to the .exe and .zip(source). http://www.mediafire.com/?db959tciirdd0
  6. CreativeMFS

    WinAPI and DirectX tutorials ?

    I agree with Aluthreney, the basics are free but then you have to pay, but you learn both windows and DX.
  7. CreativeMFS

    Feedback on my first game - Snake

    w s a d - up down left right p - pause escape - end game
  8. CreativeMFS

    Feedback on my first game - Snake

    The only bug I have seen so far is that sometimes there will be one piece of food left on the board but the level will advance. Can anyone figure it out? I believe my food generation algorithm works well (snakengine.h line 81 - 103)
  9. Hi guys, I made this game as a challenge to myself and just for the fun of it. If any more experienced people can give some constructive criticism that would be much appreciated. Sorry there are no comments or documentation yet, I will be adding some later. The game plays well, but if there is a more efficient way to do something I would like to hear it. That said, I do not want to rewrite the whole thing. snakemain.cpp ////////////////////////////////////////////////////////////////////////////////////////////////////////// //Michael Ervin - Windows based graphical snake game - 7/29/2011 ////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <Windows.h> //#include "resource.h" #include "snakengine.h" ////////////////// game _game; ////////////////// LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEXA wc; ZeroMemory(&wc, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = MessageProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); //wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1)); wc.lpszClassName = "SNAKE"; RegisterClassExA(&wc); HWND hWnd = CreateWindowExA(NULL, "SNAKE", "Snake", WS_SYSMENU | WS_BORDER, 0, 45, screen_width, screen_height, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); HDC hDC = GetDC(hWnd); MSG msg; _game.game_setstate('i'); _game.game_getvars(hDC, hWnd); while(TRUE) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { _game.game_state(); Sleep(75); } } return msg.wParam; } LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_DESTROY: { PostQuitMessage(0); return 0; } break; case WM_KEYDOWN: { switch (wParam) { case _up: case _down: case _left: case _right: _game.game_input(wParam); break; case _pause: _game.game_setstate('t'); break; case _esc: _game.game_setstate('o'); break; } return 0; } break; } return DefWindowProcA(hWnd, message, wParam, lParam); } snakengine.h //Snake object header #ifndef H_snakengine #define H_snakengine #define screen_width 800 #define screen_height 600 #include <vector> #include <ctime> #include <sstream> #include <string> COLORREF _blue = RGB(0,0,255); COLORREF _red = RGB(255,0,0); COLORREF _white = RGB(255,255,255); COLORREF _black = RGB(0,0,0); COLORREF _green = RGB(0,255,0); const int _rectwidth = 10, _rectheight = 10, _bordertop = 5, _borderleft = 5, _borderbottom = screen_height - 65, _borderright = screen_width - 9; const DWORD _up = 0x57, _down = 0x53, _left = 0x41, _right = 0x44, _pause = 0x50, _yes = 0x59, _no = 0x4E, _esc = 0x1B; HBRUSH _bluebrush = CreateSolidBrush(_blue); HBRUSH _whitebrush = CreateSolidBrush(_white); HBRUSH _blackbrush = CreateSolidBrush(_black); HBRUSH _greenbrush = CreateSolidBrush(_green); std::string convstr(const int& t){std::stringstream itoa; itoa << t; return itoa.str();} std::vector<RECT> _foodgrid, _obstaclegrid; class level { public: int _numfood, _numobstacle; level(HDC _hDC, int _lvl, int _scr):hDC(_hDC), _level(_lvl), _score(_scr){} void init_level() { draw_level(); fill_foodobstaclegrid(); } void update_info(int& _score) { std::string _slevel = convstr(_level); std::string _sscore = convstr(_score); TextOut(hDC, 60, screen_height - 55, _slevel.c_str(), _slevel.length()); TextOut(hDC, 145, screen_height - 55, convstr(_score).c_str(), _sscore.length()); } void end_game() { int _width; std::string _gameover = "Game Over"; std::string _playagain = "Do you want to play again? (y / n)"; _width = (screen_width / 2) - 40; TextOut(hDC, _width, screen_height / 2, _gameover.c_str(), _gameover.length()); _width = (screen_width / 2) - 100; TextOut(hDC, _width, (screen_height / 2) + 50, _playagain.c_str(), _playagain.length()); } private: int _level, _score; const HDC hDC; void draw_level() { RECT _back = {0,0,screen_width - 6,screen_height - 60}; RECT _front = {10,10,screen_width - 10,screen_height - 70}; FillRect(hDC, &_back, _blackbrush); FillRect(hDC, &_front, _whitebrush); } void fill_foodobstaclegrid() { _numfood = _level * 4; _numobstacle = _level * 3; int _top, _left, i, j; srand(time(0)); std::vector<RECT> _used; RECT _usedone = {390, 290, 400, 300}; _used.push_back(_usedone); for(i = 0; i < _numfood; i++) { _top = 10 + (rand() % (_borderbottom - 20)); if(_top % 10 != 0) _top -= _top % 10; _left = 10 + (rand() % (_borderright - 20)); if(_left % 10 != 0) _left -= _left % 10; RECT _food = {_left, _top, _left + 10, _top + 10}; for(j = 0; j < _used.size(); j++) if(_food.top == _used[j].top && _food.left == _used[j].left) { i--; break; } else { _foodgrid.push_back(_food); _used.push_back(_food); FillRect(hDC, &_food, _greenbrush); break; } } for(i = 0; i < _numobstacle; i++) { _top = 10 + (rand() % (_borderbottom - 20)); if(_top % 10 != 0) _top -= _top % 10; _left = 10 + (rand() % (_borderright - 21)); if(_left % 10 != 0) _left -= _left % 10; RECT _obstacle = {_left, _top, _left + 10, _top + 10}; for(j = 0; j < _used.size(); j++) if(_obstacle.top == _used[j].top && _obstacle.left == _used[j].left) { i--; break; } else { _obstaclegrid.push_back(_obstacle); _used.push_back(_obstacle); FillRect(hDC, &_obstacle, _blackbrush); break; } } TextOut(hDC, 15, screen_height - 55, "Level: ", 7); TextOut(hDC, 100, screen_height - 55, "Score: ", 7); } }; class snake { int _lvl, _length, _counter; RECT _old; std::vector<RECT> _segments; public: char _direction; snake(int _level):_lvl(_level), _length(_lvl*4), _direction(NULL), _counter(0){} void init_snake() { _segments.resize(0); for(int i = 0; i < _length; i++) { RECT _newseg = {390, 290, 390 + _rectwidth, 290 + _rectheight}; _segments.push_back(_newseg); } }; void show_head(const HDC& hDC) { FillRect(hDC, &_segments[0], _bluebrush); } void next_pos() { int i; _old = _segments.back(); for(i = _length - 1; i > 0; i--) _segments = _segments[i - 1]; switch (_direction) { case _right: _segments[0].left += _rectwidth; _segments[0].right += _rectwidth; break; case _left: _segments[0].left -= _rectwidth; _segments[0].right -= _rectwidth; break; case _up: _segments[0].top -= _rectheight; _segments[0].bottom -= _rectheight; break; case _down: _segments[0].top += _rectheight; _segments[0].bottom += _rectheight; break; } } void animate_snake(const HDC& hDC) { FillRect(hDC, &_segments[0], _bluebrush); if (_counter == (4 * _lvl) - 1) FillRect(hDC, &_old, _whitebrush); else _counter++; } void add_seg() { RECT _newseg = _segments[_length - 1]; _segments.push_back(_newseg); _length++; } char collisions() { int i; char _ret = 'n'; switch (_direction) { case _up: if(_segments[0].top <= _bordertop) _ret = 'o'; break; case _down: if(_segments[0].bottom >= _borderbottom) _ret = 'o'; break; case _left: if(_segments[0].left <= _borderleft) _ret = 'o'; break; case _right: if(_segments[0].right >= _borderright) _ret = 'o'; break; } for(i = 0; i < _foodgrid.size(); i ++) if(_segments[0].top == _foodgrid.top && _segments[0].left ==_foodgrid.left) { _ret = 'f'; break; } for(i = 0; i < _obstaclegrid.size(); i ++) if(_segments[0].top == _obstaclegrid.top && _segments[0].left ==_obstaclegrid.left) { _ret = 'o'; break; } for(i = 1; i < _length; i++) if(_segments[0].top == _segments.top && _segments[0].left ==_segments.left) { _ret = 'o'; break; } return _ret; } }; class game { level* _LVL; snake* _SNK; HDC hDC; int _level, _score, _foodeaten; char _collision, _state; HWND _hWnd; public: game():_level(1), _score(0){} void game_setstate(char state) { _state = state; } void game_state() { switch (_state) { case 'i': game_init(); _state = 'p'; break; case 'r': game_reinit(); break; case 'p': game_play(); break; case 't': case 'u': game_pause(); break; case 'o': game_over(); break; } } void game_getvars(HDC _hDC, HWND hWnd) { _hWnd = hWnd; hDC = _hDC; } void game_init() { _foodeaten = 0; _LVL = new level(hDC, _level, _score); _LVL->init_level(); _LVL->update_info(_score); _SNK = new snake(_level); _SNK->init_snake(); _SNK->show_head(hDC); } void game_reinit() { _foodgrid.resize(0); _obstaclegrid.resize(0); delete _SNK; delete _LVL; _state = 'i'; } void game_input(WPARAM _direction) { switch (_direction) { case _up: _SNK->_direction = _up; break; case _down: _SNK->_direction = _down; break; case _left: _SNK->_direction = _left; break; case _right: _SNK->_direction = _right; break; } } void game_play() { if(_SNK->_direction != NULL) { _SNK->next_pos(); _SNK->animate_snake(hDC); switch (_SNK->collisions()) { case 'f': _foodeaten++; _SNK->add_seg(); _score += _level * 5; break; case 'o': _state = 'o'; break; case 'n': break; } _LVL->update_info(_score); if(_foodeaten == _LVL->_numfood) { _level++; _state = 'r'; } } } void game_pause() { static bool _paused = false; if(_state == 't') _paused = !_paused; if(!_paused) _state = 'p'; else _state = 'u'; Sleep(10); } void game_over() { _state = 'a'; _LVL->end_game(); while(true) { if(GetAsyncKeyState(_yes)) { _level = 1; _score = 0; _state = 'r'; break; } else if(GetAsyncKeyState(_no)) { PostQuitMessage(0); break; } Sleep(10); } } }; #endif
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!