當前位置:首頁 » 操作系統 » 拼圖游戲源碼

拼圖游戲源碼

發布時間: 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 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
linux下sql 發布:2024-10-12 17:19:34 瀏覽:114
搭建sql伺服器 發布:2024-10-12 17:11:25 瀏覽:823