導航:首頁 > 編程語言 > java游戲代碼

java游戲代碼

發布時間:2022-01-21 20:20:58

java小游戲簡單代碼 (兩個武士A、B對打)

變數和方法是不同的概念!!淺顯來講,變數是數據,所謂變數,就是其值能夠被改變的數據,常量與其對立!!而方法是對這些變數進行操作的定義。可以類比一下,球是變數,那麼打球就是方法!!用JAVA來舉個例子,int a=2;那麼a就是變數,int getA(){return a;}就是方法!!不知道這樣講你明白不??

㈡ 求java編寫數字游戲程序代碼

這個 是 DOS 界面的,下面還有一個applet的
import java.lang.Math;
import java.util.Scanner;

public class Test40 {
public static void main(String[] args) {
go();
}
private static void go() {//程序邏輯結構
int aa = (int) (Math.random() * 100); // create a number from 1 to 100
System.out.println("請輸入一個數字:");
Scanner sc = new Scanner(System.in);
int count=0;//計數器,統計猜測次數
int guessnum = 0;
while(true){
guessnum = sc.nextInt();
if (guessnum > aa) {
System.out.println("大了,請輸入個小點的數字!");
}
if (guessnum < aa) {
System.out.println("小了,請輸入個大點的數字!");
}
if (guessnum == aa) {
System.out.println("恭喜你,猜對了!您一共猜了"+count+"次");
System.out.println("是否繼續?Y/N");
String s=sc.next();
if("Y".equals(s)||"y".equals(s)){
System.out.println("程序已重新開始!");
go();
}else if("N".equals(s)||"n".equals(s)){
System.out.println("程序已關閉!");
System.exit(0);//退出程序
}
}
count++;
}
}
}

下面的是applet的

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Guess extends Applet implements ActionListener {
Panel p1, p2, p3, p4;
Button b1, b2;
Label l1, l2;
TextField t1;
TextArea t2;
int num;

public void init() {
setSize(230, 300);
setBackground(Color.pink);
l1 = new Label("這是一個猜數字程序(0-100)", Label.CENTER);
l2 = new Label("begin:");
t1 = new TextField(10);
t2 = new TextArea("", 10, 30);
b1 = new Button("猜測");
b2 = new Button("查看數字");

Random rand = new Random();
num = rand.nextInt(100);

add(l1);
add(l2);
add(t1);
add(t2);
add(b1);
add(b2);

b1.addActionListener(this);
b2.addActionListener(this);

}

public void actionPerformed(ActionEvent j) {
int n;
try {

if (j.getSource() == b1) {
String s;
s = String.valueOf(t1);
if (s.compareTo("1") < 0 || s.compareTo("9") > 0)
t2.setText("輸入有誤!\n請重新輸入!");

n = Integer.parseInt(t1.getText());
if (n < num)
t2.setText("小了,請輸入個大點的數字");

else if (n > num)
t2.setText("大了,請輸入個小點的數字");

else {
t2.setText("恭喜你,猜對了");
Random rand1 = new Random();
num = rand1.nextInt(100);
}

}
if (j.getSource() == b2) {
t2.setText("" + num);
}
} catch (Exception e) {
}
}
}

㈢ 幾個Java小游戲代碼

package rece;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Vector;
public class Frame extends JFrame implements Runnable {
JPanel contentPane;
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JSlider jSlider1 = new JSlider();
JLabel jLabel1 = new JLabel();
JButton jButton2 = new JButton();
JLabel jLabel2 = new JLabel();
int count = 1, rapidity = 80; // count 當前進行的個數, rapidity 游標的位置
int zhengque = 0, cuowu = 0;
int rush[] = { 10 ,20 ,30 }; //游戲每關的個數 可以自由添加.列 { 10 ,20 ,30 ,40,50}
int rush_count = 0; //記錄關數
char list[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; //隨機出現的數字 可以自由添加
Vector number = new Vector();
String paian = "true";
AudioClip Musci_anjian, Music_shi, Music_chenggong;
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);

//-----------------聲音文件---------------------
Musci_anjian = Applet.newAudioClip(new File("sounds//anjian.wav")
.toURL());
Music_shi = Applet.newAudioClip(new File("sounds//shi.wav")
.toURL());
Music_chenggong = Applet.newAudioClip(new File(
"sounds//chenggong.wav").toURL());

//---------------------------------------
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(588, 530));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 4, 573, 419));
jPanel1.setLayout(null);
jButton1.setBounds(new Rectangle(277, 442, 89, 31));
jButton1.setText("開始");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jSlider1.setBounds(new Rectangle(83, 448, 164, 21));
jSlider1.setMaximum(100);
jSlider1.setMinimum(1);
jSlider1.setValue(50);
jLabel1.setText("速度");
jLabel1.setBounds(new Rectangle(35, 451, 39, 18));
jButton2.setBounds(new Rectangle(408, 442, 89, 31));
jButton2.setText("結束");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jLabel2.setText("第一關:100個");
jLabel2.setBounds(new Rectangle(414, 473, 171, 21));
contentPane.add(jPanel1);
contentPane.add(jButton2);
contentPane.add(jButton1);
contentPane.add(jSlider1);
contentPane.add(jLabel1);
contentPane.add(jLabel2);
this.addKeyListener(new MyListener());
jButton1.addKeyListener(new MyListener());
jSlider1.addKeyListener(new MyListener());
jSlider1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
rapidity = jSlider1.getValue();
}
});
}
public void run() {
number.clear();
zhengque = 0;
cuowu = 0;
paian = "true";
while (count <= rush[rush_count]) {
try {
Thread t = new Thread(new Tthread());
t.start();
count += 1;
Thread.sleep(1000 + (int) (Math.random() * 2000)); // 生產下組停頓時間
// 最快1快.最慢2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (true) { // 等待最後一個字元消失
if (number.size() == 0) {
break;
}
}
if (zhengque == 0) { // 為了以後相除..如果全部正確或者錯誤就會出現錯誤. 所以..
zhengque = 1;
}
if (cuowu == 0) {
cuowu = 1;
}
if (paian.equals("true")) { // 判斷是否是自然結束
if (zhengque / cuowu >= 2) {
JOptionPane.showMessageDialog(null, "恭喜你過關了");
rush_count += 1; // 自動加1關
if (rush_count < rush.length) {
if (rapidity > 10) { // 當速度大於10的時候在-5提加速度.怕速度太快
rapidity -= 5; // 速度自動減10毫秒
jSlider1.setValue(rapidity); // 選擇位置
}
Thread t = new Thread(this);
t.start();
} else {
JOptionPane.showMessageDialog(null, "牛B...你通關了..");
rush_count = 0;
count = 0;
}
} else {
JOptionPane.showMessageDialog(null, "請再接再勵");
rush_count = 0;
count = 0;
}
} else {
rush_count = 0;
count = 0;
}
}
public void jButton1_actionPerformed(ActionEvent e) {
Thread t = new Thread(this);
t.start();
}
public void jButton2_actionPerformed(ActionEvent e) {
count = rush[rush_count] + 1;
paian = "flase";
}
class Tthread implements Runnable {
public void run() {
boolean fo = true;
int Y = 0, X = 0;
JLabel show = new JLabel();
show.setFont(new java.awt.Font("宋體", Font.PLAIN, 33));
jPanel1.add(show);
X = 10 + (int) (Math.random() * 400);
String parameter = list[(int) (Math.random() * list.length)] + "";
Bean bean = new Bean();
bean.setParameter(parameter);
bean.setShow(show);
number.add(bean);
show.setText(parameter);
while (fo) {
// ---------------------數字下移--------------------
show.setBounds(new Rectangle(X, Y += 2, 33, 33));
try {
Thread.sleep(rapidity);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Y >= 419) {
fo = false;
for (int i = number.size() - 1; i >= 0; i--) {
Bean bn = ((Bean) number.get(i));
if (parameter.equalsIgnoreCase(bn.getParameter())) {
cuowu += 1;
jLabel2.setText("正確:" + zhengque + "個,錯誤:" + cuowu
+ "個");
number.removeElementAt(i);
Music_shi.play();
break;
}
}
}
}
}
}
class MyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
String uu = e.getKeyChar() + "";
for (int i = 0; i < number.size(); i++) {
Bean bean = ((Bean) number.get(i));
if (uu.equalsIgnoreCase(bean.getParameter())) {
zhengque += 1;
number.removeElementAt(i);
bean.getShow().setVisible(false);
jLabel2.setText("正確:" + zhengque + "個,錯誤:" + cuowu + "個");
Music_chenggong.play();
break;
}
}
Musci_anjian.play();
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
Frame frame = new Frame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
}
class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton2_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton1_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Bean {
String parameter = null;
JLabel show = null;
public JLabel getShow() {
return show;
}
public void setShow(JLabel show) {
this.show = show;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
}
我只有一個打字母小游戲

㈣ 求JAVA小游戲的練習代碼

建議你加些這類的交流群 那裡面有很多同道中人 可以交流學習 望採納哦1

㈤ 求一個簡單又有趣的JAVA小游戲代碼

具體如下:

連連看的小源碼

package Lianliankan;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數組

JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕

JLabel fractionLable=new JLabel("0"); //分數標簽

JButton firstButton,secondButton; //

分別記錄兩次被選中的按鈕

int grid[][] = new int[8][7];//儲存游戲按鈕位置

static boolean pressInformation=false; //判斷是否有按鈕被選中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標

int i,j,k,n;//消除方法控制

代碼(code)是程序員用開發工具所支持的語言寫出來的源文件,是一組由字元、符號或信號碼元以離散形式表示信息的明確的規則體系。

對於字元和Unicode數據的位模式的定義,此模式代表特定字母、數字或符號(例如 0x20 代表一個空格,而 0x74 代表字元「t」)。一些數據類型每個字元使用一個位元組;每個位元組可以具有 256 個不同的位模式中的一個模式。

在計算機中,字元由不同的位模式(ON 或 OFF)表示。每個位元組有 8 位,這 8 位可以有 256 種不同的 ON 和 OFF 組合模式。對於使用 1 個位元組存儲每個字元的程序,通過給每個位模式指派字元可表示最多 256 個不同的字元。2 個位元組有 16 位,這 16 位可以有 65,536 種唯一的 ON 和 OFF 組合模式。使用 2 個位元組表示每個字元的程序可表示最多 65,536 個字元。

單位元組代碼頁是字元定義,這些字元映射到每個位元組可能有的 256 種位模式中的每一種。代碼頁定義大小寫字元、數字、符號以及 !、@、#、% 等特殊字元的位模式。每種歐洲語言(如德語和西班牙語)都有各自的單位元組代碼頁。

雖然用於表示 A 到 Z 拉丁字母表字元的位模式在所有的代碼頁中都相同,但用於表示重音字元(如"é"和"á")的位模式在不同的代碼頁中卻不同。如果在運行不同代碼頁的計算機間交換數據,必須將所有字元數據由發送計算機的代碼頁轉換為接收計算機的代碼頁。如果源數據中的擴展字元在接收計算機的代碼頁中未定義,那麼數據將丟失。

如果某個資料庫為來自許多不同國家的客戶端提供服務,則很難為該資料庫選擇這樣一種代碼頁,使其包括所有客戶端計算機所需的全部擴展字元。而且,在代碼頁間不停地轉換需要花費大量的處理時間。

㈥ 跪求java小項目,經典小游戲代碼

五子棋

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();

if(x < 25 || x > 330 + 25 ||y < 25 || y > 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i <= 330;i += 30)
{
for(int j = 30;j <= 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}

for(int j = 30;j <= 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}

}
void Drawchess(Graphics g)
{
for(int i = 0;i < 11;i++)
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;

for(i = 0;i < 11;i++)//橫向判斷
{
for(int j = 0;j < 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}

for(i = 0;i < 11;i++)//豎向判斷
{
for(int j = 0;j < 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}

for(i = 0;i < 7;i++)//左向右斜判斷
{
for(int j = 0;j < 7;j++)
{
for(int k = 0;k < 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}

for(i = 4;i < 11;i++)//右向左斜判斷
{
for(int j = 6;j >= 0;j--)
{
for(int k = 0;k < 5;k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋勝利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋勝利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}

}
void Clear_Chess()
{
for(int i=0;i<11;i++)
{
for(int j=0;j<11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}

}

class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle("單人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);

}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}

㈦ 求一個簡單的JAVA游戲代碼,100行左右,謝謝!

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Painter extends JFrame{
/**
*
*/
private static final long serialVersionUID = 8160427604782702376L;
CanvasPanel canvas = new CanvasPanel();;
public Painter() {
super("Star");
this.add(canvas);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Painter();
}
}
class CanvasPanel extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -464252885453878L;
private JButton[] btn = new JButton[4];
private String[] btn_name = {"+", "-", "R", "L"};
private int center_x = 200, center_y = 200, radius = 100, degree = 0;
public CanvasPanel() {
this.setPreferredSize(new Dimension(400, 500));
this.setLayout(null);
for(int i = 0; i < 4; i++) {
btn[i] = new JButton(btn_name[i]);
btn[i].setBounds(160 + i * 60, 425, 50, 50);
btn[i].addActionListener(this);
this.add(btn[i]);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i < 5; i++) {
g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),
(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),
(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand() == "+") {
if(radius < 200)
radius += 2;
repaint();
} else if(e.getActionCommand() == "-") {
if(radius > 0)
radius -= 2;
repaint();
} else if(e.getActionCommand() == "R") {
degree = (degree + 2) % 360;
repaint();
} else if(e.getActionCommand() == "L") {
degree = (degree - 2) % 360;
repaint();
}
}
}

㈧ 求java小游戲源代碼

[最佳答案] 連連看java源代碼 import javax.swing.*; import java.awt.*; import java.awt.event.*; pu... int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位...

㈨ 跪求游戲java源代碼

給你一個俄羅斯方塊的把!!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.swing.Timer;

public class Tetris extends JFrame {
public Tetris() {
Tetrisblok a = new Tetrisblok();
addKeyListener(a);
add(a);
}

public static void main(String[] args) {
Tetris frame = new Tetris();
JMenuBar menu = new JMenuBar();
frame.setJMenuBar(menu);
JMenu game = new JMenu("游戲");
JMenuItem newgame = game.add("新游戲");
JMenuItem pause = game.add("暫停");
JMenuItem goon = game.add("繼續");
JMenuItem exit = game.add("退出");
JMenu help = new JMenu("幫助");
JMenuItem about = help.add("關於");
menu.add(game);
menu.add(help);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220, 275);
frame.setTitle("Tetris內測版");
// frame.setUndecorated(true);
frame.setVisible(true);
frame.setResizable(false);

}
}

// 創建一個俄羅斯方塊類
class Tetrisblok extends JPanel implements KeyListener {

// blockType 代表方塊類型
// turnState代表方塊狀態
private int blockType;
private int score = 0;

private int turnState;

private int x;

private int y;

private int i = 0;

int j = 0;
int flag = 0;
// 定義已經放下的方塊x=0-11,y=0-21;
int[][] map = new int[13][23];

// 方塊的形狀 第一組代表方塊類型有S、Z、L、J、I、O、T 7種 第二組 代表旋轉幾次 第三四組為 方塊矩陣
private final int shapes[][][] = new int[][][] {
// i
{ { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } },
// s
{ { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 } },
// z
{ { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 } },
// j
{ { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
// o
{ { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
// l
{ { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
// t
{ { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } };

// 生成新方塊的方法
public void newblock() {
blockType = (int) (Math.random() * 1000) % 7;
turnState = (int) (Math.random() * 1000) % 4;
x = 4;
y = 0;
if (gameover(x, y) == 1) {

newmap();
drawwall();
score = 0;
JOptionPane.showMessageDialog(null, "GAME OVER");
}
}

// 畫圍牆
public void drawwall() {
for (i = 0; i < 12; i++) {
map[i][21] = 2;
}
for (j = 0; j < 22; j++) {
map[11][j] = 2;
map[0][j] = 2;
}
}

// 初始化地圖
public void newmap() {
for (i = 0; i < 12; i++) {
for (j = 0; j < 22; j++) {
map[i][j] = 0;
}
}
}

// 初始化構造方法
Tetrisblok() {
newblock();
newmap();
drawwall();
Timer timer = new Timer(1000, new TimerListener());
timer.start();
}

// 旋轉的方法
public void turn() {
int tempturnState = turnState;
turnState = (turnState + 1) % 4;
if (blow(x, y, blockType, turnState) == 1) {
}
if (blow(x, y, blockType, turnState) == 0) {
turnState = tempturnState;
}
repaint();
}

// 左移的方法
public void left() {
if (blow(x - 1, y, blockType, turnState) == 1) {
x = x - 1;
}
;
repaint();
}

// 右移的方法
public void right() {
if (blow(x + 1, y, blockType, turnState) == 1) {
x = x + 1;
}
;
repaint();
}

// 下落的方法
public void down() {
if (blow(x, y + 1, blockType, turnState) == 1) {
y = y + 1;
delline();
}
;
if (blow(x, y + 1, blockType, turnState) == 0) {
add(x, y, blockType, turnState);
newblock();
delline();
}
;
repaint();
}

// 是否合法的方法
public int blow(int x, int y, int blockType, int turnState) {
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x
+ b + 1][y + a] == 1))
|| ((shapes[blockType][turnState][a * 4 + b] == 1) && (map[x
+ b + 1][y + a] == 2))) {

return 0;
}
}
}
return 1;
}

// 消行的方法
public void delline() {
int c = 0;
for (int b = 0; b < 22; b++) {
for (int a = 0; a < 12; a++) {
if (map[a][b] == 1) {

c = c + 1;
if (c == 10) {
score += 10;
for (int d = b; d > 0; d--) {
for (int e = 0; e < 11; e++) {
map[e][d] = map[e][d - 1];

}
}
}
}
}
c = 0;
}
}

// 判斷你掛的方法
public int gameover(int x, int y) {
if (blow(x, y, blockType, turnState) == 0) {
return 1;
}
return 0;
}

// 把當前添加map
public void add(int x, int y, int blockType, int turnState) {
int j = 0;
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
if (map[x + b + 1][y + a] == 0) {
map[x + b + 1][y + a] = shapes[blockType][turnState][j];
}
;
j++;
}
}
}

// 畫方塊的的方法
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 畫當前方塊
for (j = 0; j < 16; j++) {
if (shapes[blockType][turnState][j] == 1) {
g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10);
}
}
// 畫已經固定的方塊
for (j = 0; j < 22; j++) {
for (i = 0; i < 12; i++) {
if (map[i][j] == 1) {
g.fillRect(i * 10, j * 10, 10, 10);

}
if (map[i][j] == 2) {
g.drawRect(i * 10, j * 10, 10, 10);

}
}
}
g.drawString("score=" + score, 125, 10);
g.drawString("抵制不良游戲,", 125, 50);
g.drawString("拒絕盜版游戲。", 125, 70);
g.drawString("注意自我保護,", 125, 90);
g.drawString("謹防受騙上當。", 125, 110);
g.drawString("適度游戲益腦,", 125, 130);
g.drawString("沉迷游戲傷身。", 125, 150);
g.drawString("合理安排時間,", 125, 170);
g.drawString("享受健康生活。", 125, 190);
}

// 鍵盤監聽
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_UP:
turn();
break;
case KeyEvent.VK_RIGHT:
right();
break;
case KeyEvent.VK_LEFT:
left();
break;
}

}

// 無用
public void keyReleased(KeyEvent e) {
}

// 無用
public void keyTyped(KeyEvent e) {
}

// 定時器監聽
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

repaint();
if (blow(x, y + 1, blockType, turnState) == 1) {
y = y + 1;
delline();
}
;
if (blow(x, y + 1, blockType, turnState) == 0) {

if (flag == 1) {
add(x, y, blockType, turnState);
delline();
newblock();
flag = 0;
}
flag = 1;
}
;
}
}
}

閱讀全文

與java游戲代碼相關的資料

熱點內容
壓縮因子定義 瀏覽:968
cd命令進不了c盤怎麼辦 瀏覽:214
葯業公司招程序員嗎 瀏覽:974
毛選pdf 瀏覽:659
linuxexecl函數 瀏覽:727
程序員異地戀結果 瀏覽:374
剖切的命令 瀏覽:229
干什麼可以賺錢開我的世界伺服器 瀏覽:290
php備案號 瀏覽:990
php視頻水印 瀏覽:167
怎麼追程序員的女生 瀏覽:487
空調外壓縮機電容 瀏覽:79
怎麼將安卓變成win 瀏覽:459
手機文件管理在哪兒新建文件夾 瀏覽:724
加密ts視頻怎麼合並 瀏覽:775
php如何寫app介面 瀏覽:804
宇宙的琴弦pdf 瀏覽:396
js項目提成計算器程序員 瀏覽:944
pdf光子 瀏覽:834
自拍軟體文件夾名稱大全 瀏覽:328