猿编程游戏代码大全(如何用猿编程编游戏)

http://www.itjxue.com  2023-03-18 02:00  来源:未知  点击次数: 

c++编程小游戏代码

以下是贪吃蛇源代码:

?

#includeiostream.h

#includewindows.h

#includetime.h

#includestdlib.h

#includeconio.h

#define?N?21

void?gotoxy(int?x,int?y)//位置函数{

COORD?pos;

pos.X=2*x;

pos.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);

}

void?color(int?a)//颜色函数{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);

}

void?init(int?apple[2])//初始化函数(初始化围墙、显示信息、苹果)

{

int?i,j;//初始化围墙

int?wall[N+2][N+2]={{0}};

for(i=1;i=N;i++)

{

for(j=1;j=N;j++)

wall[i][j]=1;

}

color(11);

for(i=0;iN+2;i++)

{

for(j=0;jN+2;j++)

{

if(wall[i][j])

cout"■";

else?cout"□"?;

}

coutendl;

}

gotoxy(N+3,1);//显示信息

color(20);

cout"按?W?S?A?D?移动方向"endl;

gotoxy(N+3,2);

color(20);

cout"按任意键暂停"endl;

gotoxy(N+3,3);

color(20);

cout"得分:"endl;

apple[0]=rand()%N+1;//苹果

apple[1]=rand()%N+1;

gotoxy(apple[0],apple[1]);

color(12);

cout"●"endl;

}

int?main()

{

int?i,j;

int**?snake=NULL;

int?apple[2];

int?score=0;

int?tail[2];

int?len=3;

char?ch='p';

srand((unsigned)time(NULL));

init(apple);

snake=(int**)realloc(snake,sizeof(int*)*len);

for(i=0;ilen;i++)

snake[i]=(int*)malloc(sizeof(int)*2);

for(i=0;ilen;i++)

{

snake[i][0]=N/2;

snake[i][1]=N/2+i;

gotoxy(snake[i][0],snake[i][1]);

color(14);

cout"★"endl;

}

while(1)//进入消息循环

{

tail[0]=snake[len-1][0];

tail[1]=snake[len-1][1];

gotoxy(tail[0],tail[1]);

color(11);

cout"■"endl;

for(i=len-1;i0;i--)

{

snake[i][0]=snake[i-1][0];

snake[i][1]=snake[i-1][1];

gotoxy(snake[i][0],snake[i][1]);

color(14);

cout"★"endl;

}

if(kbhit())

{

gotoxy(0,N+2);

ch=getche();

}

switch(ch)

{

case?'w':snake[0][1]--;break;

case?'s':snake[0][1]++;break;

case?'a':snake[0][0]--;break;

case?'d':snake[0][0]++;break;

default:?break;

}

gotoxy(snake[0][0],snake[0][1]);

color(14);

cout"★"endl;

Sleep(abs(200-0.5*score));

if(snake[0][0]==apple[0]snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1

{

score++;

len++;

snake=(int**)realloc(snake,sizeof(int*)*len);

snake[len-1]=(int*)malloc(sizeof(int)*2);

apple[0]=rand()%N+1;

apple[1]=rand()%N+1;

gotoxy(apple[0],apple[1]);

color(12);

cout"●"endl;

gotoxy(N+5,3);

color(20);

coutscoreendl;

}

if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败

{

gotoxy(N/2,N/2);

color(30);

cout"失败!!!"endl;

for(i=0;ilen;i++)

free(snake[i]);

Sleep(INFINITE);

exit(0);

}

}

return?0;

}

怎么编程一个最简单游戏代码?

利用随机数猜大小,如下:

1、代码的第一行,是一个include语句。没有它我们的程序会编译不过。有了它就是告诉编译器在对代码进行编译之前,必须要包含程序需要的文件。这里的stdio.h就是我们需要的头文件。

2、代码第二行是一个main函数,这个main函数的返回值是一个int整型数据。刚开始学习编程的时候我们可以认为程序运行的时候是从main函数开始的。后续会专门给大家做一个介绍向大家说明在main函数之前还做了哪些事情。

3、每个函数都用一对“{}”进行包含,表示着函数体的开始和结束,当然后面说到控制语句的时候它还表示一段控制语句的开始和结束。

4、main函数中调用了一个printf函数。它是用来向控制台输出我们想要的内容。printf的函数定位格式为:int printf(constchar*format,...)。format中定义了输出内容和格式。

5、return函数执行完后。在退出函数体之前,会将函数进行返回。return后的内容根据函数返回值定义而定。在本段程序中返回的是整型数据0。

猿编程中的输入文字代码是什么

1FANUC 车床G代码G02顺时针切圆弧(CW,顺时钟)G03逆时针切圆弧(CCW,逆时钟)G04暂停(Dwel1)G09停于精确的位置G20英制输入G21公制输入G22内部行程限位有效G23内部行程限位无效G27检查参考点返回G28参考点返回G29从参考点返回G30回到第二参考点G32切螺纹G40取消刀尖半径偏置G41刀尖半径偏置(左侧)G42刀尖半径偏置(右侧)G50修改工件坐标;设置主轴最大的RPMG52设置局部坐标系G53选择机床坐标系G70精加工循环G71内外径粗切循环G72台阶粗切循环G73成形重复循环2. FANUC t床G代码G00顶位(快速移动)定位(快速移动)G01直线切削G02顺时针切圆弧G03逆时针切圆弧G04暂停G15/G16极坐标指令G17XY面赋值G18X2面赋值G19YZ面赋值G28机床返回原点G30机床返回第2和第3原点*G40取消刀具直径偏移G41刀具直径左偏移G42刀具直径右偏移*G43刀具长度+方向偏移*G44刀具长度-方向偏移G49取消刀具长度偏移*G53机床坐标系选择G54工件坐标系1选择G55工件坐标系2选择G56工件坐标系3选择G57工件坐标系4选择G58工件坐标系5选择G59工件坐标系6选择G73高速深孔钻削循环G74左螺旋切削循环G76精镜孔循环

5

百度文库VIP限时优惠现在开通,立享6亿+VIP内容

立即获取

猿编程代码大全复制

智阳文库

猿编程代码大全复制

1FANUC 车床G代码G02顺时针切圆弧(CW,顺时钟)G03逆时针切圆弧(CCW,逆时钟)G04暂停(Dwel1)G09停于精确的位置G20英制输入G21公制输入G22内部行程限位有效G23内部行程限位无效G27检查参考点返回G28参考点返回G29从参考点返回G30回到第二参考点G32切螺纹G40取消刀尖半径偏置G41刀尖半径偏置(左侧)G42刀尖半径偏置(右侧)G50修改工件坐标;设置主轴最大的RPMG52设置局部坐标系G53选择机床坐标系G70精加工循环G71内外径粗切循环G72台阶粗切循环G73成形重复循环

小游戏的C++代码

/*一个火柴人游戏,亲自验证,可运行*/

/*在编译时添加如下命令:-std=c++11,否则会编译错误*/

#include cstdio

#include cstdlib

#include Windows.h

#include thread

#include conio.h

using namespace std;

const unsigned char CTRL_KEY = 0XE0;

const unsigned char LEFT = 0X4B;

const unsigned char RIGHT = 0X4D;

const unsigned char DOWN = 0X50;

const unsigned char UP = 0X48;

int men2[2] = {0,0};

int women2[2]={10,10};

int Game();

void gotoxy( int x, int y ) //光标移动到(x,y)位置

{

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition(handle,pos);

}

int clean( int mm, int nn )

{

gotoxy ( mm, nn );

printf ( " " );

gotoxy ( mm,nn+1);

printf ( " " );

gotoxy ( mm,nn+2);

printf (" ");

}

int men( int x, int y )

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN);

gotoxy( x, y );

printf(" O");

gotoxy( x, y+1 );

printf("H");

gotoxy( x, y+2 );

printf("I I");

}

int women( int i, int j )

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);

gotoxy( i+1,j );

printf(" O");

gotoxy( i+1,j+1 );

printf("H");

gotoxy( i,j+2 );

printf("/I I\\");

}

int m=10, n=10;

int x=0;int y=0;

int TorF()

{

if ( x == m y == n ) return 1;

else return 0;

}

int womenmove()

{

int turn;

int YNbreak=0;

while( YNbreak == 0 )

{

YNbreaak = TorF();

turn=rand()%3;

clean( m, n );

if( m x ) m++;

else m--;

if( m == x )

{

if( n y ) n++;

else n--;

}

if ( m 0 ) m = 0;

if ( m = 75 ) m = 75;

if ( n 0 ) n = 0;

if ( n = 22 ) n = 22;

women( m,n );

women2[0]=m;

women2[1]=n;

Sleep(100);

}

system ( "cls" );

gotoxy ( 28, 10 );

printf ( "You died!!!\n" );

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE);

system ( "pause" );

exit(0);

return 0;

}

int menmove()

{

system( "cls" );

while (1)

{

switch( getch())

{

case UP:y--;break;

case DOWN:y++;break;

case LEFT:x--;break;

case RIGHT:x++;break;

}

system( "cls" );

if ( x 0 ) x = 0;

if ( x 77 ) x = 77;

if ( y 0 ) y = 0;

if ( y 22 ) y = 22;

men( x, y );

men2[0] = x;

men2[1] = y;

}

}

int Game()

{

women( 10, 10 );

men( 0, 0 );

int t = 0;

thread qq( womenmove );

menmove();

qq.join();

return 0;

}

int main()

{

system( "mode con cols=80 lines=25" );

printf ( "游戏开始后,随机按下一个键,唤醒你的蓝色小人.如果你被红色的老女人碰到了,那么你就死了\n" );

printf ( "方向键操控小人\n" );

system ( "pause" );

system ( "cls" );

Game();

return 0;

}

/*留下您的赞再拿走,谢谢!*/

python有趣的编程代码

class?Point:

??row=0

??col=0

??def?__init__(self,?row,?col):

????self.row=row

????self.col=col

??def?copy(self):

????return?Point(row=self.row,?col=self.col)

#初始框架

import?pygame

import?random

#初始化

pygame.init()

W=800

H=600

ROW=30

COL=40

size=(W,H)

window=pygame.display.set_mode(size)

pygame.display.set_caption('贪吃蛇')

bg_color=(255,255,255)

snake_color=(200,200,200)

head=Point(row=int(ROW/2),?col=int(COL/2))

head_color=(0,128,128)

snakes=[

??Point(row=head.row,?col=head.col+1),

??Point(row=head.row,?col=head.col+2),

??Point(row=head.row,?col=head.col+3)

]

#生成食物

def?gen_food():

??while?1:

????pos=Point(row=random.randint(0,ROW-1),?col=random.randint(0,COL-1))

????#

????is_coll=False

????#是否跟蛇碰上了

????if?head.row==pos.row?and?head.col==pos.col:

??????is_coll=True

????#蛇身子

????for?snake?in?snakes:

??????if?snake.row==pos.row?and?snake.col==pos.col:

????????is_coll=True

????????break

????if?not?is_coll:

??????break

??return?pos

#定义坐标

food=gen_food()

food_color=(255,255,0)

direct='left'???????#left,right,up,down

#

def?rect(point,?color):

??cell_width=W/COL

??cell_height=H/ROW

??left=point.col*cell_width

??top=point.row*cell_height

??pygame.draw.rect(

????window,?color,

????(left,?top,?cell_width,?cell_height)

??)

??pass

#游戏循环

quit=True

clock=pygame.time.Clock()

while?quit:

??#处理事件

??for?event?in?pygame.event.get():

????if?event.type==pygame.QUIT:

??????quit=False

????elif?event.type==pygame.KEYDOWN:

??????if?event.key==273?or?event.key==119:

????????if?direct=='left'?or?direct=='right':

??????????direct='up'

??????elif?event.key==274?or?event.key==115:

????????if?direct?==?'left'?or?direct?==?'right':

??????????direct='down'

??????elif?event.key==276?or?event.key==97:

????????if?direct?==?'up'?or?direct?==?'down':

??????????direct='left'

??????elif?event.key==275?or?event.key==100:

????????if?direct?==?'up'?or?direct?==?'down':

??????????direct='right'

??#吃东西

??eat=(head.row==food.row?and?head.col==food.col)

??#重新产生食物

??if?eat:

????food?=?gen_food()

??#处理身子

??#1.把原来的头,插入到snakes的头上

??snakes.insert(0,?head.copy())

??#2.把snakes的最后一个删掉

??if?not?eat:

????snakes.pop()

??#移动

??if?direct=='left':

????head.col-=1

??elif?direct=='right':

????head.col+=1

??elif?direct=='up':

????head.row-=1

??elif?direct=='down':

????head.row+=1

??#检测

??dead=False

??#1.撞墙

??if?head.col0?or?head.row0?or?head.col=COL?or?head.row=ROW:

????dead=True

??#2.撞自己

??for?snake?in?snakes:

????if?head.col==snake.col?and?head.row==snake.row:

??????dead=True

??????break

??if?dead:

????print('死了')

????quit=False

??#渲染——画出来

??#背景

??pygame.draw.rect(window,?bg_color,?(0,0,W,H))

??#蛇头

??for?snake?in?snakes:

????rect(snake,?snake_color)

??rect(head,?head_color)

??rect(food,?food_color)

??#

??pygame.display.flip()

??#设置帧频(速度)

??clock.tick(8)

#收尾工作

这是一个简易版贪吃蛇的代码,虽然结构简单,但是该有的功能都是完整的,可玩性也不错

(责任编辑:IT教学网)

更多

推荐Frontpage教程文章