?? snake.cpp.bak
字號(hào):
#include <qapplication.h>
#include <qpalette.h>
#include "snake.h"
#define N 100
#define LEFT 0x1012
#define RIGHT 0x1014
#define DOWN 0x1015
#define UP 0x1013
#define ESC 0x1000
#define PAUSE 0X50
struct Food{ int x;/*食物的橫坐標(biāo)*/ int y;/*食物的縱坐標(biāo)*/ int yes;/*判斷是否要出現(xiàn)食物的變量*/}food;/*食物的結(jié)構(gòu)體*/struct Snake{ int x[N]; int y[N]; int node;/*蛇的節(jié)數(shù)*/ int direction;/*蛇移動(dòng)方向*/ int life;/* 蛇的生命,0活著,1死亡*/}snake;
SnakeGame::SnakeGame(QWidget *parent,const char *name,WFlags f):QWidget(parent,name,f)
{
SnakeTime=new QTimer(this);
QObject::connect(SnakeTime,SIGNAL(timeout()),this,SLOT(SnakeRun()));
lcd =new QLCDNumber(5,this,"");
lcd->setSegmentStyle( QLCDNumber::Flat );
lcd->setFrameStyle( QFrame::NoFrame | QFrame::Plain );
InitGame();
}
void SnakeGame::paintEvent(QPaintEvent *event)
{
QPainter paint( this );
QRect v = paint.viewport();
paint.setViewport( v.left() ,
v.top() , width(), height());
lcd->setGeometry(width()-100,5,100,30);
DrawGame(&paint);
PlayGame(&paint);
}
void SnakeGame::InitGame()
{
pause=1;stage=1,score=0;gamespeed =400;key=0;
setPaletteBackgroundColor(QColor(192,253,123));
SnakeTime->start(gamespeed,false);
food.yes=1;/*1表示需要出現(xiàn)新食物,0表示已經(jīng)存在食物*/
snake.life=0;/*活著*/
snake.direction=1;/*方向往右*/
snake.x[0]=50;snake.y[0]=100;/*蛇頭*/
snake.x[1]=40;snake.y[1]=100;
snake.node=2;/*節(jié)數(shù)*/
}
void SnakeGame::SnakeRun()
{
update();
}
void SnakeGame::GameScore()
{
lcd->display(score);
}
void SnakeGame::DrawGame(QPainter *painter)
{
painter->setPen( QPen(QColor( 41,75,35),1,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
for(i=0;i<=18;i+=2)/*畫圍墻*/
{
painter->drawRect(i,i+40,width()-2*i,height()-2*i-40);
}
}
void SnakeGame::PlayGame(QPainter *painter)
{
GameScore();/*輸出得分*/
if(food.yes==1)/*需要出現(xiàn)新食物*/
{
food.x=rand()%width()/2+20;
food.y=rand()%height()/2+20+40;
while(food.x%10!=0)/*食物隨機(jī)出現(xiàn)后必須讓食物能夠在整格內(nèi),這樣才可以讓蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*畫面上有食物了*/
}
if(food.yes==0)/*畫面上有食物了就要顯示*/
{painter->setBrush( QColor(Qt::yellow ));
painter->drawRoundRect(food.x,food.y,10,10,50,50);
}
if(snake.life==0&&pause==1)/*蛇移動(dòng)*/
{
QString str;
str = QString( "GAME STAGE : %1" )
.arg( stage);
painter->drawText(10,20,str);
for(i=snake.node-1;i>0;i--)/*蛇的每個(gè)環(huán)節(jié)往前移動(dòng),也就是貪吃蛇的關(guān)鍵算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四個(gè)方向,通過這個(gè)判斷來移動(dòng)蛇頭*/
if(key==UP&&snake.direction!=4)
/*判斷是否往相反的方向移動(dòng)*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++)/*從蛇的第四節(jié)開始判斷是否撞到自己了,因?yàn)樯哳^為兩節(jié),第三節(jié)不可能拐過來*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
snake.life=1;
break;
}
}
if(snake.x[0]<30||snake.x[0]>width()-40||snake.y[0]<70||
snake.y[0]>height()-40)/*蛇是否撞到墻壁*/
{
snake.life=1; /*蛇死*/
}
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/
{
painter->setBrush( QColor( 192,253,123));
painter->drawRoundRect(food.x,food.y,10,10,50,50); /*把畫面上的食物東西去掉*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一節(jié)先放在看不見的位置,下次循環(huán)就取前一節(jié)的位置*/
snake.node++;/*蛇的身體長一節(jié)*/
food.yes=1;/*畫面上需要出現(xiàn)新的食物*/
score+=10;
GameScore();/*輸出新得分*/
if(snake.node>10*stage+1&&stage<6)
{
stage+=1;
gamespeed=gamespeed-100;
pause=0;
}/*關(guān)卡速度變快,游戲暫停*/
}
}
else
{
SnakeTime->stop();
if(pause==1)painter->drawText(10,20,"GAME OVER!");
else painter->drawText(10,20,"GAME PAUSE!");
}/*游戲結(jié)束、暫停*/
painter->setBrush( QColor( Qt::red));painter->drawRoundRect(snake.x[0],snake.y[0],10,
10,50,50);painter->setBrush( QColor( Qt::blue));
for(i=1;i<snake.node;i++)
painter->drawRoundRect(snake.x[i],snake.y[i],10,
10,50,50);/*畫出蛇*/
painter->setPen( QPen(QColor( QColor(192,253,123)),1,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));painter->setBrush( QColor( 192,253,123));
painter->drawRoundRect(snake.x[snake.node-1],snake.y[snake.node-1],
10,10,50,50);/*去除蛇的的最后一節(jié)*/
}
void SnakeGame::keyPressEvent(QKeyEvent *k)
{
key= k->key();
if(key==ESC){SnakeTime->stop();InitGame();}
if(key==PAUSE)
{
if(SnakeTime->isActive())
{//SnakeTime->stop();
pause=0;}
else
{
SnakeTime->start(gamespeed,false);
pause=1;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -