Let's go to the last figure Wow:
Figure 1, first pass view
Figure 2, Second pass view
Game Description:
(1). Only 20 red fruits can be cleared for each clearance
(2). Add new scene, appear in the second pass
(3). Increase the ready state of just-opened program, start by Space
(4). Increase the pause function, suspend the space by pressing the blanks in the program running, and continue the game by space.
(5). As long as you do not close the application, you will retain the number, that is, in the X-turn and the beginning of the X-turn
(6). Increase speed every two times, and the scene repeats itself.
In fact, I have done two scenes, but countless close (speed ~ hehe), because two scenes have been done, and then add the scene is only physical life ha, do not go to do. This is completely cross-platform, win under the figure I will not cut, the following is the code:
Main.cpp
#include <QApplication>
#include "paint.h"
int main (int argc,char**argv)
{
qapplication app ( ARGC,ARGV);
Paint *sb=new paint;
Sb->show ();
return app.exec ();
}
Paint.h
#ifndef paint_h
#define PAINT_H
#include <QWidget>
#include <QPaintEvent>
#include < qkeyevent>
#include <QTimer>
#include <QProgressBar>
enum Direct
{
up,
Down, left
,
right
};
Enum result
{
Win,
lose,
Normal,
Ready
};
Class Paint:public Qwidget
{
q_object public
:
paint (qwidget*parent=0);
~paint ();
void Init ();
int Getrandnum ();
Protected:
void PaintEvent (qpaintevent*);
void Keypressevent (Qkeyevent *keyevent);
Public slots://protected better??
void Autorun ();
Private:
int snake[100][2];
int food[2];
int length;
result Res;
Direct dir;
Qtimer *timer;
int count;
int level;
int speed;
int Randsuanzi;
int Jiou;
Qprogressbar *progress;
};
#endif
Paint.cpp
#include "paint.h" #include <QtGui> #define Countrange Paint::p aint (qwidget*parent): Qwidget (parent) {level=1;
speed=200;
Init ();
Setwindowtitle ("Little Snake v0.2");
Timer=new Qtimer;
Timer->start (speed);
Connect (timer,signal (timeout ()), This,slot (Autorun ()));
Setfixedsize (360,410);
Progress=new Qprogressbar;
Progress->setrange (0,countrange);
Progress->setvalue (count);
Progress->setorientation (qt::horizontal);
Qvboxlayout *main=new qvboxlayout;
Main->addstretch (2);
Main->addwidget (progress);
SetLayout (main);
} paint::~paint () {} void Paint::init () {count=0;
length=4;
randsuanzi=0;
jiou=0;
Dir=up;
Res=ready;
snake[0][0]=20*1;
snake[0][1]=20*13;
snake[1][0]=20*1;
snake[1][1]=20*14;
snake[2][0]=20*1;
snake[2][1]=20*15;
snake[3][0]=20*1;
snake[3][1]=20*16;
Food[0]= (Getrandnum () +1) *20;
Food[1]= (Getrandnum () +1) *20; for (int i=0;i<length;++i) {if (level%2) {if (food[0]-snake[i][0]==0) && (food[1]-snake[i][1]==0)) {food[0]= (Getrandnum () +1) *20;
Food[1]= (Getrandnum () +1) *20;
I=-1;
Continue } else {if ((Food[0]==snake[i][0]) && (food[1]==snake[i][1)) | | ((food[0]==5*20) && (food[1]>=7*20) && (food[1]<=10*20) | | ((food[0]==12*20) && (food[1]>=7*20) && (food[1]<=10*20) | | ((food[1]==5*20) && (food[0]>=7*20) && (food[0]<=10*20) | |
((food[1]==12*20) && (food[0]>=7*20) && (food[0]<=10*20)))
{food[0]= (Getrandnum () +1) *20;
Food[1]= (Getrandnum () +1) *20;
I=-1;
Continue
()}} int Paint::getrandnum () {randsuanzi++;
Srand (Time (0) +randsuanzi);
if (randsuanzi>9999) randsuanzi=0;
Return to Rand ()%16;//0~15} void Paint::p aintevent (qpaintevent*) {Qpainter P (this);
QRECTF border (20-20,20-20,16*20+40,16*20+40);
QRECTF Inter (20,20,16*20,16*20);
P.setpen (Qt::nopen); if (level%2) {P.setbrush (Qbrush (Qt::d arkgreEn,qt::solidpattern));
else {P.setbrush (Qbrush (Qt::d arkcyan,qt::solidpattern));
} p.drawrect (border);
P.setbrush (Qbrush (Qt::lightgray,qt::solidpattern));
P.drawrect (inter);
P.setpen (Qpen (Qt::d arkgray,1,qt::solidline,qt::roundcap,qt::roundjoin));
P.setbrush (Qbrush (Qt::green));
QRECTF snakehead (snake[0][0],snake[0][1],20,20);
P.drawroundrect (snakehead);
QRECTF Snakefood (food[0],food[1],20,20);
P.setbrush (Qbrush (Qt::d arkred));
P.drawellipse (Snakefood);
for (int i=1;i<length;++i) {QRECTF snakebody (snake[i][0],snake[i][1],20,20);
P.setbrush (Qbrush (qt::red));
P.drawroundrect (Snakebody);
} QRECTF Levelinfo (12*20,20,80,20);
if (level%2) {P.setpen (Qpen (Qt::d arkgreen));
else {P.setpen (Qpen (Qt::d Arkcyan));
} QString Levelnum=qstring::number (level,10);
QString levelstr= "Level";
Levelstr.append (Levelnum);
P.drawtext (LEVELINFO,QT::ALIGNRIGHT,LEVELSTR);
QRECTF Xiaolei (9*20,0,8*20,20); P.setpen (Qpen (Qt::lightgray));
P.drawtext (Xiaolei,qt::alignright, "made by Kanglei"); if (!) (
level%2)) {QRECTF block1 (5*20,7*20,20,4*20);
P.setbrush (Qbrush (Qt::d Arkcyan));
P.drawrect (BLOCK1);
QRECTF Block2 (7*20,5*20,4*20,20);
P.setbrush (Qbrush (Qt::d Arkcyan));
P.drawrect (BLOCK2);
QRECTF Block3 (12*20,7*20,20,4*20);
P.setbrush (Qbrush (Qt::d Arkcyan));
P.drawrect (BLOCK3);
QRECTF Block4 (7*20,12*20,4*20,20);
P.setbrush (Qbrush (Qt::d Arkcyan));
P.drawrect (BLOCK4);
QRECTF block_add1 (0,8*20,20,40);
P.setbrush (Qbrush (Qt::lightgray));
P.drawrect (BLOCK_ADD1);
QRECTF block_add2 (17*20,8*20,20,40);
P.setbrush (Qbrush (Qt::lightgray));
P.drawrect (BLOCK_ADD2);
QRECTF block_add3 (8*20,0,40,20);
P.setbrush (Qbrush (Qt::lightgray));
P.drawrect (BLOCK_ADD3);
QRECTF block_add4 (8*20,17*20,40,20);
P.setbrush (Qbrush (Qt::lightgray));
P.drawrect (BLOCK_ADD4);
} if (Res==ready) {P.setfont (Qfont ("", 16,16));
P.setpen (Qpen (qt::yellow,60));
QRECTF text (50,50,16*20-50,16*20-50); P.drawtext (tExt,qt::aligncenter, "ready?\npress space Start");
} if (Res==lose) {P.setfont (Qfont ("", 16,16));
P.setpen (Qpen (qt::yellow,60));
QRECTF text (50,50,16*20-50,16*20-50); P.drawtext (Text,qt::aligncenter, "lose!!
\npress space Restart ");
} if (Res==win) {P.setfont (Qfont ("", 16,16));
P.setpen (Qpen (qt::yellow,60));
QRECTF text (50,50,16*20-50,16*20-50);
P.drawtext (Text,qt::aligncenter, "win!!");
} if ((Res==normal) && (Jiou)) {P.setfont (Qfont ("", 16,16));
P.setpen (Qpen (qt::yellow,60));
QRECTF text (50,50,16*20-50,16*20-50);
P.drawtext (Text,qt::aligncenter, "Pause");
} void Paint::keypressevent (qkeyevent* keyevent) {switch (Keyevent->key ()) {case qt::key_up:dir=up;
Break
Case Qt::key_down:dir=down;
Break
Case Qt::key_left:dir=left;
Break
Case Qt::key_right:dir=right;
Break
Case Qt::key_space:if (Res==ready)//start {res=normal;
Break
} if (Res==lose)//restart {init (); progress-≫setvalue (count);
Update ();
Break
} if (Res==normal)//pause {if (!jiou) {timer->stop ();
Jiou=1;
else {timer->start ();
jiou=0;
Update ();
Break
}} void Paint::autorun () {if (res==normal) {int temp[2];
TEMP[0]=SNAKE[LENGTH-1][0];
TEMP[1]=SNAKE[LENGTH-1][1];
for (int i=length-1;i>0;--i) {snake[i][0]=snake[i-1][0];
SNAKE[I][1]=SNAKE[I-1][1];
} if (dir==up) {snake[0][1]-=20;
if (Snake[0][1]==snake[2][1]) {snake[0][1]+=40; } if (snake[0][1]<20| |
SNAKE[0][1]>16*20) {if (level%2)//1,3,5 ...
Res=lose; else {if ((snake[0][0]==8*20) | |
(SNAKE[0][0]==9*20))
&& (snake[0][1]==0)) {snake[0][1]=16*20; else if ((snake[0][0]==8*20) | |
(SNAKE[0][0]==9*20))
&& (snake[0][1]==17*20)) {snake[0][1]=20;
else Res=lose;
}} if (Dir==down) {snake[0][1]+=20; if (Snake[0][1]==snaKe[2][1]) {snake[0][1]-=40; } if (snake[0][1]<20| |
SNAKE[0][1]>16*20) {if (level%2) res=lose; else {if ((snake[0][0]==8*20) | |
(SNAKE[0][0]==9*20))
&& (snake[0][1]==0)) {snake[0][1]=16*20; else if ((snake[0][0]==8*20) | |
(SNAKE[0][0]==9*20))
&& (snake[0][1]==17*20)) {snake[0][1]=20;
else Res=lose;
}} if (Dir==left) {snake[0][0]-=20;
if (Snake[0][0]==snake[2][0]) {snake[0][0]+=40; } if (snake[0][0]<20| |
SNAKE[0][0]>16*20) {if (level%2) res=lose; else {if ((snake[0][1]==8*20) | |
(SNAKE[0][1]==9*20))
&& (snake[0][0]==0)) {snake[0][0]=16*20; else if ((snake[0][1]==8*20) | |
(SNAKE[0][1]==9*20))
&& (snake[0][0]==17*20)) {snake[0][0]=20;
else Res=lose;
}} if (Dir==right) {snake[0][0]+=20; if (Snake[0][0]==snake[2][0]) {snake[0][0]-=40; } if (snake[0][0]<20| |
SNAKE[0][0]>16*20) {if (level%2) res=lose; else {if ((snake[0][1]==8*20) | |
(SNAKE[0][1]==9*20))
&& (snake[0][0]==0)) {snake[0][0]=16*20; else if ((snake[0][1]==8*20) | |
(SNAKE[0][1]==9*20))
&& (snake[0][0]==17*20)) {snake[0][0]=20;
else Res=lose;
{if (snake[0][0]-snake[i][0]==0) \ && (snake[0][1]-snake[i][1]==0)) for (int I=1;i<length;++i)}}}
{res=lose; } if (! (
level%2))//2,4,6 ....
{if ((snake[0][0]==5*20) && (snake[0][1]>=7*20) && (snake[0][1]<=10*20)) {res=lose;
} if ((snake[0][0]==12*20) && (snake[0][1]>=7*20) && (snake[0][1]<=10*20)) {res=lose;
} if ((snake[0][1]==5*20) && (snake[0][0]>=7*20) && (snake[0][0]<=10*20)) {res=lose;
} if ((snake[0][1]==12*20) && (snake[0][0]>=7*20) && (snake[0][0]<=10*20)) {res=lose;
} if ((Snake[0][0]==food[0]) && (snake[0][1]==food[1])) {count++;
Progress->setvalue (count);
length++;
SNAKE[LENGTH-1][0]=TEMP[0];
SNAKE[LENGTH-1][1]=TEMP[1];
Food[0]= (Getrandnum () +1) *20;
Food[1]= (Getrandnum () +1) *20; for (int i=0;i<length;++i) {if (level%2) {if (food[0]-snake[i][0]==0) \ && (food[1]-snake[i][1)
==0)) {food[0]= (Getrandnum () +1) *20;
Food[1]= (Getrandnum () +1) *20;
I=-1;
Continue } else {if ((Food[0]==snake[i][0]) && (food[1]==snake[i][1)) | | ((food[0]==5*20) && (food[1]>=7*20) && (food[1]<=10*20) | | ((food[0]==12*20) && (food[1]>=7*20) && (food[1]<=10*20) | | ((food[1]==5*20) && (food[0]>=7*20) && (food[0]<=10*20) | |
((food[1]==12*20) && (food[0]>=7*20) && (food[0]<=10*20)))
{food[0]= (Getrandnum () +1) *20; FOod[1]= (Getrandnum () +1) *20;
I=-1;
Continue
}} if (Count>=countrange) {level++;
if (level%2) speed=speed-50;
if (speed<=0) speed=20;
Timer->start (speed);
Init ();
Progress->setvalue (count);
} update (); }
}
The code is very simple, I think the writing is also very organized, should be very clear ha ~ with Qt Creator really can be developed quickly, but with Visual Studio like, there is no meaning. So I adhere to vim under the code to write, but also very convenient, more than 400 lines of games, very happy ~ haha ~
Reprint Please specify: Transfer from http://blog.csdn.net/littlethunder/article/details/8916987