谁给下这段贪吃蛇代码注释 100
#include"graphics.h"#include<stdlib.h>#defineMAP_W40#defineMAP_H30constintGCOLOR[]={D...
#include "graphics.h"
#include <stdlib.h>
#define MAP_W 40
#define MAP_H 30
const int GCOLOR[] = {DARKGRAY, GREEN, RED};
int gw, gh;
struct SNAKE {
int dir, head, inc, tail;
int pool[MAP_W* MAP_H];
} game;
inline void drawAt( const int &i ) {
int x = ( i % MAP_W ) * gw, y = ( i / MAP_W ) * gh;
setfillcolor( GCOLOR[game.pool[i] >> 16] );
bar( x, y, x + gw, y + gh );
}
void newFruit( void ) {
int nf;
while ( game.pool[nf = random( MAP_W * MAP_H )] >> 16 );
game.pool[nf] = 0x20000, drawAt( nf );
}
int moveSnake( const int dx, const int dy, const bool u = false ) {
if ( u && dx + ( game.dir & 3 ) == 1 && dy + ( game.dir >> 2 ) == 1 ) return 1;
int nh;
if ( dx && !dy ) {
nh = game.head % MAP_W + dx;
if ( nh < 0 || nh >= MAP_W ) return 0;
nh = game.head + dx;
} else {
nh = game.head / MAP_W + dy;
if ( nh < 0 || nh >= MAP_H ) return 0;
nh = game.head + dy * MAP_W;
}
int s = game.pool[nh] >> 16;
if ( s == 1 ) return 0;
if ( s == 2 ) game.inc += 5, newFruit();
if ( game.inc > 0 ) --game.inc;
else {
game.tail = game.pool[s = game.tail] & 0xffff;
game.pool[s] = 0, drawAt( s );
}
game.pool[game.head] |= nh;
game.pool[game.head = nh] = 0x10000, drawAt( nh );
game.dir = ( dx + 1 ) | ( ( dy + 1 ) << 2 );
return 1;
}
void gameInit( void ) {
int data[] = {6, 0, 2, 0, 0x10000};
memset( game.pool, 0, sizeof( game.pool ) );
memmove( &game, data, sizeof( data ) );
}
void gameScene( void ) {
setbkcolor( DARKGRAY );
setfillcolor( GREEN );
bar( 0, 0, gw, gh );
newFruit();
for ( int c = -1; is_run(); delay_fps( 60 ), --c ) {
while ( kbhit() ) {
int key = getch() | 0x20;
if ( key == ( 27 | 0x20 ) ) return;
if ( key == 'a' || key == 'd' ) {
if ( !moveSnake( ( ( key - 'a' ) >> 1 << 1 ) - 1, 0, true ) ) return;
} else if ( key == 's' || key == 'w' ) {
if ( !moveSnake( 0, 1 - ( ( key - 's' ) >> 2 << 1 ), true ) ) return;
}
}
if ( c < 0 ) {
if ( !moveSnake( ( game.dir & 3 ) - 1, ( game.dir >> 2 ) - 1 ) ) return;
c = 20;
}
}
}
int main( void ) {
setinitmode( INIT_ANIMATION );
initgraph( 640, 480 );
gw = getwidth() / MAP_W, gh = getheight() / MAP_H;
randomize();
gameInit();
gameScene();
return 0;
} 展开
#include <stdlib.h>
#define MAP_W 40
#define MAP_H 30
const int GCOLOR[] = {DARKGRAY, GREEN, RED};
int gw, gh;
struct SNAKE {
int dir, head, inc, tail;
int pool[MAP_W* MAP_H];
} game;
inline void drawAt( const int &i ) {
int x = ( i % MAP_W ) * gw, y = ( i / MAP_W ) * gh;
setfillcolor( GCOLOR[game.pool[i] >> 16] );
bar( x, y, x + gw, y + gh );
}
void newFruit( void ) {
int nf;
while ( game.pool[nf = random( MAP_W * MAP_H )] >> 16 );
game.pool[nf] = 0x20000, drawAt( nf );
}
int moveSnake( const int dx, const int dy, const bool u = false ) {
if ( u && dx + ( game.dir & 3 ) == 1 && dy + ( game.dir >> 2 ) == 1 ) return 1;
int nh;
if ( dx && !dy ) {
nh = game.head % MAP_W + dx;
if ( nh < 0 || nh >= MAP_W ) return 0;
nh = game.head + dx;
} else {
nh = game.head / MAP_W + dy;
if ( nh < 0 || nh >= MAP_H ) return 0;
nh = game.head + dy * MAP_W;
}
int s = game.pool[nh] >> 16;
if ( s == 1 ) return 0;
if ( s == 2 ) game.inc += 5, newFruit();
if ( game.inc > 0 ) --game.inc;
else {
game.tail = game.pool[s = game.tail] & 0xffff;
game.pool[s] = 0, drawAt( s );
}
game.pool[game.head] |= nh;
game.pool[game.head = nh] = 0x10000, drawAt( nh );
game.dir = ( dx + 1 ) | ( ( dy + 1 ) << 2 );
return 1;
}
void gameInit( void ) {
int data[] = {6, 0, 2, 0, 0x10000};
memset( game.pool, 0, sizeof( game.pool ) );
memmove( &game, data, sizeof( data ) );
}
void gameScene( void ) {
setbkcolor( DARKGRAY );
setfillcolor( GREEN );
bar( 0, 0, gw, gh );
newFruit();
for ( int c = -1; is_run(); delay_fps( 60 ), --c ) {
while ( kbhit() ) {
int key = getch() | 0x20;
if ( key == ( 27 | 0x20 ) ) return;
if ( key == 'a' || key == 'd' ) {
if ( !moveSnake( ( ( key - 'a' ) >> 1 << 1 ) - 1, 0, true ) ) return;
} else if ( key == 's' || key == 'w' ) {
if ( !moveSnake( 0, 1 - ( ( key - 's' ) >> 2 << 1 ), true ) ) return;
}
}
if ( c < 0 ) {
if ( !moveSnake( ( game.dir & 3 ) - 1, ( game.dir >> 2 ) - 1 ) ) return;
c = 20;
}
}
}
int main( void ) {
setinitmode( INIT_ANIMATION );
initgraph( 640, 480 );
gw = getwidth() / MAP_W, gh = getheight() / MAP_H;
randomize();
gameInit();
gameScene();
return 0;
} 展开
1个回答
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
public class InterFace extends JFrame {
/**
* WIDTH:宽
* HEIGHT:高
* SLEEPTIME:可以看作蛇运动的速度
* L = 1,R = 2, U = 3, D = 4 左右上下代码
*/
public static final int WIDTH = 800, HEIGHT = 600, SLEEPTIME = 200, L = 1,R = 2, U = 3, D = 4;
BufferedImage offersetImage= new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_3BYTE_BGR);;
Rectangle rect = new Rectangle(20, 40, 15 * 50, 15 * 35);
Snake snake;
Node node;
public InterFace() {
//创建"蛇"对象
snake = new Snake(this);
//创建"食物"对象
createNode();
this.setBounds(100, 100, WIDTH, HEIGHT);
//添加键盘监听器
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent arg0) {
System.out.println(arg0.getKeyCode());
switch (arg0.getKeyCode()) {
//映射上下左右4个键位
case KeyEvent.VK_LEFT:
snake.dir = L;
break;
case KeyEvent.VK_RIGHT:
snake.dir = R;
break;
case KeyEvent.VK_UP:
snake.dir = U;
break;
case KeyEvent.VK_DOWN:
snake.dir = D;
}
}
});
this.setTitle("贪吃蛇 0.1 By : Easy");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
//启动线程,开始执行
new Thread(new ThreadUpadte()).start();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) offersetImage.getGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, WIDTH, HEIGHT);
g2d.setColor(Color.black);
g2d.drawRect(rect.x, rect.y, rect.width, rect.height);
//如果蛇碰撞(吃)到食物,则创建新食物
if (snake.hit(node)) {
createNode();
}
snake.draw(g2d);
node.draw(g2d);
g.drawImage(offersetImage, 0, 0, null);
}
class ThreadUpadte implements Runnable {
public void run() {
//无限重绘画面
while (true) {
try {
Thread.sleep(SLEEPTIME);
repaint(); //
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 创建食物
*/
public void createNode() {
//随机食物的出现位置
int x = (int) (Math.random() * 650) + 50,y = (int) (Math.random() * 500) + 50;
Color color = Color.blue;
node = new Node(x, y, color);
}
public static void main(String args[]) {
new InterFace();
}
}
/**
* 节点类(包括食物和蛇的身躯组成节点)
*/
class Node {
int x, y, width = 15, height = 15;
Color color;
public Node(int x, int y, Color color) {
this(x, y);
this.color = color;
}
public Node(int x, int y) {
this.x = x;
this.y = y;
this.color = color.black;
}
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.drawRect(x, y, width, height);
}
public Rectangle getRect() {
return new Rectangle(x, y, width, height);
}
}
/**
* 蛇
*/
class Snake {
public List<Node> nodes = new ArrayList<Node>();
InterFace interFace;
int dir=InterFace.R;
public Snake(InterFace interFace) {
this.interFace = interFace;
nodes.add(new Node(20 + 150, 40 + 150));
addNode();
}
/**
* 是否碰撞到食物
* @return true 是 false 否
*/
public boolean hit(Node node) {
//遍历整个蛇体是否与食物碰撞
for (int i = 0; i < nodes.size(); i++) {
if (nodes.get(i).getRect().intersects(node.getRect())) {
addNode();
return true;
}
}
return false;
}
public void draw(Graphics2D g2d) {
for (int i = 0; i < nodes.size(); i++) {
nodes.get(i).draw(g2d);
}
move();
}
public void move() {
nodes.remove((nodes.size() - 1));
addNode();
}
public synchronized void addNode() {
Node nodeTempNode = nodes.get(0);
//如果方向
switch (dir) {
case InterFace.L:
//判断是否会撞墙
if (nodeTempNode.x <= 20) {
nodeTempNode = new Node(20 + 15 * 50, nodeTempNode.y);
}
nodes.add(0, new Node(nodeTempNode.x - nodeTempNode.width,
nodeTempNode.y));
break;
case InterFace.R:
//判断是否会撞墙
if (nodeTempNode.x >= 20 + 15 * 50 - nodeTempNode.width) {
nodeTempNode = new Node(20 - nodeTempNode.width, nodeTempNode.y);
}
nodes.add(0, new Node(nodeTempNode.x + nodeTempNode.width,
nodeTempNode.y));
break;
case InterFace.U:
//判断是否会撞墙
if (nodeTempNode.y <= 40) {
nodeTempNode = new Node(nodeTempNode.x, 40 + 15 * 35);
}
nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y - nodeTempNode.height));
break;
case InterFace.D:
//判断是否会撞墙
if (nodeTempNode.y >= 40 + 15 * 35 - nodeTempNode.height) {
nodeTempNode = new Node(nodeTempNode.x,40 - nodeTempNode.height);
}
nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y + nodeTempNode.height));
break;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询