编程一个最简单游戏代码复制(好玩的代码复制)
怎么编程一个最简单游戏代码?
利用随机数猜大小,如下:
1、代码的第一行,是一个include语句。没有它我们的程序会编译不过。有了它就是告诉编译器在对代码进行编译之前,必须要包含程序需要的文件。这里的stdio.h就是我们需要的头文件。
2、代码第二行是一个main函数,这个main函数的返回值是一个int整型数据。刚开始学习编程的时候我们可以认为程序运行的时候是从main函数开始的。后续会专门给大家做一个介绍向大家说明在main函数之前还做了哪些事情。
3、每个函数都用一对“{}”进行包含,表示着函数体的开始和结束,当然后面说到控制语句的时候它还表示一段控制语句的开始和结束。
4、main函数中调用了一个printf函数。它是用来向控制台输出我们想要的内容。printf的函数定位格式为:int printf(constchar*format,...)。format中定义了输出内容和格式。
5、return函数执行完后。在退出函数体之前,会将函数进行返回。return后的内容根据函数返回值定义而定。在本段程序中返回的是整型数据0。
求一个VB小游戏编程,要有代码的。。
贪吃蛇
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim C As Long
If KeyCode = 27 Then End
If KeyCode = 32 Then
If Timer1.Enabled = True Then
Timer1.Enabled = False
Label1.Visible = True
Else
Timer1.Enabled = True
Label1.Visible = False
End If
End If
C = UBound(She)
If GFangXiang = True Then Exit Sub
Select Case KeyCode
Case 37
If She(C).F = 2 Then Exit Sub
She(C).F = 0
GFangXiang = True
Case 38
If She(C).F = 3 Then Exit Sub
She(C).F = 1
GFangXiang = True
Case 39
If She(C).F = 0 Then Exit Sub
She(C).F = 2
GFangXiang = True
Case 40
If She(C).F = 1 Then Exit Sub
She(C).F = 3
GFangXiang = True
End Select
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
Me.BackColor = HC000
Me.FillColor = 255
Me.FillStyle = 0
Me.ScaleWidth = 24
Me.ScaleHeight = 24
Me.WindowState = 2
Set Timer1 = Controls.Add("VB.Timer", "Timer1")
Set Label1 = Controls.Add("VB.Label", "Label1")
Label1.AutoSize = True
Label1.BackStyle = 0
Label1 = "暂停"
Label1.ForeColor = RGB(255, 255, 0)
Label1.FontSize = 50
ChuShiHua
End Sub
Private Sub Form_Resize()
On Error GoTo 1:
With Me
If .WindowState 1 Then
.Cls
.ScaleMode = 3
HWB = .ScaleHeight / .ScaleWidth
.ScaleWidth = 24
.ScaleHeight = 24
Label1.Move (Me.ScaleWidth - Label1.Width) / 2, (Me.ScaleHeight - Label1.Height) / 2
HuaTu
Me.Line (X, Y)-(X + 1, Y + 1), RGB(255, 255, 0), BF
End If
End With
1:
End Sub
Private Sub Timer1_Timer()
Dim C As Long, I As Long
On Error GoTo 2:
QingChu
C = UBound(She)
Select Case She(C).F
Case 0
If ZhuangTai(She(C).X - 1, She(C).Y) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X - 1
She(C).Y = She(C - 1).Y
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X - 1, She(C).Y) = 1 Then
GoTo 2:
End If
Case 1
If ZhuangTai(She(C).X, She(C).Y - 1) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X
She(C).Y = She(C - 1).Y - 1
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X, She(C).Y - 1) = 1 Then
GoTo 2:
End If
Case 2
If ZhuangTai(She(C).X + 1, She(C).Y) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X + 1
She(C).Y = She(C - 1).Y
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X + 1, She(C).Y) = 1 Then
GoTo 2:
End If
Case 3
If ZhuangTai(She(C).X, She(C).Y + 1) = 2 Then
C = C + 1
ReDim Preserve She(C)
She(C).F = She(C - 1).F
She(C).X = She(C - 1).X
She(C).Y = She(C - 1).Y + 1
ChanShengShiWu
GoTo 1:
ElseIf ZhuangTai(She(C).X, She(C).Y + 1) = 1 Then
GoTo 2:
End If
End Select
ZhuangTai(She(0).X, She(0).Y) = 0
For I = 0 To C
Select Case She(I).F
Case 0
She(I).X = She(I).X - 1
Case 1
She(I).Y = She(I).Y - 1
Case 2
She(I).X = She(I).X + 1
Case 3
She(I).Y = She(I).Y + 1
End Select
Next
TiaoZheng
1:
GFangXiang = False
ZhuangTai(She(C).X, She(C).Y) = 1
HuaTu
Exit Sub
2:
If MsgBox("游戏结束,点“是”重新开始游戏,点“否”", vbYesNo, "贪吃蛇") = vbYes Then
ChuShiHua
Else
End
End If
End Sub
Private Sub ChuShiHua()
Me.Cls
Timer1.Enabled = True
Timer1.Interval = 200
Erase ZhuangTai
ReDim She(2)
She(0).F = 2
She(0).X = 9
She(0).Y = 11
ZhuangTai(9, 11) = 1
She(1).F = 2
She(1).X = 10
She(1).Y = 11
ZhuangTai(10, 11) = 1
She(2).F = 2
She(2).X = 11
She(2).Y = 11
ZhuangTai(11, 11) = 1
HuaTu
ChanShengShiWu
End Sub
Private Sub QingChu()
Dim I As Long
For I = 0 To UBound(She)
Me.Line (She(I).X, She(I).Y)-(She(I).X + 1, She(I).Y + 1), Me.BackColor, BF
Next
End Sub
Private Sub HuaTu()
Dim I As Long
For I = 0 To UBound(She)
Me.Circle (She(I).X + 0.5, She(I).Y + 0.5), 0.49, RGB(255, 255, 0), , , HWB
Next
End Sub
Private Sub TiaoZheng()
Dim I As Long
For I = 0 To UBound(She) - 1
She(I).F = She(I + 1).F
Next
End Sub
Private Sub ChanShengShiWu()
Randomize Timer
1:
X = Int(Rnd * 24)
Y = Int(Rnd * 24)
If ZhuangTai(X, Y) 0 Then GoTo 1:
ZhuangTai(X, Y) = 2
Me.Line (X, Y)-(X + 1, Y + 1), RGB(255, 255, 0), BF
End Sub
直接复制到空代码窗口
用C语言编写的小游戏代码是什么?
“猜数字小游戏”,每个数字后按空格,最后按回车确认
#includestdio.h
#includestdlib.h
#includetime.h
int a[4],b[4];
int count=0;? //计算猜测次数
void csh( );? //初始化
void start( );? //开始游戏
int main( )
{ csh( );
start( );
}
void csh( )? //初始化
{ printf("\n\n???????? 猜? 数? 字? 小? 游? 戏\n\n");
printf(“? ??猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B.\n”);
}
void start( )? //开始游戏
{int m,n;? //m是完全猜对的个数,n是顺序不对的个数
while(1)
{srand((unsigned)time(NULL));? //初始化随机数发生器srand( )
while(1) { for(int i=0;i4;i++) a[i]=rand( )%10; ?//rand( )函数每次随机产生一个0-9的数
if( (a[3]!=a[2]a[3]!=a[1]a[3]!=a[0])
(a[2]!=a[1]a[2]!=a[0])a[1]!=a[0] ) break; }? //4个随机数各自不相等
printf("?? ?请依次输入4个一位整数:\n\n? ?");
while(1)
{for(int i=0;i4;i++) scanf(“%d”,b[i]);
printf("?? ?你输入的是:%d? %d? %d? %d ",b[0],b[1],b[2],b[3]);
m=0;n=0;
for(int i=0;i4;i++)
{for(int j=0;j4;j++)
{ if(b[i]==a[j]i==j)m=m+1; if(b[i]==a[j]i!=j)n=n+1; }
}
count=count+1;
printf("????? %dA? %dB?? 你试了%d次\n? ?",m,n,count);
if(m==4)break;
if(count==8){ count=0; break; }
}
printf("\n");
if(m==4)printf("???? 你猜对了(^-^)! 就是:%d %d %d %d\n",a[0],a[1],a[2],a[3]);
else printf("???? 你输了(T-T)!哈哈!应该是:%d %d %d %d\n",a[0],a[1],a[2],a[3]);
int z;
printf("???? (要继续吗?1或0)\n? ?");
scanf(“%d”,z);
if(z==0) break;
}
}
c++游戏编程,写一个游戏编程,简单点
#includeiostream
#includeiomanip
using?namespace?std;
const?int?X?=?21;?//棋盘行数
const?int?Y?=?21;?//棋盘列数
char?p[X][Y];??//定义棋盘
int?m=0;//定义临时点,保存输入坐标
int?n=0;
void?display()??//输出棋盘
{
for(int?i=0;?iX;?i++)
coutsetw(3)setfill('?')i;
coutendl;
for(int?i=1;?iY;?i++)
{
coutsetw(3)setfill('?')i;
for(int?j=1;jX;j++)
coutsetw(3)setfill('?')p[i][j];
coutendl;
}
}
void?black()??//黑方落子
{
cout"请黑方输入落子位置:\n"
"请输入落子的行数:";
cinm;
cout"请输入落子的列数:";
cinn;
if(m=0||m=X||n=Y||n=0)
{
cout"超出棋盘范围,请重新输入正确坐标!\n";
black();
}
else?if((p[m][n]==1)||p[m][n]==2)
{
cout"该点已有棋子,请重新选取落子点!\n";
black();
}
else
p[m][n]=1;?//黑方用1来表示
system("cls");
display();
}
void?red()??//红方落子
{
cout"请红方输入落子位置:\n"
"请输入落子的行数:";
cinm;
cout"请输入落子的列数:";
cinn;
if(m=X||m=0||n=0||n=Y)
{
cout"超出棋盘范围,请重新输入正确坐标!\n";
red();
}
else?if((p[m][n]==1)||p[m][n]==2)
{
cout"该点已有棋子,请重新选取落子点!\n";
red();
}
else
p[m][n]=2;?//红方用2来表示
system("cls");
display();
}
int?evalue()??//只需要判断落子点为中心的九点“米”字是否连续即可
{
int?k?=?0,r?=?0;
/*斜线判断*/
for(k=3;kX-2;k++)??//两条,其中的p[k][r]!='-'是排除空子的情况
{
for(r=3;rY-2;r++)
{
if(p[k][r]!='-'p[k-2][r-2]==p[k][r]p[k-1][r-1]==p[k][r]p[k+1][r+1]==p[k][r]p[k+2][r+2]==p[k][r])
return?1;
else?if(p[k][r]!='-'p[k+2][r-2]==p[k][r]p[k+1][r-1]==p[k][r]p[k-1][r+1]==p[k][r]p[k-2][r+2]==p[k][r])
return?1;
}
}
/*横线判断*/
for(k=1;kX;k++)??//p[k][r]!='-'是排除空子的情况
for(r=3;rY-2;r++)
if(p[k][r]!='-'p[k][r-2]==p[k][r]p[k][r-1]==p[k][r]p[k][r+1]==p[k][r]p[k][r+2]==p[k][r])
return?1;
/*竖线判断*/
for(k=3;kX-2;k++)??//p[k][r]!='-'是排除空子的情况
for(r=1;rY;r++)
if(p[k][r]!='-'p[k-2][r]==p[k][r]p[k-1][r]==p[k][r]p[k+1][r]==p[k][r]p[k+2][r]==p[k][r])
return?1;
????return?0;
}
int?main()
{
memset(p,'-',441);??//初始化为‘-’
cout"欢迎使用简易双人对战五子棋游戏\n"
"五子棋棋谱如下:\n";
display();
while(1)
{
red();
if(evalue())
{
cout"红方赢!\n";
break;
}
black();
if(evalue())
{
cout"黑方赢!\n";
break;
}
}
return?0;
}
人人对战五子棋
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;
}