网上有关贵州闲来麻将怎样容易赢(详细开挂教程)
网上有关“编写贪吃蛇程序”话题很是火热,小编也是针对编写贪吃蛇程序寻找了一些与之相关的一些信息进行分析,如果能碰巧解决你现在面临的问题,希望能够帮助到您。
您好:手机麻将有挂是真的吗这款游戏可以开挂,确实是有挂的,咨询加微信【】很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,总是好牌,而且好像能看到其他人的牌一样。所以很多小伙伴就怀疑这款游戏是不是有挂,实际上这款游戏确实是有挂的
1.手机麻将有挂是真的吗这款游戏可以开挂,确实是有挂的,通过添加客服微信
2.咨询软件加微信【】在"设置DD功能DD微信手麻工具"里.点击"开启".
3.打开工具.在"设置DD新消息提醒"里.前两个选项"设置"和"连接软件"均勾选"开启"(好多人就是这一步忘记做了)
4.打开某一个微信组.点击右上角.往下拉."消息免打扰"选项.勾选"关闭"(也就是要把"群消息的提示保持在开启"的状态.这样才能触系统发底层接口)
有一个我们老师编写的贪吃蛇程序 供你参考一下:
GreedSnake.java (也是程序入口):
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GreedSnake implements KeyListener {
JFrame mainFrame;
Canvas paintCanvas;
JLabel labelScore;// 计分牌
SnakeModel snakeModel = null;// 蛇
public static final int canvasWidth = 200;
public static final int canvasHeight = 300;
public static final int nodeWidth = 10;
public static final int nodeHeight = 10;
// ----------------------------------------------------------------------
// GreedSnake():初始化游戏界面
// ----------------------------------------------------------------------
public GreedSnake() {
// 设置界面元素
mainFrame = new JFrame("GreedSnake");
Container cp = mainFrame.getContentPane();
labelScore = new JLabel("Score:");
cp.add(labelScore, BorderLayout.NORTH);
paintCanvas = new Canvas();
paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1);
paintCanvas.addKeyListener(this);
cp.add(paintCanvas, BorderLayout.CENTER);
JPanel panelButtom = new JPanel();
panelButtom.setLayout(new BorderLayout());
JLabel labelHelp;// 帮助信息
labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.NORTH);
labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.CENTER);
labelHelp = new JLabel("SPACE or P for pause", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.SOUTH);
cp.add(panelButtom, BorderLayout.SOUTH);
mainFrame.addKeyListener(this);
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
begin();
}
// ----------------------------------------------------------------------
// keyPressed():按键检测
// ----------------------------------------------------------------------
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (snakeModel.running)
switch (keyCode) {
case KeyEvent.VK_UP:
snakeModel.changeDirection(SnakeModel.UP);
break;
case KeyEvent.VK_DOWN:
snakeModel.changeDirection(SnakeModel.DOWN);
break;
case KeyEvent.VK_LEFT:
snakeModel.changeDirection(SnakeModel.LEFT);
break;
case KeyEvent.VK_RIGHT:
snakeModel.changeDirection(SnakeModel.RIGHT);
break;
case KeyEvent.VK_ADD:
case KeyEvent.VK_PAGE_UP:
snakeModel.speedUp();// 加速
break;
case KeyEvent.VK_SUBTRACT:
case KeyEvent.VK_PAGE_DOWN:
snakeModel.speedDown();// 减速
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_P:
snakeModel.changePauseState();// 暂停或继续
break;
default:
}
// 重新开始
if (keyCode == KeyEvent.VK_R || keyCode == KeyEvent.VK_S
|| keyCode == KeyEvent.VK_ENTER) {
snakeModel.running = false;
begin();
}
}
// ----------------------------------------------------------------------
// keyReleased():空函数
// ----------------------------------------------------------------------
public void keyReleased(KeyEvent e) {
}
// ----------------------------------------------------------------------
// keyTyped():空函数
// ----------------------------------------------------------------------
public void keyTyped(KeyEvent e) {
}
// ----------------------------------------------------------------------
// repaint():绘制游戏界面(包括蛇和食物)
// ----------------------------------------------------------------------
void repaint() {
Graphics g = paintCanvas.getGraphics();
// draw background
g.setColor(Color.WHITE);
g.fillRect(0, 0, canvasWidth, canvasHeight);
// draw the snake
g.setColor(Color.BLACK);
LinkedList na = snakeModel.nodeArray;
Iterator it = na.iterator();
while (it.hasNext()) {
Node n = (Node) it.next();
drawNode(g, n);
}
// draw the food
g.setColor(Color.RED);
Node n = snakeModel.food;
drawNode(g, n);
updateScore();
}
// ----------------------------------------------------------------------
// drawNode():绘画某一结点(蛇身或食物)
// ----------------------------------------------------------------------
private void drawNode(Graphics g, Node n) {
g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1,
nodeHeight - 1);
}
// ----------------------------------------------------------------------
// updateScore():改变计分牌
// ----------------------------------------------------------------------
public void updateScore() {
String s = "Score: " + snakeModel.score;
labelScore.setText(s);
}
// ----------------------------------------------------------------------
// begin():游戏开始,放置贪吃蛇
你可以参考一下 每次遇到不会的都来求助是很不方便的
如果你想了解和学习更多的JAVA编程,成为一名真正的JAVA高手,你可以来这个裙,前面三个数是四二六 中间是三九六 后面是二八四
把这串数字连起来就可以了,这是一个高手的学习裙,在这里你可以学习到书上学不到的知识,还有大牛相互讨论并指导你解答哦!
// ----------------------------------------------------------------------
void begin() {
if (snakeModel == null || !snakeModel.running) {
snakeModel = new SnakeModel(this, canvasWidth / nodeWidth,
this.canvasHeight / nodeHeight);
(new Thread(snakeModel)).start();
}
}
// ----------------------------------------------------------------------
// main():主函数
// ----------------------------------------------------------------------
public static void main(String[] args) {
GreedSnake gs = new GreedSnake();
}
}
Node.java:
public class Node {
int x;
int y;
Node(int x, int y) {
this.x = x;
this.y = y;
}
}
SnakeModel.java:
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JOptionPane;
public class SnakeModel implements Runnable {
GreedSnake gs;
boolean[][] matrix;// 界面数据保存在数组里
LinkedList nodeArray = new LinkedList();
Node food;
int maxX;// 最大宽度
int maxY;// 最大长度
int direction = 2;// 方向
boolean running = false;
int timeInterval = 200;// 间隔时间(速度)
double speedChangeRate = 0.75;// 速度改变程度
boolean paused = false;// 游戏状态
int score = 0;
int countMove = 0;
// UP和DOWN是偶数,RIGHT和LEFT是奇数
public static final int UP = 2;
public static final int DOWN = 4;
public static final int LEFT = 1;
public static final int RIGHT = 3;
// ----------------------------------------------------------------------
// GreedModel():初始化界面
// ----------------------------------------------------------------------
public SnakeModel(GreedSnake gs, int maxX, int maxY) {
this.gs = gs;
this.maxX = maxX;
this.maxY = maxY;
matrix = new boolean[maxX][];
for (int i = 0; i < maxX; ++i) {
matrix[i] = new boolean[maxY];
Arrays.fill(matrix[i], false);// 没有蛇和食物的地区置false
}
// 初始化贪吃蛇
int initArrayLength = maxX > 20 ? 10 : maxX / 2;
for (int i = 0; i < initArrayLength; ++i) {
int x = maxX / 2 + i;
int y = maxY / 2;
nodeArray.addLast(new Node(x, y));
matrix[x][y] = true;// 蛇身处置true
}
food = createFood();
matrix[food.x][food.y] = true;// 食物处置true
}
// ----------------------------------------------------------------------
// changeDirection():改变运动方向
// ----------------------------------------------------------------------
public void changeDirection(int newDirection) {
if (direction % 2 != newDirection % 2)// 避免冲突
{
direction = newDirection;
}
}
// ----------------------------------------------------------------------
// moveOn():贪吃蛇运动函数
// ----------------------------------------------------------------------
public boolean moveOn() {
Node n = (Node) nodeArray.getFirst();
int x = n.x;
int y = n.y;
switch (direction) {
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if ((0 <= x && x < maxX) && (0 <= y && y < maxY)) {
if (matrix[x][y])// 吃到食物或者撞到身体
{
if (x == food.x && y == food.y)// 吃到食物
{
nodeArray.addFirst(food);// 在头部加上一结点
// 计分规则与移动长度和速度有关
int scoreGet = (10000 - 200 * countMove) / timeInterval;
score += scoreGet > 0 ? scoreGet : 10;
countMove = 0;
food = createFood();
matrix[food.x][food.y] = true;
return true;
} else
return false;// 撞到身体
} else// 什么都没有碰到
{
nodeArray.addFirst(new Node(x, y));// 加上头部
matrix[x][y] = true;
n = (Node) nodeArray.removeLast();// 去掉尾部
matrix[n.x][n.y] = false;
countMove++;
return true;
}
}
return false;// 越界(撞到墙壁)
}
// ----------------------------------------------------------------------
// run():贪吃蛇运动线程
// ----------------------------------------------------------------------
public void run() {
running = true;
while (running) {
try {
Thread.sleep(timeInterval);
} catch (Exception e) {
break;
}
if (!paused) {
if (moveOn())// 未结束
{
gs.repaint();
} else// 游戏结束
{
JOptionPane.showMessageDialog(null, "GAME OVER",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
running = false;
}
// ----------------------------------------------------------------------
// createFood():生成食物及放置地点
// ----------------------------------------------------------------------
private Node createFood() {
int x = 0;
int y = 0;
do {
Random r = new Random();
x = r.nextInt(maxX);
y = r.nextInt(maxY);
} while (matrix[x][y]);
return new Node(x, y);
}
// ----------------------------------------------------------------------
// speedUp():加快蛇运动速度
// ----------------------------------------------------------------------
public void speedUp() {
timeInterval *= speedChangeRate;
}
// ----------------------------------------------------------------------
// speedDown():放慢蛇运动速度
// ----------------------------------------------------------------------
public void speedDown() {
timeInterval /= speedChangeRate;
}
// ----------------------------------------------------------------------
// changePauseState(): 改变游戏状态(暂停或继续)
// ----------------------------------------------------------------------
public void changePauseState() {
paused = !paused;
}
}
满意请采纳
求贪吃蛇的C++程序代码
//?ConsoleApplication1.cpp:?定义控制台应用程序的入口点。
//
#include?"stdafx.h"
#include?<stdio.h>
#include?<windows.h>
#include?<time.h>
#include?<conio.h>
#define?frame_height?20?//define?map?size
#define?frame_width?40
#define?UP?'w'?//define?operate?key
#define?DOWN?'s'
#define?LEFT?'a'
#define?RIGHT?'d'
int?i,?j,?k;
char?ch?=?UP;?//initial?direction
int?grow?=?0;?//flag:?if?snake?grow
struct?Food?{
int?x;
int?y;
}food;
struct?Snake?{
int?x[50];
int?y[50];
int?len;
int?speed;
}snake;?//snake[0]?is?head
void?init_map(void);
void?update_food(void);
void?move_snake(void);
int?is_alive(void);
void?get_speed(void);
void?gotoxy(int?x,?int?y);
int?main()
{
init_map();?//初始化地图
while?(1)
{
update_food();
//是否产生食物
get_speed();?//获取速度
move_snake();?//移动蛇身
Sleep(snake.speed);
//移动速度
if?(!(is_alive()))
//蛇的死活(撞墙或自食)
break;
}
printf("Game?Over!");
_getch();
return?0;
}
//initialize
void?init_map(void)
{
//initial?food
srand(time(NULL));
food.x?=?rand()?%?(frame_height?-?2)?+?1;
food.y?=?rand()?%?(frame_width?-?2)?+?1;
gotoxy(food.x,?food.y);
printf("!");
//initial?snake
snake.x[0]?=?frame_height?/?2;
snake.y[0]?=?frame_width?/?2;
gotoxy(snake.x[0],?snake.y[0]);
printf("@");
snake.len?=?3;
snake.speed?=?200;
for?(k?=?1;?k<snake.len;?k++)
{
snake.x[k]?=?snake.x[k?-?1]?+?1;
snake.y[k]?=?snake.y[k?-?1];
gotoxy(snake.x[k],?snake.y[k]);
printf("@");
}
//initial?bar
for?(j?=?0;?j<frame_width;?j++)
{
gotoxy(0,?j);
printf("#");
gotoxy(frame_height?-?1,?j);
printf("#");
}
for?(i?=?1;?i<frame_height?-?1;?i++)
{
gotoxy(i,?0);
printf("#");
gotoxy(i,?frame_width?-?1);
printf("#");
}
}
//generate?food
void?update_food()
{
if?(snake.x[0]?==?food.x&&snake.y[0]?==?food.y)
{
srand(time(NULL));
food.x?=?rand()?%?(frame_height?-?2)?+?1;
food.y?=?rand()?%?(frame_width?-?2)?+?1;
gotoxy(food.x,?food.y);
printf("!");
snake.len++;
grow?=?1;
}
}
//move?snake
void?move_snake()
{
if?(_kbhit())
ch?=?_getch();
if?(!grow)
{
gotoxy(snake.x[snake.len?-?1],?snake.y[snake.len?-?1]);
printf("?");
}
for?(k?=?snake.len?-?1;?k>0;?k--)
{
snake.x[k]?=?snake.x[k?-?1];
snake.y[k]?=?snake.y[k?-?1];
}
switch?(ch)
{
case?UP:?snake.x[0]--;?break;
case?DOWN:?snake.x[0]++;?break;
case?LEFT:?snake.y[0]--;?break;
case?RIGHT:?snake.y[0]++;?break;
default:?break;
}
gotoxy(snake.x[0],?snake.y[0]);
printf("@");
grow?=?0;
gotoxy(frame_height,?0);
}
//is?alive
int?is_alive(void)
{
if?(snake.x[0]?==?0?||?snake.x[0]?==?frame_height?-?1?||?snake.y[0]?==?frame_width?-?1?||?snake.y[0]?==?0)
return?0;
for?(k?=?1;?k<snake.len;?k++)
if?(snake.x[k]?==?snake.x[0]?&&?snake.y[k]?==?snake.y[0])
return?0;
return?1;
}
//speed?up
void?get_speed(void)
{
if?(snake.len?<=?6)
snake.speed?=?200;
else?if?(snake.len?<=?10)
snake.speed?=?100;
else?if?(snake.len?<=?20)
snake.speed?=?50;
else?if?(snake.len?<=?30)
snake.speed?=?30;
else?snake.speed?=?20;
}
//move?cursor
void?gotoxy(int?x,?int?y)
{
HANDLE?hout;
COORD?cor;
hout?=?GetStdHandle(STD_OUTPUT_HANDLE);
cor.X?=?y;
cor.Y?=?x;
SetConsoleCursorPosition(hout,?cor);
}
如何用visualC++6.0编写一个贪吃蛇程序
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的结构体*/
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*食物是否出现的变量*/
}food;
struct Snack /*蛇的结构体*/
{
int x[N];
int y[N];
int node; /*蛇的节数*/
int direction; /*蛇的方向*/
int life; /*蛇的生命,0活着,1死亡*/
}snake;
void Init(void); /*图形驱动*/
void Close(void); /*关闭游戏函数*/
void DrawK(void); /*画图函数*/
void GameOver(void);/*输出失败函数*/
void GamePlay(); /*游戏控制函数 主要程序*/
void PrScore(void); /*分数输出函数*/
DELAY(char ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延迟函数*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/*游戏开始菜单*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}
/*主函数*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49); /*画出上边框*/
rectangle(i,451,i+10,460); /*画出下边框*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*画出左边框*/
rectangle(601,i,610,i+10); /*画出右边框*/
}
}
void GamePlay(char ch)
{
randomize(); /*随机数发生器*/
food.yes=1; /*1代表要出现食物,0表示以存在食物*/
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1) /*可以重复游戏*/
{
while(!kbhit()) /*在没有按键的情况下蛇自己移动*/
{
if(food.yes==1) /*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++; /*判断食物是否出现在整格里*/
food.yes=0; /*现在有食物了*/
}
if(food.yes==0) /*有食物了就要显示出来*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--) /*贪吃蛇的移动算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/
}
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++) /*判断是否头部与身体相撞*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
/*下面是判断是否撞到墙壁*/
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)
{
GameOver();
snake.life=1;
}
if(snake.life==1) /*如果死亡就退出循环*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y) /*判断蛇是否吃到食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10); /*吃的食物后用黑色将食物擦去*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*现把增加的一节放到看不到的地方去*/
snake.node++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4); /*每次移动后将后面的身体擦去*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(snake.life==1)
break;
key=bioskey(0); /*接受按键*/
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)/*判断是否改变方向*/
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;
}
}
void GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}
#ifndef __COLORCONSOLE__H__
#define __COLORCONSOLE__H__
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <time.h>
using namespace std;
HANDLE initiate();
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString);
#endif
#ifndef __FOOD__H__ //防止重复引用该头文件
#define __FOOD__H__
#include "colorConsole.h"
class Food
{
public:
void initfood(int); //出始化食物函数
protected:
WORD goodcolor[1]; //定义正常食物的颜色
WORD badcolor[1]; //定义变质食物的颜色
int numgood,goodx[50],goody[50]; //定义正常食物的个数、坐标
int numbad,badx[30],bady[30]; //定义变质食物的个数、坐标
};
#endif
#ifndef __FOOD__H__ //防止重复引用该头文件
#define __FOOD__H__
#include "colorConsole.h"
class Food
{
public:
void initfood(int); //出始化食物函数
protected:
WORD goodcolor[1]; //定义正常食物的颜色
WORD badcolor[1]; //定义变质食物的颜色
int numgood,goodx[50],goody[50]; //定义正常食物的个数、坐标
int numbad,badx[30],bady[30]; //定义变质食物的个数、坐标
};
#endif
#ifndef __SNAKE__H__ //防止重复引用该头文件
#define __SNAKE__H__
#include "colorConsole.h"
class Snake
{
public:
void initsnake(); //初始化蛇的函数
protected:
WORD bColor[1]; //定义蛇身的颜色
WORD hColor[1]; //定义蛇头的颜色
int headx,heady,bodyx[100],bodyy[100]; //定义蛇头和蛇身的坐标
int node; //定义蛇身的节数
};
#endif
#include "colorConsole.h"
HANDLE initiate()
{
HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
return hOutput;
}
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString)
{
DWORD cWritten;
BOOL fSuccess;
COORD coord;
coord.X = x; // start at first cell
coord.Y = y; // of first row
fSuccess = WriteConsoleOutputCharacter(
hOutput, // screen buffer handle
lpszString, // pointer to source string
lstrlen(lpszString), // length of string
coord, // first cell to write to
&cWritten); // actual number written
if (! fSuccess)
cout<<"error:WriteConsoleOutputCharacter"<<endl;
for (;fSuccess && coord.X < lstrlen(lpszString)+x; coord.X += nColors)
{
fSuccess = WriteConsoleOutputAttribute(
hOutput, // screen buffer handle
wColors, // pointer to source string
nColors, // length of string
coord, // first cell to write to
&cWritten); // actual number written
}
if (! fSuccess)
cout<<"error:WriteConsoleOutputAttribute"<<endl;
return 0;
}
#include "Food.h"
HANDLE handle;
void Food::initfood(int lev) //初始化食物
{
numgood=lev*5;
numbad=lev*2;
goodcolor[0]=FOREGROUND_RED|FOREGROUND_BLUE; //正常食物的颜色
badcolor[0]=FOREGROUND_RED|FOREGROUND_GREEN; //变质食物的颜色
srand((unsigned) time(NULL));
textout(handle,64,6,goodcolor,1,"正常食物为★"); //输出图例
textout(handle,64,8,badcolor,1,"变质食物为★");
for(int fgood=0;fgood<numgood;fgood++) //输出正常食物
{
goodx[fgood]=((rand()%55+4)/2)*2;
goody[fgood]=rand()%26+4;
textout(handle,goodx[fgood],goody[fgood],goodcolor,1,"★");
}
for(int fbad=0;fbad<numbad;fbad++) //输出变质食物
{
badx[fbad]=((rand()%56+4)/2)*2;
bady[fbad]=rand()%26+4;
textout(handle,badx[fbad],bady[fbad],badcolor,1,"★");
}
}
#include "Game.h"
HANDLE handle;
Game::Game()
{
node=3; //蛇的出始默认节数
headx=44,heady=4; //蛇头的出始默认坐标
controlx=-2,controly=0; //蛇的出始默认方向为向左
start=0;
scores=0;
self=0,wall=0,died=0; //游戏结束条件默认为否
sColor[0]=FOREGROUND_GREEN|FOREGROUND_INTENSITY; //围墙颜色
head[0]=FOREGROUND_RED|FOREGROUND_INTENSITY; //开始界面文字颜色
}
int Game::starting()
{
while(1)
{
textout(handle,22,16,head,1,"控制键: W-上 S-下 A-左 D-右"); //开始界面
textout(handle,16,18,head,1,"吃完所有正常食物升级 千万不要吃到变质的食物哦!");
textout(handle,30,20,head,1, "选择级数 1-9 ");
if(_kbhit())
{
int v=_getch(); //选择级数
switch(v)
{
case 49:
level=1;
break;
case 50:
level=2;
break;
case 51:
level=3;
break;
case 52:
level=4;
break;
case 53:
level=5;
break;
case 54:
level=6;
break;
case 55:
level=7;
break;
case 56:
level=8;
break;
default:
level=9;
break;
}
textout(handle,16,18,head,1," "); //清屏
textout(handle,30,20,head,1, " ");
textout(handle,22,16,head,1," ");
break;
}
}
//设置四周围墙
for(int wideup=2;wideup<60;wideup+=2) //上边
{
textout(handle,wideup,2,sColor,1,"■");
}
for(int widedown=2;widedown<60;widedown+=2) //下边
{
textout(handle,widedown,30,sColor,1,"■");
}
for(int longr=2;longr<30;longr++) //左边
{
textout(handle,2,longr,sColor,1,"■");
}
for(int longl=2;longl<31;longl++) //右边
{
textout(handle,60,longl,sColor,1,"■");
}
levelup=5*level;
return level;
}
void Game::ifstart() //由用户判断是否开始游戏
{
textout(handle,24,0,sColor,1,"按 y 开始游戏");
int v=_getch();
switch(v)
{
case 'y': start=1;break;
default : start=0;break;
}
textout(handle,24,0,sColor,1," "); //清屏
}
bool Game::getstart() //获得start
{
return start;
}
void Game::moventurn() //关于移动和转向的函数
{
int len1=0; //设置一个消除原有图形的计数器
for(int len=0;len<node;len++) //消除原有图形
{
textout(handle,bodyx[len],bodyy[len],bColor,1," ");
}
for(int len6=node-1;len6>0;len6--) //显示移动以后的图形
{
len1=len6-1;
bodyx[len6]=bodyx[len1]; //获得前一节body的坐标
bodyy[len6]=bodyy[len1];
textout(handle,bodyx[len6],bodyy[len6],bColor,1,"●"); //移动后的图形
}
bodyx[0]=headx;
bodyy[0]=heady;
textout(handle,bodyx[0],bodyy[0],bColor,1,"●"); //显示移动后的第一节
headx+=controlx;
heady+=controly;
textout(handle,headx,heady,hColor,1,"◎"); //显示移动后的头部
Sleep(165-level*17); //速度
if(kbhit()) //转向
{
char direction=getch(); //获取方向
if(direction=='w'&&direc!='s')
{
textout(handle,headx,heady,hColor,1," ");
textout(handle,headx,heady,hColor,1,"◎");
controlx=0;
controly=-1;
direc='w';
}
if(direction=='s'&&direc!='w')
{
textout(handle,headx,heady,hColor,1," ");
textout(handle,headx,heady,hColor,1,"◎");
controlx=0;
controly=1;
direc='s';
}
if(direction=='a'&&direc!='d')
{
textout(handle,headx,heady,hColor,1," ");
textout(handle,headx,heady,hColor,1,"◎");
controlx=-2;
controly=0;
direc='a';
}
if(direction=='d'&&direc!='a')
{
textout(handle,headx,heady,hColor,1," ");
textout(handle,headx,heady,hColor,1,"◎");
controlx=2;
controly=0;
direc='d';
}
}
}
bool Game::ifgameover() //判断是否结束游戏
{
if(headx==2||headx==60||heady==2||heady==30) //撞墙
{
wall=1;
return 1;
}
for(int sbody=1;sbody<node;sbody++) //咬到自己
{
if(headx==bodyx[sbody]&&heady==bodyy[sbody])
{
self=1;
return 1;
}
}
for(int countbad=0;countbad<numbad;countbad++) //吃到变质食物
{
if(headx==badx[countbad]&&heady==bady[countbad])
{
died=1;
return 1;
}
}
return 0;
}
void Game::gameover() //输出游戏结束界面
{
//sndPlaySound("DOWN.wav",SND_ASYNC);
if(wall==1)
textout(handle,20,14,sColor,1,"撞到墙了 游戏结束");
if(self==1)
textout(handle,20,14,sColor,1,"咬到自己了 游戏结束");
if(died==1)
textout(handle,19,14,sColor,1,"吃到变质食物了 游戏结束");
}
void Game::scorenlevelup()
{
char marks[5],Level[5]; //准备输出级数和分数
for(int eatgood=0;eatgood<numgood;eatgood++) //判断是否咬到正常食物以加分
{
if(headx==goodx[eatgood]&&heady==goody[eatgood])
{
//sndPlaySound("LASER.wav",SND_ASYNC);
goodx[eatgood]=74; //防止以后走到已吃过食物点的时候会判断为吃到而加分以影响升级
goody[eatgood]=6;
node++; //长一截
scores+=10; //加十分
itoa(scores,marks,10); //转换数据类型,准备输出
itoa(level,Level,10);
textout(handle,64,11,hColor,1,"分数:"); //输出分数
textout(handle,70,11,hColor,1,marks);
textout(handle,64,13,hColor,1,"级数:"); //输出级数
textout(handle,70,13,hColor,1,Level);
levelup--;
if(levelup==4)
{
for(int fgood=0;fgood<numgood;fgood++) //再次输出正常食物
{
textout(handle,goodx[fgood],goody[fgood],goodcolor,1,"★");
}
}
}
}
if(levelup==0) //升级
{
level++;
levelup=5*level;
for(int i=4;i<58;i+=2) //清屏
{
for(int j=4;j<30;j++)
{
textout(handle,i,j,sColor,1," ");
}
}
initfood(level); //输出食物
textout(handle,headx,heady,hColor,1,"◎"); //打印蛇头
for(int j=0;j<node;j++) //打印蛇身
{
textout(handle,bodyx[j],bodyy[j],bColor,1,"●");
}
textout(handle,18,0,hColor,1,"按任意键开始更高的等级");
itoa(level,Level,10);
textout(handle,70,13,hColor,1,Level); //输出新的级数
while(1) //由用户控制是否开始更高等级
{
if (_kbhit())
{
textout(handle,18,0,sColor,1, " ");
break;
}
}
}
}
#include "Snake.h"
extern HANDLE handle;
void Snake::initsnake() //初始化蛇
{
hColor[0]=FOREGROUND_RED|FOREGROUND_INTENSITY; //蛇头颜色
bColor[0]=FOREGROUND_BLUE|FOREGROUND_INTENSITY; //蛇身颜色
for(int i=0;i<node;i++) //蛇身各节的坐标定义
{
bodyx[i]=(i+1)*2+headx;
bodyy[i]=heady;
}
textout(handle,headx,heady,hColor,1,"◎"); //打印蛇头
for(int j=0;j<node;j++) //打印蛇身
{
textout(handle,bodyx[j],bodyy[j],bColor,1,"●");
}
}
#include "Snake.h"
#include "Food.h"
#include "Game.h"
Game game;
HANDLE handle; //窗口句柄
void main()
{
//sndPlaySound("BACKGROUND.wav",SND_LOOP|SND_ASYNC);
handle=initiate(); //初始化窗口句柄
game.initfood(game.starting()); //初始化食物
game.initsnake(); //初始化蛇
game.ifstart(); //玩家是否开始游戏
if(game.getstart())
{ while(1)
{
game.moventurn(); //蛇的移动和转向
game.scorenlevelup(); //得分和升级
if(game.ifgameover()) //判断是否中止游戏
break;
}
game.gameover(); //输出游戏结束原因
}
}
这是一个控制台的贪吃蛇 比较简单
只要楼主了解几个API函数就可以 至于算法 有好多 我这个不怎么样
我建议用链表来表示蛇体 下面是我的一个snake 类 函数体没给 楼主自己想吧
struct node
{node* pre;
Cpoint point;
node* next;
};
node::node()
{ point(0,0);
pre=NULL;
next=NULL;
};
class snake
{ private:
node* snakehead;
node snakebody[100];
int length
int direction;
public:
void go();
bool fail();
void textout();
} ;
关于“编写贪吃蛇程序”这个话题的介绍,今天小编就给大家分享完了,如果对你有所帮助请保持对本站的关注!