代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
贪吃蛇游戏
作者:
cool609
/ 发布于
2013/3/18
/
533
http://www.oschina.net/code/snippet_438292_19299
?/// @brief /// 各种定义。 #include <list> using namespace std; #define SNAKE_FALSE_BREAK(_bSuccess) \ { \ if (!_bSuccess)\ {\ break;\ }\ } #define SnakeSafeDeleteObject(_object)\ {\ if (_object)\ {\ delete _object;\ }\ } #define SnakeSafeDeleteArray(_array)\ {\ if (_array)\ {\ delete []_array;\ }\ } #define SnakeSafeDeleteLst(_list)\ {\ for (SnakeList::iterator _iter = _list.begin(); _iter != _list.end(); _iter++)\ {\ delete (*_iter);\ }\ _list.clear();\ } /// @brief /// 蛇的每一节身体的坐标结构体。 struct SSnakePoint { int nX; ///< X坐标。 int nY; ///< Y坐标。 SSnakePoint(int nX = 0, int nY = 0) { this->nX = nX; this->nY = nY; } void SetPoint(int nX, int nY) { this->nX = nX; this->nY = nY; } }; typedef struct SSnakePoint SSnakePoint; typedef list<SSnakePoint*> SnakeList; Snake.h ?#pragma once #include "SnakeHead.h" /// @brief /// 贪吃蛇类。 class Snake { public: /// @name 构造/析构 函数。 /// @{ Snake(); ~Snake(void); /// @} /// @name 自定义的公有成员函数。 /// @{ /// 初始化。 /// @return 初始化成功返回TRUE。 bool Initialize(int nWidth, int nHeight); /// @brief /// 设置蛇的活动场地大小。 /// <param name = "nWidth">[in] 宽,蛇身宽度为单位长度。</parma> /// <param name = "nHeight">[in] 高,蛇身宽度为单位长度。</parma> void SetSnakeSpace(int nWidth, int nHeight); /// @brief /// 设置蛇是否已死亡。 /// <param name = "bDead">[in] 是否死亡。</param> void SetSnakeDead(bool bDead); /// @brief /// 蛇是否死亡。 /// @return 死亡为TRUE,否则为FALSE。 bool IsDead(); /// @brief /// 蛇的位置。 /// <param name = "nX">[in] 蛇的X位置。</param> /// <param name = "nY">[in] 蛇的Y坐标。</param> /// @return 设置错误返回FALSE,否则为TRUE。 bool SetSnakePos(int nX, int nY); /// @brief /// 获取蛇的位置。 /// <param name = "nCount">[in] 第几节身体的坐标。</param> /// <param name = "nX">[in] 蛇的X位置。</param> /// <param name = "nY">[in] 蛇的Y坐标。</param> /// @return 获取错误返回FALSE,否则为TRUE。 bool GetSnakePos(int nCount, int& nX, int& nY); /// @brief /// 获取蛇的长度。 /// @return 返回蛇的长度。 int GetSnakeSize(); /// @brief /// 设置食物的位置。 /// <param name = "nX">[in] 食物的X位置。</param> /// <param name = "nY">[in] 食物的Y坐标。</param> /// @return 设置错误返回FALSE,否则为TRUE。 bool SetFoodPos(); /// @brief /// 获取食物的位置。 /// <param name = "nX">[in] 食物的X位置。</param> /// <param name = "nY">[in] 食物的Y坐标。</param> /// @return 设置错误返回FALSE,否则为TRUE。 void GetFoodPos(int& nX, int& nY); /// @brief /// 获取分数。 int GetScore(); /// @} protected: /// @name 自定义的成员函数。 /// @{ /// @brief /// 蛇身行走。 bool _SnakeRun(int nX, int nY); /// @brief /// 增加长度。 void _AddSnake(int nX, int nY); /// @brief /// 是否吃到食物。 /// @return 吃到食物返回TRUE。否则为FALSE。 bool _IsEatFood(); /// @brief /// 是否碰到自己的身体。 bool _IsStrikeMySelf(); /// @brief /// 是否碰撞到墙壁。 bool _IsStrikeWall(); /// @} protected: /// @name 自定义的成员变量。 /// @{ SnakeList m_lstSnake; ///< 蛇。 SSnakePoint m_sFoodPoint; ///< 食物的坐标点。 bool m_bDead; ///< 是否活着。 int m_nScreenWidth; ///< 场景宽。 int m_nScreenHeight;///< 场景高。 int m_nScore; ///< 吃掉的方块个数。 /// @} }; Snake.cpp #include "Snake.h" #include <time.h> #include <stdlib.h> Snake::Snake() : m_bDead(false) , m_nScreenWidth(0) , m_nScreenHeight(0) , m_nScore(0) { } Snake::~Snake(void) { SnakeSafeDeleteLst(m_lstSnake); } bool Snake::Initialize(int nWidth, int nHeight) { bool bRet = false; SSnakePoint* pPoint = NULL; do { SnakeSafeDeleteLst(m_lstSnake); m_nScreenWidth = nWidth; m_nScreenHeight = nHeight; for (int n = 5; n > 0; n--) { pPoint = new SSnakePoint; pPoint->SetPoint(n, 0); m_lstSnake.push_back(pPoint); } srand((int)time(0)); SetFoodPos(); m_bDead = false; bRet = true; } while (false); return bRet; } void Snake::SetSnakeSpace(int nWidth, int nHeight) { m_nScreenWidth = nWidth; m_nScreenHeight = nHeight; } void Snake::SetSnakeDead(bool bDead) { m_bDead = bDead; } bool Snake::IsDead() { return m_bDead; } bool Snake::SetSnakePos(int nX, int nY) { bool bSuccess = false; SSnakePoint* sPoint = NULL; do { if (_IsStrikeMySelf() || _IsStrikeWall()) { SetSnakeDead(true); } SNAKE_FALSE_BREAK(!IsDead()); if (_IsEatFood()) { _AddSnake(nX, nY); SetFoodPos(); } _SnakeRun(nX, nY); bSuccess = true; } while (false); return bSuccess; } bool Snake::GetSnakePos(int nCount, int& nX, int& nY) { bool bSuccess = false; do { SNAKE_FALSE_BREAK((nCount < m_lstSnake.size())); int n = 0; for (SnakeList::iterator iter = m_lstSnake.begin(); iter != m_lstSnake.end(); iter++,n++) { if (n == nCount) { nX = (*iter)->nX; nY = (*iter)->nY; break; } } bSuccess = true; } while (false); return bSuccess; } int Snake::GetSnakeSize() { return m_lstSnake.size(); } bool Snake::SetFoodPos() { bool bSuccess = false; bool bBreak = false; int nX = 0; int nY = 0; do { SNAKE_FALSE_BREAK(!IsDead()); while(1) { bBreak = false; nX = rand() % m_nScreenWidth; nY = rand() % m_nScreenHeight; for (SnakeList::iterator iter = m_lstSnake.begin(); iter != m_lstSnake.end(); iter++) { if ((*iter)->nX == nX && (*iter)->nY == nY) { bBreak = true; break; } } if (!bBreak) { break; } } m_sFoodPoint.SetPoint(nX, nY); bSuccess = true; } while (false); return bSuccess; } void Snake::GetFoodPos(int& nX, int& nY) { do { nX = m_sFoodPoint.nX; nY = m_sFoodPoint.nY; } while (false); } int Snake::GetScore() { return m_nScore; } bool Snake::_SnakeRun(int nX, int nY) { bool bSuccess = false; SSnakePoint sTmp; SSnakePoint sSwap; do { for (SnakeList::iterator iter = m_lstSnake.begin(); iter != m_lstSnake.end(); iter++) { sTmp.SetPoint((*iter)->nX, (*iter)->nY); if (iter == m_lstSnake.begin()) { (*iter)->SetPoint(nX, nY); } else { (*iter)->SetPoint(sSwap.nX, sSwap.nY); } sSwap.SetPoint(sTmp.nX, sTmp.nY); } } while (false); return bSuccess; } void Snake::_AddSnake(int nX, int nY) { SSnakePoint* pPoint = new SSnakePoint(nX, nY); m_lstSnake.push_back(pPoint); } bool Snake::_IsEatFood() { bool bRet = false; SSnakePoint sSnakePos; SSnakePoint sFoodPos; do { GetFoodPos(sFoodPos.nX, sFoodPos.nY); GetSnakePos(0, sSnakePos.nX, sSnakePos.nY); if (sFoodPos.nX == sSnakePos.nX && sFoodPos.nY == sSnakePos.nY) { bRet = true; m_nScore++; } } while (false); return bRet; } bool Snake::_IsStrikeMySelf() { bool bSuccess = false; SnakeList::iterator iter = m_lstSnake.begin(); int nX = (*iter)->nX; int nY = (*iter)->nY; do { for (iter++; iter != m_lstSnake.end(); iter++) { if ((*iter)->nX == nX && (*iter)->nY == nY) { bSuccess = true; break; } } } while (false); return bSuccess; } bool Snake::_IsStrikeWall() { bool bSuccess = false; do { SSnakePoint* pPoint = NULL; pPoint = m_lstSnake.front(); if (pPoint->nX > (m_nScreenWidth-1) || pPoint->nY > (m_nScreenHeight-1) || pPoint->nX < 0 || pPoint->nY < 0) { bSuccess = true; } }while(false); return bSuccess; } qsnake.h ?#ifndef QSNAKE_H #define QSNAKE_H #include <QWidget> #include "Snake.h" //#include "ui_QSnake.h" enum ESnakeDriect { ESnakeDriect_Up = 1, ESnakeDriect_Left, ESnakeDriect_Down, ESnakeDriect_Right, }; class QtSnake : public QWidget { Q_OBJECT signals: /// @brief /// 发送分数信号。 void SignalScore(); /// @brief /// 蛇死亡。 void SignalSnakeDead(); public: /// @brief /// 贪吃蛇面板。 /// <param name = "nHorNum">[in] 横行。</param> /// <param name = "nVerNum">[in] 竖行。</param> /// <param name = "parent">[in] 所属父类。</param> QtSnake(int nHorNum, int nVerNum, QWidget *parent = 0); ~QtSnake(); void StartGame(); void ReStartGame(); void PauseGame(); void SnakeRun(); /// @brief /// 设置难度等级。 /// <param name = "nLevel">[in] 等级。</param> void SetLevel(int nLevel); int GetScore(); protected: virtual void keyPressEvent(QKeyEvent* pEvent); virtual void paintEvent(QPaintEvent* pEvent); virtual void timerEvent(QTimerEvent* pEvent); void _DrawSnake(QPainter* pPainter); void _DrawFood(QPainter* pPainter); void _DrawGameOver(QPainter* pPainter); private: Snake m_oSnake; ///< 贪吃蛇。 ESnakeDriect m_eDriect; ///< 方向。 int m_nTimerId; ///< TimerId。 bool m_bPause; ///< 是否暂停了游戏。 int m_nHorNum; ///< 横向方块数。 int m_nVerNum; ///< 纵向方块数。 int m_nLevel; ///< 难度等级。 //Ui::QtSnakeClass ui; }; #endif // QSNAKE_H
试试其它关键字
贪吃蛇
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
cool609
贡献的其它代码
(
2
)
.
输出简单话System.out.print
.
贪吃蛇游戏
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3