当前位置:首页 > 黑客教程 > 正文内容

java游戏编程小游戏代码(java游戏程序代码)

hacker2年前 (2022-07-01)黑客教程79

本文目录一览:

JAVA小游戏程序代码

这个是比较有名的那个烟花,不知道你有没有用:

建个工程,以Fireworks为类即可

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

public class Fireworks extends Applet implements MouseListener,Runnable

{

int x,y;

int top,point;

/**

*对小程序进行变量和颜色的初始化。

*/

public void init()

{

x = 0;

y = 0;

//设置背景色为黑色

setBackground(Color.black);

addMouseListener(this);

}

public void paint(Graphics g)

{

}

/**

*使该程序可以作为应用程序运行。

*/

public static void main(String args[]) {

Fireworks applet = new Fireworks();

JFrame frame = new JFrame("TextAreaNew");

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

frame.getContentPane().add(

applet, BorderLayout.CENTER);

frame.setSize(800,400);

applet.init();

applet.start();

frame.setVisible(true);

}

/**

*程序主线程,对一个烟花进行绘制。

*/

public void run()

{

//变量初始化

Graphics g1;

g1 = getGraphics();

int y_move,y_click,x_click;

int v;

x_click = x;

y_click = y;

y_move = 400;

v = 3;

int r,g,b;

while(y_move y_click)

{

g1.setColor(Color.black);

g1.fillOval(x_click,y_move,5,5);

y_move -= 5;

r = (((int)Math.round(Math.random()*4321))%200)+55;

g = (((int)Math.round(Math.random()*4321))%200)+55;

b = (((int)Math.round(Math.random()*4321))%200)+55;

g1.setColor(new Color(r,g,b));

g1.fillOval(x_click,y_move,5,5);

for(int j = 0 ;j=10;j++)

{

if(r55) r -= 20;

if(g55) g -= 20;

if(b55) b -=20;

g1.setColor(new Color(r,g,b));

g1.fillOval(x_click,y_move+j*5,5,5);

}

g1.setColor(Color.black);

g1.fillOval(x_click,y_move+5*10,5,5);

try

{

Thread.currentThread().sleep(v++);

} catch (InterruptedException e) {}

}

for(int j=12;j=0;j--)

{

g1.setColor(Color.black);

g1.fillOval(x_click,y_move+(j*5),5,5);

try

{

Thread.currentThread().sleep((v++)/3);

} catch (InterruptedException e) {}

}

y_move = 400;

g1.setColor(Color.black);

while(y_move y_click)

{

g1.fillOval(x_click-2,y_move,9,5);

y_move -= 5;

}

v = 15;

for(int i=0;i=25;i++)

{

r = (((int)Math.round(Math.random()*4321))%200)+55;

g = (((int)Math.round(Math.random()*4321))%200)+55;

b = (((int)Math.round(Math.random()*4321))%200)+55;

g1.setColor(new Color(r,g,b));

g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);

if(i23)

{

g1.drawOval(x_click-3*(i+1),y_click-3*(i+1),6*(i+1),6*(i+1));

g1.drawOval(x_click-3*(i+2),y_click-3*(i+2),6*(i+2),6*(i+2));

}

try

{

Thread.currentThread().sleep(v++);

} catch (InterruptedException e) {}

g1.setColor(Color.black);

g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);

}

}

/**

*对鼠标事件进行监听。

*临听其鼠标按下事件。

*当按下鼠标时,产生一个新线程。

*/

public void mousePressed(MouseEvent e)

{

x = e.getX();

y = e.getY();

Thread one;

one = new Thread(this);

one.start();

one = null;

}

/**

*实现MouseListener接中的方法。为一个空方法。

*/

public void mouseReleased(MouseEvent e)

{

}

/**

*实现MouseListener接中的方法。为一个空方法。

*/

public void mouseEntered(MouseEvent e)

{

}

/**

*实现MouseListener接中的方法。为一个空方法。

*/

public void mouseExited(MouseEvent e)

{

}

/**

*实现MouseListener接中的方法。为一个空方法。

*/

public void mouseClicked(MouseEvent e)

{

}

}

求一个简单又有趣的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; //

分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮

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.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 (shunum)

System.out.println("输入的数大了,输小点的!");

else if (shunum)

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;}

}

} }

java程序编写小游戏 要求:程序随机产生20—50根火柴

为什么用AWT不用 swing?

算法思想很简单

是取胜原理

用反推法:欲拿最后一根,必须留2根在那里,欲留2根,必须上一轮留2+3+1=6给对方,(它拿一,你拿三,它拿二,你拿二,它拿三,你拿一。都是留2根)。再向上一轮,就是6+4=10。

取胜原理:把随机产生的火柴数,分解成:2+4的n次方+m,(m≤3),当m=0的时候,后取者胜,当m=1、2、3的时候,先取者胜。先取者取完m,留2+4的n次方给对方,对方不管取多少,你取的数和对方相加等于4,一直到最后,留2根给对方,根据规则,对方只能取一根,留一根给你取胜。

另:取完者胜(含最后一根):最后留4根给对方,不管对方取多少,你都可以一次取完。上一轮同样加4。

取胜原理:把随机产生的火柴数,分解成:4的n次方+m,(m≤3),当m=0的时候,后取者胜,当m=1、2、3的时候,先取者胜。先取者取完m,留4的n次方给对方,对方不管取多少,你取的数和对方相加等于4,一直到最后,留4根给对方。

代码调试可用

自己用GUI搭个界面 二十分钟的事

import java.util.Scanner;

public class MatchGame {

private static Scanner scanner = new Scanner(System.in);;

private int total;

private Computer com;

private static int exit = 1;

public MatchGame(int from, int to, String level) {

if (from = to) {

throw new IllegalArgumentException();

}

total = (int)( Math.random() * (to - from)) + from;

com = new Computer(level);

}

public void start() {

System.out.println("0 means endGame, 4 means restartGame!");

System.out.println("The number of matches is " + total);

System.out.println("~Start~");

System.out.println("----------------------------------------");

while (true) {

int u = scanner.nextInt();

if (u 0 u 4) {

System.out.println("You entered " + u);

if (total - u = 0) {

exit = 2;

endGame();

}

total = total - u;

System.out.println("Total : " + total);

int c = com.play(u, total);

System.out.println("Computer selected " + c + " matches~");

if (total - c = 0) {

exit = 0;

endGame();

}

total = total - c;

System.out.println("Total : " + total);

}else if (u == 0) {

endGame();

}else if (u 4 || u 0) {

System.out.println("You entered Wrong number~");

} else {

restart();

}

}

}

public static void restart() {

MatchGame game;

System.out

.println("Please select Computer Level: 1:HARD 2:NORMAL 3:EASY");

int level = scanner.nextInt();

if (level == 1) {

game = new MatchGame(20, 50, Computer.HARD);

} else if (level == 2) {

game = new MatchGame(20, 50, Computer.NORMAL);

} else {

game = new MatchGame(20, 50, Computer.EASY);

}

game.start();

}

public static void endGame() {

if (exit == 0) {

System.out.println("YOU WIN!!!");

} else if (exit == 2) {

System.out.println("YOU LOSE!!!");

}

System.exit(exit);

}

public static class Computer {

private static String EASY = "EASY";

private static String NORMAL = "NORMAL";

private static String HARD = "HARD";

private static String LEVEL;

private int com;

public Computer(String level) {

LEVEL = level;

}

public int play(int user, int total) {

if (LEVEL.equals(EASY)) {

com = 1;

} else if (LEVEL.equals(NORMAL)) {

com = (int) (Math.random() * 2) + 1;

} else {

int i;

if (total % 4 == 0) {

i = total / 4 - 1;

} else {

i = total / 4;

}

com = total - 4 * i - 1;

if (com == 0) {

com = 4 - user;

}

}

return com;

}

}

public static void main(String[] args) {

MatchGame game;

System.out

.println("Please select Computer Level: 1:HARD 2:NORMAL 3:EASY");

int level = scanner.nextInt();

if (level == 1) {

game = new MatchGame(20, 50, Computer.HARD);

} else if (level == 2) {

game = new MatchGame(20, 50, Computer.NORMAL);

} else {

game = new MatchGame(20, 50, Computer.EASY);

}

game.start();

}

}

求一个简单的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;//消除方法控制

public void init(){

mainFrame=new JFrame("JKJ连连看");

thisContainer = mainFrame.getContentPane();

thisContainer.setLayout(new BorderLayout());

centerPanel=new JPanel();

southPanel=new JPanel();

northPanel=new JPanel();

thisContainer.add(centerPanel,"Center");

thisContainer.add(southPanel,"South");

thisContainer.add(northPanel,"North");

centerPanel.setLayout(new GridLayout(6,5));

for(int cols = 0;cols 6;cols++){

for(int rows = 0;rows 5;rows++ ){

diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));

diamondsButton[cols][rows].addActionListener(this);

centerPanel.add(diamondsButton[cols][rows]);

}

}

exitButton=new JButton("退出");

exitButton.addActionListener(this);

resetButton=new JButton("重列");

resetButton.addActionListener(this);

newlyButton=new JButton("再来一局");

newlyButton.addActionListener(this);

southPanel.add(exitButton);

southPanel.add(resetButton);

southPanel.add(newlyButton);

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));

northPanel.add(fractionLable);

mainFrame.setBounds(280,100,500,450);

mainFrame.setVisible(true);

}

public void randomBuild() {

int randoms,cols,rows;

for(int twins=1;twins=15;twins++) {

randoms=(int)(Math.random()*25+1);

for(int alike=1;alike=2;alike++) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

}

this.grid[cols][rows]=randoms;

}

}

}

public void fraction(){

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));

}

public void reload() {

int save[] = new int[30];

int n=0,cols,rows;

int grid[][]= new int[8][7];

for(int i=0;i=6;i++) {

for(int j=0;j=5;j++) {

if(this.grid[i][j]!=0) {

save[n]=this.grid[i][j];

n++;

}

}

}

n=n-1;

this.grid=grid;

while(n=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

}

this.grid[cols][rows]=save[n];

n--;

}

mainFrame.setVisible(false);

pressInformation=false; //这里一定要将按钮点击信息归为初始

init();

for(int i = 0;i 6;i++){

for(int j = 0;j 5;j++ ){

if(grid[i+1][j+1]==0)

diamondsButton[i][j].setVisible(false);

}

}

}

public void estimateEven(int placeX,int placeY,JButton bz) {

if(pressInformation==false) {

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

pressInformation=true;

}

else {

x0=x;

y0=y;

fristMsg=secondMsg;

firstButton=secondButton;

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

if(fristMsg==secondMsg secondButton!=firstButton){

xiao();

}

}

}

public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释

if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判断是否相邻

remove();

}

else{

for (j=0;j7;j++ ) {

if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空

if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边

for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0) {

k=0;

break;

}

else{ k=1; } //K=1说明通过了第一次验证

}

if (k==1) {

linePassOne();

}

}

if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边

for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0){

k=0;

break;

}

else { k=1; }

}

if (k==1){

linePassOne();

}

}

if (y==j ) {

linePassOne();

}

}

if (k==2) {

if (x0==x) {

remove();

}

if (x0x) {

for (n=x0;n=x-1;n++ ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 n==x-1) {

remove();

}

}

}

if (x0x) {

for (n=x0;n=x+1 ;n-- ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 n==x+1) {

remove();

}

}

}

}

}

for (i=0;i8;i++ ) { //列

if (grid[i][y0]==0) {

if (xi) {

for (j=x-1;j=i ;j-- ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else { k=1; }

}

if (k==1) {

rowPassOne();

}

}

if (xi) {

for (j=x+1;j=i;j++ ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else { k=1; }

}

if (k==1) {

rowPassOne();

}

}

if (x==i) {

rowPassOne();

}

}

if (k==2){

if (y0==y) {

remove();

}

if (y0y) {

for (n=y0;n=y-1 ;n++ ) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 n==y-1) {

remove();

}

}

}

if (y0y) {

for (n=y0;n=y+1 ;n--) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 n==y+1) {

remove();

}

}

}

}

}

}

}

public void linePassOne(){

if (y0j){ //第一按钮同行空按钮在左边

for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮

if (grid[x0][i]!=0) {

k=0;

break;

}

else { k=2; } //K=2说明通过了第二次验证

}

}

if (y0j){ //第一按钮同行空按钮在与第二按钮之间

for (i=y0+1;i=j ;i++){

if (grid[x0][i]!=0) {

k=0;

break;

}

else{ k=2; }

}

}

}

public void rowPassOne(){

if (x0i) {

for (j=x0-1;j=i ;j-- ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else { k=2; }

}

}

if (x0i) {

for (j=x0+1;j=i ;j++ ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else { k=2; }

}

}

}

public void remove(){

firstButton.setVisible(false);

secondButton.setVisible(false);

fraction();

pressInformation=false;

k=0;

grid[x0][y0]=0;

grid[x][y]=0;

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==newlyButton){

int grid[][] = new int[8][7];

this.grid = grid;

randomBuild();

mainFrame.setVisible(false);

pressInformation=false;

init();

}

if(e.getSource()==exitButton)

System.exit(0);

if(e.getSource()==resetButton)

reload();

for(int cols = 0;cols 6;cols++){

for(int rows = 0;rows 5;rows++ ){

if(e.getSource()==diamondsButton[cols][rows])

estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);

}

}

}

public static void main(String[] args) {

lianliankan llk = new lianliankan();

llk.randomBuild();

llk.init();

}

}

//old 998 lines

//new 318 lines

java小游戏编程

/**

* File: ControlPanel.java

* User:相思无偿

* Date: 2004.12.3

* Describe: 俄罗斯方块的 Java 实现

*/

import javax.swing.*;

import javax.swing.border.Border;

import javax.swing.border.EtchedBorder;

import java.awt.*;

import java.awt.event.*;

/**

* 控制面板类,继承自JPanel.

* 上边安放预显窗口、等级、得分、控制按钮

* 主要用来控制游戏进程。

*/

class ControlPanel extends JPanel {

private JTextField

tfLevel = new JTextField("" + ErsBlocksGame.DEFAULT_LEVEL),

tfScore = new JTextField("0");

private JButton

btPlay = new JButton("Play"),

btPause = new JButton("Pause"),

btStop = new JButton("Stop"),

btTurnLevelUp = new JButton("Turn hard"),

btTurnLevelDown = new JButton("Turn easy");

private JPanel plTip = new JPanel(new BorderLayout());

private TipPanel plTipBlock = new TipPanel();

private JPanel plInfo = new JPanel(new GridLayout(4, 1));

private JPanel plButton = new JPanel(new GridLayout(5, 1));

private Timer timer;

private ErsBlocksGame game;

private Border border = new EtchedBorder(

EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));

/**

* 控制面板类的构造函数

* @param game ErsBlocksGame, ErsBoxesGame类的一个实例引用,

* 方便直接控制ErsBoxesGame类的行为。

*/

public ControlPanel(final ErsBlocksGame game) {

setLayout(new GridLayout(3, 1, 0, 4));

this.game = game;

plTip.add(new JLabel("Next block"), BorderLayout.NORTH);

plTip.add(plTipBlock);

plTip.setBorder(border);

plInfo.add(new JLabel("Level"));

plInfo.add(tfLevel);

plInfo.add(new JLabel("Score"));

plInfo.add(tfScore);

plInfo.setBorder(border);

tfLevel.setEditable(false);

tfScore.setEditable(false);

plButton.add(btPlay);

plButton.add(btPause);

plButton.add(btStop);

plButton.add(btTurnLevelUp);

plButton.add(btTurnLevelDown);

plButton.setBorder(border);

add(plTip);

add(plInfo);

add(plButton);

addKeyListener(new ControlKeyListener());

btPlay.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

game.playGame();

}

});

btPause.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

if (btPause.getText().equals(new String("Pause"))) {

game.pauseGame();

} else {

game.resumeGame();

}

}

});

btStop.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

game.stopGame();

}

});

btTurnLevelUp.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

try {

int level = Integer.parseInt(tfLevel.getText());

if (level ErsBlocksGame.MAX_LEVEL)

tfLevel.setText("" + (level + 1));

} catch (NumberFormatException e) {

}

requestFocus();

}

});

btTurnLevelDown.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

try {

int level = Integer.parseInt(tfLevel.getText());

if (level 1)

tfLevel.setText("" + (level - 1));

} catch (NumberFormatException e) {

}

requestFocus();

}

});

addComponentListener(new ComponentAdapter() {

public void componentResized(ComponentEvent ce) {

plTipBlock.fanning();

}

});

timer = new Timer(500, new ActionListener() {

public void actionPerformed(ActionEvent ae) {

tfScore.setText("" + game.getScore());

int scoreForLevelUpdate =

game.getScoreForLevelUpdate();

if (scoreForLevelUpdate = ErsBlocksGame.PER_LEVEL_SCORE

scoreForLevelUpdate 0)

game.levelUpdate();

}

});

timer.start();

}

/**

* 设置预显窗口的样式,

* @param style int,对应ErsBlock类的STYLES中的28个值

*/

public void setTipStyle(int style) {

plTipBlock.setStyle(style);

}

/**

* 取得用户设置的游戏等级。

* @return int, 难度等级,1 - ErsBlocksGame.MAX_LEVEL

*/

public int getLevel() {

int level = 0;

try {

level = Integer.parseInt(tfLevel.getText());

} catch (NumberFormatException e) {

}

return level;

}

/**

* 让用户修改游戏难度等级。

* @param level 修改后的游戏难度等级

*/

public void setLevel(int level) {

if (level 0 level 11) tfLevel.setText("" + level);

}

/**

* 设置"开始"按钮的状态。

*/

public void setPlayButtonEnable(boolean enable) {

btPlay.setEnabled(enable);

}

public void setPauseButtonLabel(boolean pause) {

btPause.setText(pause ? "Pause" : "Continue");

}

/**

* 重置控制面板

*/

public void reset() {

tfScore.setText("0");

plTipBlock.setStyle(0);

}

/**

* 重新计算TipPanel里的boxes[][]里的小框的大小

*/

public void fanning() {

plTipBlock.fanning();

}

/**

* 预显窗口的实现细节类

*/

private class TipPanel extends JPanel {

private Color backColor = Color.darkGray, frontColor = Color.lightGray;

private ErsBox[][] boxes =

new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];

private int style, boxWidth, boxHeight;

private boolean isTiled = false;

/**

* 预显窗口类构造函数

*/

public TipPanel() {

for (int i = 0; i boxes.length; i++) {

for (int j = 0; j boxes[i].length; j++)

boxes[i][j] = new ErsBox(false);

}

}

/**

* 预显窗口类构造函数

* @param backColor Color, 窗口的背景色

* @param frontColor Color, 窗口的前景色

*/

public TipPanel(Color backColor, Color frontColor) {

this();

this.backColor = backColor;

this.frontColor = frontColor;

}

/**

* 设置预显窗口的方块样式

* @param style int,对应ErsBlock类的STYLES中的28个值

*/

public void setStyle(int style) {

this.style = style;

repaint();

}

/**

* 覆盖JComponent类的函数,画组件。

* @param g 图形设备环境

*/

public void paintComponent(Graphics g) {

super.paintComponent(g);

if (!isTiled) fanning();

int key = 0x8000;

for (int i = 0; i boxes.length; i++) {

for (int j = 0; j boxes[i].length; j++) {

Color color = (((key style) != 0) ? frontColor : backColor);

g.setColor(color);

g.fill3DRect(j * boxWidth, i * boxHeight,

boxWidth, boxHeight, true);

key = 1;

}

}

}

/**

* 根据窗口的大小,自动调整方格的尺寸

*/

public void fanning() {

boxWidth = getSize().width / ErsBlock.BOXES_COLS;

boxHeight = getSize().height / ErsBlock.BOXES_ROWS;

isTiled = true;

}

}

private class ControlKeyListener extends KeyAdapter {

public void keyPressed(KeyEvent ke) {

if (!game.isPlaying()) return;

ErsBlock block = game.getCurBlock();

switch (ke.getKeyCode()) {

case KeyEvent.VK_DOWN:

block.moveDown();

break;

case KeyEvent.VK_LEFT:

block.moveLeft();

break;

case KeyEvent.VK_RIGHT:

block.moveRight();

break;

case KeyEvent.VK_UP:

block.turnNext();

break;

default:

break;

}

}

}

}

扫描二维码推送至手机访问。

版权声明:本文由黑客平台在线接单_免费黑客平台的网站发布,如需转载请注明出处。

本文链接:http://heikexuexi.com/39024.html

分享给朋友:

“java游戏编程小游戏代码(java游戏程序代码)” 的相关文章

电力安全个人心得体会

电力安全个人心得体会

                                                                                               电力平安 口吃懂 怎么写?上面是给除夕 野带几部电力平安 小我 口吃理解 范文,供参考。领会 口患?一尔针对变...

写给老师的一封信300字

写给老师的一封信300字

                                                                                               亲爱的先生 支到尔写给生写了一篇关于 是否有点奇怪的文章 ,每天平凡 如此光滑 的尔会给你写问题。它真的很多...

工作总结格式模板

工作总结格式模板

                                                                                               当事情 入止必然 阶段后来,回想 本身 ?以前的事情 ,卖力 反思而且 作孬本身 的事情 总结,积聚 更多的事...

销售员工作总结简短

销售员工作总结简短

                                                                                               对付 事情 员工 说,事情 总结是否有用 天空?对于 事情的优势 战争缺陷 停止总结。所以对付 销售 员工 说...

大学生自我鉴定200字

大学生自我鉴定200字

                                                                                               许多 人愤怒和悲伤地写自我判断 。因为 通过过程 自我判断 ,我们能理解吗?过去, 在每个圆圈中都有一段空间表...

调研报告范文2000字

调研报告范文2000字

                                                                                               研究申报 在于通过过过程 查询和访问 患病的案例数据 解释 答案,用案例数据 解释 不雅 点,贴上不纪律的 设...

评论列表

闹旅徒掠
2年前 (2022-07-02)

.setColor(new Color(r,g,b)); g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i); if(i

辞眸北槐
2年前 (2022-07-02)

或信号码元以离散形式表示信息的明确的规则体系。对于字符和Unicode数据的位模式的定义,此模式代表特定字母、数字或符号(例如 0x20 代表一个空格,而 0x74 代表

萌懂假欢
2年前 (2022-07-02)

x = e.getX(); y = e.getY(); Thread one; one = new Thread(this); one.start(); one = null; } /** *实现MouseListener接中的方法。为一个空方法

冬马痛言
2年前 (2022-07-02)

s[]) { Fireworks applet = new Fireworks(); JFrame frame = new JFrame("TextAreaNew"); frame.addWindowListener(new

青迟音梦
2年前 (2022-07-02)

n; //退出,重列,重新开始按钮 JLabel fractionLable=new JLabel("0"); //分数标签 JButton firstButton,secondButton; //分别记录两次被选中的按钮 int grid[][]

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。