小游戏编程代码
❶ vb小游戏源代码
Dim pFist, cFist, cCount, pCount, dCount, tCount As Integer
Private Sub Form_Load()
cCount = 0
pCount = 0
dCount = 0
tCount = 1
lblPWinNum.Caption = ""
lblPWinNum.Caption = lblPWinNum.Caption & pCount
lblCWinNum.Caption = ""
lblCWinNum.Caption = lblCWinNum.Caption & cCount
lblDrawNum.Caption = ""
lblDrawNum.Caption = lblDrawNum.Caption & dCount
lblTotalNum.Caption = ""
lblTotalNum.Caption = lblTotalNum.Caption & tCount
End Sub
Private Sub imgCloth_Click()
pFist = 3
cFist = Int(Rnd * 3) + 1
txtPlayer.Text = ""
txtPlayer.Text = txtPlayer.Text & "布"
If cFist = 1 Then
txtComputer = ""
txtComputer = txtComputer & "石头"
Else
If cFist = 2 Then
txtComputer = ""
txtComputer = txtComputer & "剪刀"
Else
If cFist = 3 Then
txtComputer = ""
txtComputer = txtComputer & "布"
End If
End If
End If
If cFist = 1 Then
pCount = pCount + 1
lblPWinNum.Caption = ""
lblPWinNum.Caption = lblPWinNum.Caption & pCount
picSusess.Visible = True
MsgBox "恭喜你,你赢了!"
Else
If cFist = 2 Then
cCount = cCount + 1
lblCWinNum.Caption = ""
lblCWinNum.Caption = lblCWinNum.Caption & cCount
MsgBox "很遗憾,你输了!"
Else
dCount = dCount + 1
lblDrawNum.Caption = ""
lblDrawNum.Caption = lblDrawNum.Caption & dCount
MsgBox "打平了!"
End If
End If
tCount = tCount + 1
lblTotalNum.Caption = ""
lblTotalNum.Caption = lblTotalNum.Caption & tCount
End Sub
Private Sub imgScissors_Click()
pFist = 2
cFist = Int(Rnd * 3) + 1
txtPlayer.Text = ""
txtPlayer.Text = txtPlayer.Text & "剪刀"
If cFist = 1 Then
txtComputer = ""
txtComputer = txtComputer & "石头"
Else
If cFist = 2 Then
txtComputer = ""
txtComputer = txtComputer & "剪刀"
Else
If cFist = 3 Then
txtComputer = ""
txtComputer = txtComputer & "布"
End If
End If
End If
If cFist = 3 Then
pCount = pCount + 1
lblPWinNum.Caption = ""
lblPWinNum.Caption = lblPWinNum.Caption & pCount
picSusess.Visible = True
MsgBox "恭喜你,你赢了!"
Else
If cFist = 1 Then
cCount = cCount + 1
lblCWinNum.Caption = ""
lblCWinNum.Caption = lblCWinNum.Caption & cCount
MsgBox "很遗憾,你输了!"
Else
dCount = dCount + 1
lblDrawNum.Caption = ""
lblDrawNum.Caption = lblDrawNum.Caption & dCount
MsgBox "打平了!"
End If
End If
tCount = tCount + 1
lblTotalNum.Caption = ""
lblTotalNum.Caption = lblTotalNum.Caption & tCount
End Sub
Private Sub imgStone_Click()
pFist = 1
cFist = Int(Rnd * 3) + 1
txtPlayer.Text = ""
txtPlayer.Text = txtPlayer.Text & "石头"
If cFist = 1 Then
txtComputer = ""
txtComputer = txtComputer & "石头"
Else
If cFist = 2 Then
txtComputer = ""
txtComputer = txtComputer & "剪刀"
Else
If cFist = 3 Then
txtComputer = ""
txtComputer = txtComputer & "布"
End If
End If
End If
If cFist = 2 Then
pCount = pCount + 1
lblPWinNum.Caption = ""
lblPWinNum.Caption = lblPWinNum.Caption & pCount
picSusess.Visible = True
MsgBox "恭喜你,你赢了!"
Else
If cFist = 3 Then
cCount = cCount + 1
lblCWinNum.Caption = ""
lblCWinNum.Caption = lblCWinNum.Caption & cCount
MsgBox "很遗憾,你输了!"
Else
dCount = dCount + 1
lblDrawNum.Caption = ""
lblDrawNum.Caption = lblDrawNum.Caption & dCount
MsgBox "打平了!"
End If
End If
tCount = tCount + 1
lblTotalNum.Caption = ""
lblTotalNum.Caption = lblTotalNum.Caption & tCount
End Sub
Private Sub picSusess_Click()
picSusess.Visible = False
End Sub
我原来做的一个,按照你的提示加了一个图片,picSusess这个就是图片,里面放着一个苹果的图片,当你赢了就会出现,你点击一下,那个图片就消失,我这个里面除了你这个功能,还有战绩提示,统计你胜负平的局数和第多少局(计算公式是胜负平的局数+1,就是当前局数)
❷ c++编程小游戏代码
以下是贪吃蛇源代码:
#include<iostream.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#defineN21
voidgotoxy(intx,inty)//位置函数{
COORDpos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
voidcolor(inta)//颜色函数{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
voidinit(intapple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
inti,j;//初始化围墙
intwall[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;i<N+2;i++)
{
for(j=0;j<N+2;j++)
{
if(wall[i][j])
cout<<"■";
elsecout<<"□";
}
cout<<endl;
}
gotoxy(N+3,1);//显示信息
color(20);
cout<<"按WSAD移动方向"<<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;
}
intmain()
{
inti,j;
int**snake=NULL;
intapple[2];
intscore=0;
inttail[2];
intlen=3;
charch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;i<len;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;i>0;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);
cout<<score<<endl;
}
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;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return0;
}
❸ 求一个简单又有趣的JAVA小游戏代码
那你就自己做个猜数字好了
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("请输入一个100以内的整数:");
for (int i=0;i<=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shu>num)
System.out.println("输入的数大了,输小点的!");
else if (shu<num)
System.out.println("输入的数小了,输大点的!");
else {
System.out.println("恭喜你,猜对了!");
if (i<=2)
System.out.println("你真是个天才!");
else if (i<=6)
System.out.println("还将就,你过关了!");
else if (i<=8)
System.out.println("但是你还……真笨!");
else
System.out.println("你和猪没有两样了!");
break;}
}
}
}
❹ 用C语言编写的小游戏代码是什么
/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int head=3 ,tail=0;
int main()
{
int i,j,k=0;
int zuobiao[2][80];
long start;
int direction=77;
int gamespeed;
int timeover;
int change(char qipan[20][80],
int zuobiao[2][80],
char direction);
zuobiao[0][tail]=1;
zuobiao[1][tail]=1;
zuobiao[0][1]=1;
zuobiao[1][1]=2;zuobiao[0
[2]=1;
zuobiao[1][2]=3;
zuobiao[0][head]=1;
zuobiao[1][head]=4;
/*处理棋盘*/
char qipan[20][80];
//定义棋盘
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=' ';//初始化棋盘
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。
❺ 用C语言编写的小游戏代码是什么
/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++6.0、turbo…………)上都能运行,你还可以进一步改进。这是一个类似贪吃蛇的小游戏。祝你好运*/
/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int head=3 ,tail=0;
int main()
{
int i,j,k=0;
int zuobiao[2][80];
long start;
int direction=77;
int gamespeed;
int timeover;
int change(char qipan[20][80],int zuobiao[2][80],char direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*处理棋盘*/
char qipan[20][80];//定义棋盘
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=' ';//初始化棋盘
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");
printf("Input your game speed,please.(e.g.300)\n");
scanf("%d",&gamespeed);
while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//打印出棋盘
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return 0;
system("cls");
printf("GAME OVER!\n");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME OVER!\n");
}
}
return 0;
}
int change(char qipan[20][80],int zuobiao[2][80],char direction)
{
int x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return 0;
if(qipan[x][y]!=' ')
return 0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]=' ';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return 1;
}