当前位置:首页 » 操作系统 » 拼图游戏源码

拼图游戏源码

发布时间: 2024-10-12 16:52:22

1. 谁有能在vc++6.0上运行的c语言小游戏代码

最基础的贪吃蛇的代码

#include<stdio.h>
#include<windows.h>//基本型态定义。支援型仔明态定义函数。使用者界面函数 图形装置界面函数。
#include<conio.h> //用户通过按键盘产生的对应操作 (控制台)
#include<stdlib.h>
#include<time.h> //日期和时间头文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //数组的元素代表蛇的各个部位
char Sna_Hea_Dir = 'a';//记录蛇头的移动方向
int Sna_Hea_X, Sna_Hea_Y;//记录蛇头的位置
int Snake_Len = 3;//记录蛇的长度
clock_t Now_Time;//记录当前时间,以便自动移动
int Wait_Time ;//记录自动移动的时间间隔
int Eat_Apple = 1;//吃到苹果表示为1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //获取标准输出的句柄 <windows.h>
//句柄 :标志应用程序中的不同对象和同类对象中的不同的实例 方便操控,
void gotoxy(int x, int y)//设置光标位置
{
COORD pos = {x,y}; //定义一个字符在控制台屏幕上的坐标POS

SetConsoleCursorPosition(hConsole, pos); //定位光标位置的函数<windows.h>

}

void Hide_Cursor()//隐藏光标 固定函数
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, &cursor_info);
}

void SetColor(int color)//设置颜色
{
SetConsoleTextAttribute(hConsole, color);
//是API设置字体颜色和背景色的函数 格式:SetConsoleTextAttribute(句柄,颜色);
}

void Print_Snake()//打印蛇头和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{

if(Snake[ix][iy] == 1)//蛇头
{
SetColor(0xf); //oxf代庆搏表分配的内存地址 setcolor:34行自定义设置颜色的函数
gotoxy(ix*2, iy);
printf("※");
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函数是产生随机念差告数的一个随机函数。C语言里还有 srand()函数等。
//头文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1; //变色
SetColor(color);
gotoxy(ix*2, iy);
printf("■");
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf("≈");
}
}
}

void Clear_Snake()//擦除贪吃蛇
{
int iy, ix;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(" ");
}
}

void Rand_Apple()//随机产生苹果
{
int ix, iy;

do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);

Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf("⊙");
Eat_Apple = 0;
}

void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf("Game Over");
Sleep(3000);
system("pause > nul");
exit(0);
}

void Move_Snake()//让蛇动起来
{
int ix, iy;

for(ix = 0; ix < LEN; ++ix)//先标记蛇头
for(iy = 0; iy < WID; ++iy)
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根据新的蛇头方向标志蛇头
{
case 'w':
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy - 1;
Sna_Hea_X = ix;

break;
case 's':
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;

break;
case 'a':
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix - 1;
Sna_Hea_Y = iy;

break;
case 'd':
if(ix == (LEN - 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;

break;
default:
break;
}
}

if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();

if(Snake[Sna_Hea_X][Sna_Hea_Y] < 0)//吃到苹果
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix < LEN; ++ix)//处理蛇尾
for(iy = 0; iy < WID; ++iy)
{
if(Snake[ix][iy] > 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}

Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//处理蛇头
}

void Get_Input()//控制蛇的移动方向
{
if(kbhit())
{
switch(getch())
{
case 87:

Sna_Hea_Dir = 'w';
break;
case 83:

Sna_Hea_Dir = 's';
break;
case 65:

Sna_Hea_Dir = 'a';
break;
case 68:

Sna_Hea_Dir = 'd';
break;
default:
break;
}
}

if(clock() - Now_Time >= Wait_Time)//蛇到时间自动行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}

void Init()//初始化
{
system("title 贪吃毛毛蛇");
system("mode con: cols=80 lines=25");
Hide_Cursor();

gotoxy(61, 4);
printf("You Score:");
gotoxy(61, 6);
printf("You Level:");
gotoxy(61, 8);
printf("The Lenght:");
gotoxy(61, 10);
printf("The Speed:");
gotoxy(61, 12);
printf("Apple Num:");

int i;
for(i = 0; i < Snake_Len; ++i)//生成蛇
Snake[10+i][15] = i+1;

int iy, ix;//打印蛇
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]);
gotoxy(ix*2, iy);
printf("■");
}
}
}

void Pri_News()//打印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf("%3d", All_Score);
gotoxy(73, 6);
printf("%3d", Level);
gotoxy(73, 8);
printf("%3d",Snake_Len);
gotoxy(73, 10);
printf("0.%3ds", Wait_Time/10);
gotoxy(73, 12);
printf("%d", Apple_Num);
}

void Lev_Sys()//等级系统
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time > 50)
Wait_Time -= 50;
else
if(Wait_Time > 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}

int main(void)
{
Init();
srand((unsigned)time(NULL));//设置随机数的种子
Now_Time = clock();
int speed1=1000,speed2,a;
printf("\n");
printf("请输入你想要的速度\n");
scanf("%d",&speed2);
Level=1;
Wait_Time=speed1-speed2;
printf("请输入你想要的苹果数\n");
scanf("%d",&a);

while(a--)
Rand_Apple();
while(1)
{
if(Eat_Apple)
{
++Apple_Num;
Rand_Apple();
Lev_Sys();
Pri_News();
}
Get_Input();
Sleep(10);
}
return 0;
}

2. 求C语言小游戏源程序

新手要方便写代码,可以收藏下面几个自编函数:

  1. gtxy (6, 3) //光标定位于窗口的第6列,第3行处(准备输出,行与列都是从0算起)

  2. Color (4, 0) //设置为红字配黑底 如 Color (10, 0)则是淡绿字配黑底

  3. yinc (1,0) //隐藏光标(第二个参数设为0就隐藏,没有光标闪烁,yinc代表隐藏)

  4. kou(80,25) //设定窗口缓冲区大小为80列,25行

    下面几个是库函数,不需自己编写,只要用#include包含就可以使用。

  5. SetConsoleTitle("俄罗斯方块"); //设置窗口左上角标题栏处出现"俄罗斯方块"5个字

  6. srand( (unsigned) time(NULL) ); //初始化随机数发生器

  7. n= rand( ) % 20; //产生随机数0-19中的一个. 如 rand( )%5 就产生0-4中的一个数

    SetConsoleTitle( )函数在<windows.h>里,srand( )函数与rand( )函数要配合用,

    就是同时要用,在<stdlib.h>里。如果 rand( )%10+1 就产生1-10之中的一个数。

  8. Sleep(300); //延时300毫秒(就是程序暂停300毫秒后继续运行)

  9. system("cls"); //清屏(把窗口里的内容全部清除,光标定于(0,0)位置处)

    这两个函数都在<windows.h>里。开头4个自编函数 编写如下:

void gtxy (int x, int y) //控制光标位置的函数

{ COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );

}

void Color (short ForeColor= 7, short BackGroundColor= 0) //设定颜色的函数

{ HANDLE hl = GetStdHandle ( STD_OUTPUT_HANDLE );

SetConsoleTextAttribute ( hl, ForeColor + BackGroundColor * 0x10 );

}

声明时原型可写 void Color (short x, short y);

void yinc (int x,int y) //隐藏光标的函数

{ CONSOLE_CURSOR_INFO gb={ x , y }; //gb代表光标

SetConsoleCursorInfo ( GetStdHandle(STD_OUTPUT_HANDLE), &gb );

}

void kou(int w,int h) //设置窗口大小的函数

{HANDLE hl=GetStdHandle ( STD_OUTPUT_HANDLE ) ;

COORD size={ w , h };

SetConsoleScreenBufferSize( hl , size );

SMALL_RECT rc={ 0, 0, w, h };

SetConsoleWindowInfo( hl, 1, &rc );

}

最后这个函数,参数w是宽h是高。里边5行中第一行定义了句柄型变量hl,并给它赋值。

第二行定义了坐标型结构体变量size,它的取值决定了缓冲区的大小。第三行就是使用

size的值设置好缓冲区大小。第四行定义了变量rc,它的值决定当前窗口显示的位置与

大小(不得超过缓冲区的大小)。前两个0,0是从缓冲区左上角0列0行位置处开始,后两

个参数可以小于w和h.比如rc={0,0,w-10,h-5}; 最后一行使用rc的值设置好窗口,中间

那个参数要为" 1 "或写“ true ”才有效。

3. C语言源代码是什么

数字版“拼图”游戏C源代码:

#include<time.h>

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<windows.h>

int i, j, r, k; //i、j、r用于循环, k存放随机数值


int m, n; // m、n是当前空位的下标, t标记排序是否成功

int a[4][4]; //存储4×4共16个数字的数组

void show(void); //输出数组表格

void csh(void); //初始化界面

int yes(void); //判断排序是否成功

void up(void); //数字向上移动到空位(空位则下移)

void down(void); //数字向下移

void left(void); //数字向左移

void rght(void); //数字向右移

void inkey(void); //按键操作

void gtxy(int x, int y) ; //控制光标移动的函数

int main(void)

{ while(1)

{csh( );

while(1)

{ inkey();

show();

if ( yes( ) )

{gtxy(6,12); printf("你成功了! 再来一局y/n?"); break;}

}

if(getch( )== ʹnʹ)break;

}

return 0;

}

void csh(void)

{r=0;

CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<4;i++) //给数组a依序赋值

for(j=0;j<4;j++)
{ if (i==3 && j==3) a[i][j]=0;
else a[i][j]=1+r++;
}

a[3][3]=a[1][1]; a[1][1]=0; //把a[3][3]与a[1][1]的值交换一下

m=1; n=1;

srand((unsigned)time(0)); //初始化随机数发生器

for(r=0;r<500;r++) //将数组各值打乱
{k=rand( )%(4); //取0-3随机数,分别代表上下左右四个方向
switch(k)
{ case 0: { up( );break; }
case 1: {down( );break; }
case 2: {left( );break; }
case 3: {rght( ); break; }
}
}

printf(" 数字拼图");

printf(" ┌──────┬──────┬──────┬──────┐");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" └──────┴──────┴──────┴──────┘");
show( );
}

void show(void)

{for(i=0;i<4;i++)

for(j=0;j<4;j++) //gtxy(7*j+9, 2*i+4)是光标到指定位置输出数字

{gtxy(7*j+9,2*i+4); if(a[i][j]==0)printf(" │");

else if(a[i][j]>9)printf(" %d │",a[i][j]);

else printf(" %d │",a[i][j]);

}

}

void inkey(void)

{ int key;

key=getch( );
switch(key)
{ case 72: { up( ); break;}
case 80: {down( ); break; }
case 75: {left( ); break; }
case 77: {rght( );break;}
}
}

void up(void)

{ if (m!=3) //移动时要考虑空位"0"是否已经在边界
{ a[m][n]=a[m+1][n]; m++; a[m][n]=0; }
}


void down(void)

{ if (m!=0)
{a[m][n]=a[m-1][n]; m--; a[m][n]=0; }
}

void left(void)

{ if (n!=3)
{ a[m][n]=a[m][n+1]; n++; a[m][n]=0;}
}
void rght(void)

{ if (n!=0)
{ a[m][n]=a[m][n-1]; n--; a[m][n]=0; }
}

int yes(void)

{ r=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{ if (a[i][j]!=1+r++) return (r==16)?1:0; }
}

void gtxy(int x, int y) //控制光标移动的函数

{ COORD coord;

coord.X = x;

coord.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

4. 1200分跪求java数字拼图游戏源代码!

1:
import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
// 华容道原理的拼图游戏。 利用轻组建的套用。
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MyMainFrame extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
MyCanvas myCanvas;
JPanel panelNorth,panelPreview;
Button start,preview,set;
Container container;

public MyMainFrame() {//初使化
container=this.getContentPane();
start=new Button("开始");
start.addActionListener(this);

preview=new Button("预览");
preview.addActionListener(this);
set = new Button("设置");
set.addActionListener(this);

panelPreview=new JPanel();
panelPreview.setLayout(null);
Icon icon=new ImageIcon ("images/pic_"+MyCanvas.pictureID+".jpg");
JLabel label=new JLabel(icon);
label.setBounds(0,0,400,400);
panelPreview.add(label);

panelNorth=new JPanel();
panelNorth.setBackground(Color.yellow);
panelNorth.add(start);
panelNorth.add(preview);
panelNorth.add(set);
myCanvas=new MyCanvas();
container.add(myCanvas,BorderLayout.CENTER);
container.add(panelNorth,BorderLayout.NORTH);
this.setTitle("成型拼图小游戏-1212");
this.setLocation(300,200);
this.setSize(408,465);
this.setResizable(false);
this.setVisible(true);

this.setDefaultCloseOperation(3);
} //end of 初始化 构造函数

public void actionPerformed(ActionEvent e) {

Button button=(Button)e.getSource();
if(button==start){
myCanvas.Start();

}else if(button==preview){
if(button.getLabel()=="预览"){
container.remove(myCanvas);
container.add(panelPreview);
panelPreview.updateUI();
container.repaint();
button.setLabel("返回");
}else{
container.remove(panelPreview);
container.add(myCanvas);
container.repaint();
button.setLabel("预览");
}
}else if(button==set){
Choice pic = new Choice();
//pic.add("QQ");
pic.add("美女");
int i=JOptionPane.showConfirmDialog(this,pic,"选择图片", JOptionPane.OK_CANCEL_OPTION);
//使用选择对话框来进行选择图片。
if(i==JOptionPane.YES_OPTION){
MyCanvas.pictureID=pic.getSelectedIndex()+5;
myCanvas.reLoadPictrue();
Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");
JLabel label=new JLabel(icon);
label.setBounds(0,0,400,400);
panelPreview.removeAll();
panelPreview.add(label);
panelPreview.repaint();
}
}
}

public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException
{
new MyMainFrame();

} //end of main

}
2:
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyCanvas extends JPanel implements MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件
Cell cell[];//定义方格
Rectangle cellNull;//定义空方格区域 是一个矩形类
public static int pictureID=4;// 当前选择的图片代号
public MyCanvas() {
this.setLayout(null);
this.setSize(400,400);
cellNull=new Rectangle(300,300,100,100);//空方格区域在第三行每三列
cell=new Cell[16];
Icon icon;
for (int i = 0; i < 4; i++) {
for(int j=0;j<4;j++){
icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");
cell[i*4+j]=new Cell(icon);
cell[i*4+j].setLocation(j*100,i*100);
this.add(cell[i*4+j]);
}
}
this.remove(cell[15]);//移除最后一个多余的方格
} //放置9张小图片并且移调最后一张

public void reLoadPictrue(){//当选择其它图形进行拼图时,需重新加载新图片
Icon icon;
for (int i = 0; i < 4; i++) {
for(int j=0;j<4;j++){
icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");
cell[i*4+j].setIcon(icon);
}
}
}
public boolean isFinish(){//判断是否拼合成功
for(int i=0;i<15;i++)
{ int x=cell[i].getBounds().x;
int y=cell[i].getBounds().y;
if(y/100*4+x/100!=i)
return false;
} //end of for
return true;
}

public void Start(){//对方格进行重新排列,打乱顺序

while(cell[0].getBounds().x<=100&&cell[0].getBounds().y<=100){//当第一个方格距左上角较近时
int x=cellNull.getBounds().x;
int y=cellNull.getBounds().y;
int direction=(int)(Math.random()*4);//产生0-4,对应空方格的上下左右移动
if(direction==0){//空方格左移动,与左侧方格互换位置,左侧方格右移动
x-=100;
if(test(x,y)){
for(int j=0;j<15;j++){
if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){//依次寻找左侧的按钮
cell[j].move("RIGHT",100);
cellNull.setLocation(x,y);
break;//找到后跳出for循环
}
}
}
}else if(direction==1){//RIGHT
x+=100;
if(test(x,y)){
for(int j=0;j<15;j++){
if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){
cell[j].move("LEFT",100);
cellNull.setLocation(x,y);
break;
}
}
}
}else if(direction==2){//UP
y-=100;
if(test(x,y)){
for(int j=0;j<15;j++){
if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){
cell[j].move("DOWN",100);
cellNull.setLocation(x,y);
break;
}
}
}
}else{//DOWN
y+=100;
if(test(x,y)){
for(int j=0;j<15;j++){
if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){
cell[j].move("UP",100);
cellNull.setLocation(x,y);
break;
}
}
}
}

}

if(!hasAddActionListener)//如果尚未添加动作事件,则添加
for(int i=0;i<15;i++)//为第个方格添加动作事件,这样单击按钮就能移动了
cell[i].addMouseListener(this);
hasAddActionListener=true;
}
private boolean test(int x,int y){
if((x>=0&&x<=200)||(y>=0&&y<=200))
return true;
else
return false;
}

public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
//方格的鼠标事件,因为用到了MyCanvas中的一些方法,因此没有在Cell类中处理鼠标事件
Cell button=(Cell)e.getSource();
int x1=button.getBounds().x;//得到所单击方格的坐标
int y1=button.getBounds().y;

int x2=cellNull.getBounds().x;//得到空方格的坐标
int y2=cellNull.getBounds().y;

if(x1==x2&&y1-y2==100)//进行比较,如果满足条件则进行交换
button.move("UP",100);
else if(x1==x2&&y1-y2==-100)
button.move("DOWN",100);
else if(x1-x2==100&y1==y2)
button.move("LEFT",100);
else if(x1-x2==-100&&y1==y2)
button.move("RIGHT",100);
else
return;//不满足就不进行任何处理

cellNull.setLocation(x1,y1);
this.repaint();
if(this.isFinish()){//进行是否完成的判断
JOptionPane.showMessageDialog(this,"景锋恭喜你完成拼图,加油!想继续下一关么?");
for(int i=0;i<15;i++)
cell[i].removeMouseListener(this);//如果已完成,撤消鼠标事件,鼠标单击方格不在起作用
hasAddActionListener=false;
}
}

}
3:
import javax.swing.Icon;
import javax.swing.JButton;

public class Cell extends JButton {
/**
*
*/
private static final long serialVersionUID = 1L;

Cell(Icon icon){//实际为ICON
super(icon);
this.setSize(100,100);
}

public void move(String direction,int sleep){//方格的移动
if(direction=="UP"){
this.setLocation(this.getBounds().x,this.getBounds().y-100);
}else if(direction=="DOWN"){
this.setLocation(this.getBounds().x,this.getBounds().y+100);
}else if(direction=="LEFT"){
this.setLocation(this.getBounds().x-100,this.getBounds().y);
}else{
this.setLocation(this.getBounds().x+100,this.getBounds().y);
}
}

}

5. java 源代码注释

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class GameTest extends JFrame implements ActionListener{
/*
* 新建一个主面板(这个类可能是自定义的,本程序和API中没有)。
*/
MainPanel j=new MainPanel();
JButton jPreview;
JLabel label;
Container container;
JPanel panel;
/**
* 主函数
* @param args
*/
public static void main(String[] args) {
//运行程序
new GameTest();
}
/**
* 构造函数。
*
*/
public GameTest()
{
//新建一个标题为“拼图”的窗口
JFrame fr =new JFrame("拼图");
//获取窗口容器。
container=fr.getContentPane();
//创建菜单条
JMenuBar jMenuBar=new JMenuBar();
//以下初始化菜单,并且设置快捷键和添加监听器。
JMenu jMenuGame=new JMenu("游戏(G)");
jMenuGame.setMnemonic('g');

JMenuItem jMenuItemStart = new JMenuItem("开始(S)");
jMenuItemStart.setMnemonic('s');
jMenuItemStart.addActionListener(this);

JMenuItem jMenuItemExit=new JMenuItem("退出(E)");
jMenuItemExit.setMnemonic('e');
jMenuItemExit.addActionListener(this);

jMenuGame.add(jMenuItemStart);
jMenuGame.add(jMenuItemExit);
//初始化按钮并设置快捷键和添加监听器
JButton jChoice=new JButton("选图(X)");
jChoice.setMnemonic('x');
jChoice.addActionListener(this);

jPreview=new JButton("预览(P)");
jPreview.setMnemonic('p');
jPreview.addActionListener(this);
//将菜单和按钮添加到菜单条中
jMenuBar.add(jMenuGame);
jMenuBar.add(jChoice);
jMenuBar.add(jPreview);
//将菜单条设为该窗口的主菜单
fr.setJMenuBar(jMenuBar);

//将主面板添加到该窗口的容器中。
container.add(j);
//设置大小
fr.setSize(315,360 );
fr.setVisible(true);
//设置默认关闭方式。
fr.setDefaultCloseOperation(3);

}
/**
* 事件处理函数。
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="开始(S)")
{
j.Start();
}
if(e.getActionCommand()=="预览(P)")
{
j.setVisible(false);
panel=new JPanel();
Icon icon=new ImageIcon("pictrue/pic"+"_"+MainPanel.pictureID+".jpg");
label=new JLabel(icon);
label.setBounds(300, 300, 0, 0);
panel.add(label);
panel.setSize(300, 300);
panel.setVisible(true);
this.container.add(panel);
jPreview.setText("返回(P)");
}
if(e.getActionCommand()=="返回(P)")
{
panel.setVisible(false);
j.setVisible(true);
j.repaint();
jPreview.setText("预览(P)");
}
if(e.getActionCommand()=="退出(E)")
{
System.exit(0);
}

if(e.getActionCommand()=="选图(X)")
{
//初始化选择框,并提供选择。
Choice pic = new Choice();
pic.add("七里香");
pic.add("依然范特西");
pic.add("八度空间");
pic.add("十一月的肖邦");
pic.add("魔杰座");
pic.add("叶惠美");
pic.add("我很忙");
int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION);
if(i==JOptionPane.YES_OPTION)
{
//选择图片
MainPanel.pictureID=pic.getSelectedIndex()+1;
j.removeAll();
j.reLoadPicture();
j.repaint();
}
}
}

}

6. 求各种各样的小游戏的源代码,比如:贪吃蛇、推箱子、俄罗斯方块、五子棋等,最好是.NET的,JAVA也行。

我有java的,你可以看看:一个拼图
import java.lang.Math.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class MainFrame extends JFrame implements ActionListener{ //定义整个框架
private JButton[] jb = new JButton[8];
private JButton jbs = new JButton("开 局");
private JButton jbres = new JButton("重新开始");
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private int[] n = new int[9];
private int[] n1 = new int[9];
private int position = 8,p,q;
private boolean bl,startbl=false;
private JLabel jl = new JLabel();
private int count = 0;
private JLabel jl1 = new JLabel(" "+Integer.toString(0));

public MainFrame(){ //框架的构造方法
int i;
for(int j = 0; j < n.length; j++){
n[j] = j;
n1[j] = n[j];
}
for(i = 0; i < jb.length; i++){ //给每个按钮赋相应的值,并注监听器
jb[i] = new JButton(Integer.toString(i+1));
jb[i].setFont(new Font("宋体",Font.BOLD,48));
jp2.add(jb[i]);
jb[i].addActionListener(this);
}
for(i = 0; i < n.length; i++){
if(n[i] == position)
jp2.add(jl);
else
jp2.add(jb[n[i]]);
}
jp2.setLayout(new GridLayout(3,3));//注册监听器
jbs.addActionListener(this);
jbres.addActionListener(this);
jp1.add(jbs);
jp1.add(jbres);
jp1.add(jl1);
jp1.setLayout(new FlowLayout()); //将jp1设置为流布局
setLayout(new BorderLayout()); //整体布局为边界布局
this.add("North",jp1);
this.add("Center",jp2);
this.setTitle("拼图游戏");
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //实现关闭按钮
this.setResizable(false);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e){ //实现按钮的事件

if(e.getSource()==jbres){ // 重新开始按钮事件
for(int j = 0; j<n.length;j++)
n[j] = n1[j];
reShow();
startbl=true;
count = 0;
jl1.setText(" "+Integer.toString(0));
}

else if(e.getSource()==jbs) //开局按钮事件
this.Init();
else if(startbl){ //按钮1-8移动事件
for(int i = 0; i < jb.length; i++)
if(e.getSource() == jb[i]){
//System.out.println(i+1);
for(int a=0;a<n.length;a++){
if(n[a]==i)
p=a;
if(n[a]==position)
q=a;
}
}

if(p != 0 && p != 1 && p != 2)
if((p-3) == q)
swap(p,q);

if(p != 0 && p != 3 && p != 6)
if((p-1) == q)
swap(p,q);

if(p != 2 && p != 5 && p != 8)
if((p+1) == q)
swap(p,q);

if(p != 6 && p != 7 && p != 8)
if((p+3) == q)
swap(p,q);

}
}

public void swap(int x,int y){ //按钮1-8与空白图片交换
int z;
z = n[x];
n[x] = n[y];
n[y]=z;
jl1.setText(" "+Integer.toString(++count));
reShow();
win();

}

public void Init(){ //随机产生游戏界面
int i=0,j,x;
boolean bl ;
while(i<9){
bl = true;
x=(int)(Math.random()*9);
for(j=0;j<i;j++)
if(n[j] == x)
bl=false;
if(bl){
n [i++] = x;
n1[i-1] = x;
}
}
reShow();
startbl=true;
count = 0;
jl1.setText(" "+Integer.toString(0));

}

public void reShow(){ //对游戏界面的重写
for(int i = 0; i < n.length; i++){
if(n[i] == position)
jp2.add(jl);
else

jp2.add(jb[n[i]]);
}
jp2.revalidate();
}

public void win(){ //判断是否成功
boolean winbl=true;
for(int i=0;i<n.length;i++)
if(n[i]!=i)
winbl=false;
if(winbl){
JOptionPane.showMessageDialog(this,"祝贺你,你成功了! "+"你用了"+Integer.toString(count)+"步","",JOptionPane.INFORMATION_MESSAGE);
startbl=false;
}
}

}

public class Collage { // 主函数类
public static void main(String[] args){
new MainFrame();
}
}
自已以前编的,不是很好,你就参考参考吧

热点内容
保险怎么配置合适 发布:2024-10-12 19:17:40 浏览:986
o2o生活源码 发布:2024-10-12 19:14:40 浏览:783
电脑硬件配置是什么 发布:2024-10-12 18:33:58 浏览:257
菏泽科二预约密码是多少 发布:2024-10-12 18:33:55 浏览:67
找零点C语言 发布:2024-10-12 18:33:42 浏览:190
快手怎么上传gif 发布:2024-10-12 18:15:02 浏览:513
ctr算法 发布:2024-10-12 18:13:32 浏览:246
如何创建服务器账号 发布:2024-10-12 18:13:19 浏览:724
物理存储是指闪存吗 发布:2024-10-12 18:00:21 浏览:542
怎么看bcg是否配置到vs里 发布:2024-10-12 17:53:54 浏览:732