java游戏服务端源码_页游服务端源码

hacker|
123

文章目录:

求JAVA游戏程序源代码若干

黑白棋代码

3D魔方源码

贪食蛇

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class GreedSnake implements KeyListener{

JFrame mainFrame;

Canvas paintCanvas;

JLabel labelScore;

SnakeModel snakeModel = null;

public static final int canvasWidth = 200;

public static final int canvasHeight = 300;

public static final int nodeWidth = 10;

public static final int nodeHeight = 10;

public GreedSnake() {

mainFrame = new JFrame("GreedSnake");

Container cp = mainFrame.getContentPane();

labelScore = new JLabel("Score:");

cp.add(labelScore, BorderLayout.NORTH);

paintCanvas = new Canvas();

paintCanvas.setSize(canvasWidth+1,canvasHeight+1);

paintCanvas.addKeyListener(this);

cp.add(paintCanvas, BorderLayout.CENTER);

JPanel panelButtom = new JPanel();

panelButtom.setLayout(new BorderLayout());

JLabel labelHelp;

labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);

panelButtom.add(labelHelp, BorderLayout.NORTH);

labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);

panelButtom.add(labelHelp, BorderLayout.CENTER);

labelHelp = new JLabel("SPACE or P for pause",JLabel.CENTER);

panelButtom.add(labelHelp, BorderLayout.SOUTH);

cp.add(panelButtom,BorderLayout.SOUTH);

mainFrame.addKeyListener(this);

mainFrame.pack();

mainFrame.setResizable(false);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.setVisible(true);

begin();

}

public void keyPressed(KeyEvent e){

int keyCode = e.getKeyCode();

if (snakeModel.running)

switch(keyCode){

case KeyEvent.VK_UP:

snakeModel.changeDirection(SnakeModel.UP);

break;

case KeyEvent.VK_DOWN:

snakeModel.changeDirection(SnakeModel.DOWN);

break;

case KeyEvent.VK_LEFT:

snakeModel.changeDirection(SnakeModel.LEFT);

break;

case KeyEvent.VK_RIGHT:

snakeModel.changeDirection(SnakeModel.RIGHT);

break;

case KeyEvent.VK_ADD:

case KeyEvent.VK_PAGE_UP:

snakeModel.speedUp();

break;

case KeyEvent.VK_SUBTRACT:

case KeyEvent.VK_PAGE_DOWN:

snakeModel.speedDown();

break;

case KeyEvent.VK_SPACE:

case KeyEvent.VK_P:

snakeModel.changePauseState();

break;

default:

}

if (keyCode == KeyEvent.VK_R ||

keyCode == KeyEvent.VK_S ||

keyCode == KeyEvent.VK_ENTER){

snakeModel.running = false;

begin();

}

}

public void keyReleased(KeyEvent e){

}

public void keyTyped(KeyEvent e){

}

void repaint(){

Graphics g = paintCanvas.getGraphics();

//draw background

g.setColor(Color.WHITE);

g.fillRect(0,0,canvasWidth,canvasHeight);

// draw the snake

g.setColor(Color.BLACK);

LinkedList na = snakeModel.nodeArray;

Iterator it = na.iterator();

while(it.hasNext()){

Node n = (Node)it.next();

drawNode(g,n);

}

// draw the food

g.setColor(Color.RED);

Node n = snakeModel.food;

drawNode(g,n);

updateScore();

}

private void drawNode(Graphics g, Node n){

g.fillRect(n.x*nodeWidth,

n.y*nodeHeight,

nodeWidth-1,

nodeHeight-1);

}

public void updateScore(){

String s = "Score: " + snakeModel.score;

labelScore.setText(s);

}

void begin(){

if (snakeModel == null || !snakeModel.running){

snakeModel = new SnakeModel(this,

canvasWidth/nodeWidth,

canvasHeight/nodeHeight);

(new Thread(snakeModel)).start();

}

}

public static void main(String[] args){

GreedSnake gs = new GreedSnake();

}

}

///////////////////////////////////////////////////

// 文件2

///////////////////////////////////////////////////

import java.util.*;

import javax.swing.*;

class SnakeModel implements Runnable{

GreedSnake gs;

boolean[][] matrix;

LinkedList nodeArray = new LinkedList();

Node food;

int maxX;

int maxY;

int direction = 2;

boolean running = false;

int timeInterval = 200;

double speedChangeRate = 0.75;

boolean paused = false;

int score = 0;

int countMove = 0;

// UP and DOWN should be even

// RIGHT and LEFT should be odd

public static final int UP = 2;

public static final int DOWN = 4;

public static final int LEFT = 1;

public static final int RIGHT = 3;

public SnakeModel(GreedSnake gs, int maxX, int maxY){

this.gs = gs;

this.maxX = maxX;

this.maxY = maxY;

// initial matirx

matrix = new boolean[maxX][];

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

matrix[i] = new boolean[maxY];

Arrays.fill(matrix[i],false);

}

// initial the snake

int initArrayLength = maxX 20 ? 10 : maxX/2;

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

int x = maxX/2+i;

int y = maxY/2;

nodeArray.addLast(new Node(x, y));

matrix[x][y] = true;

}

food = createFood();

matrix[food.x][food.y] = true;

}

public void changeDirection(int newDirection){

if (direction % 2 != newDirection % 2){

direction = newDirection;

}

}

public boolean moveOn(){

Node n = (Node)nodeArray.getFirst();

int x = n.x;

int y = n.y;

switch(direction){

case UP:

y--;

break;

case DOWN:

y++;

break;

case LEFT:

x--;

break;

case RIGHT:

x++;

break;

}

if ((0 = x x maxX) (0 = y y maxY)){

if (matrix[x][y]){

if(x == food.x y == food.y){

nodeArray.addFirst(food);

int scoreGet = (10000 - 200 * countMove) / timeInterval;

score += scoreGet 0? scoreGet : 10;

countMove = 0;

food = createFood();

matrix[food.x][food.y] = true;

return true;

}

else

return false;

}

else{

nodeArray.addFirst(new Node(x,y));

matrix[x][y] = true;

n = (Node)nodeArray.removeLast();

matrix[n.x][n.y] = false;

countMove++;

return true;

}

}

return false;

}

public void run(){

running = true;

while (running){

try{

Thread.sleep(timeInterval);

}

catch(Exception e){

break;

}

if(!paused){

if (moveOn()){

gs.repaint();

}

else{

JOptionPane.showMessageDialog(

null,

"you failed",

"Game Over",

JOptionPane.INFORMATION_MESSAGE);

break;

}

}

}

running = false;

}

private Node createFood(){

int x = 0;

int y = 0;

do{

Random r = new Random();

x = r.nextInt(maxX);

y = r.nextInt(maxY);

}while(matrix[x][y]);

return new Node(x,y);

}

public void speedUp(){

timeInterval *= speedChangeRate;

}

public void speedDown(){

timeInterval /= speedChangeRate;

}

public void changePauseState(){

paused = !paused;

}

public String toString(){

String result = "";

for(int i=0; inodeArray.size(); ++i){

Node n = (Node)nodeArray.get(i);

result += "[" + n.x + "," + n.y + "]";

}

return result;

}

}

class Node{

int x;

int y;

Node(int x, int y){

this.x = x;

this.y = y;

}

}

大神们 急求基于eclipse的java小游戏程序的源码,程序不要多复杂啊。像坦克大战,五子棋,扫雷之类的谢谢

import java.util.Scanner;

public class Wuziqi {

/**

* 棋盘

*/

private final int[][] qipan;

/**

* 步数

*/

private int bushu;

/**

* 构造方法,设置棋盘规格

* @param x

* @param y

*/

public Wuziqi(int x, int y) {

if (x 1 || y 1) {

System.out.println("棋盘规格应不小于1,使用默认规格");

qipan = new int[9][9];

} else {

qipan = new int[y][x];

}

}

/**

* 游戏开始

*/

public void play() {

int[] zuobiao = null;

//如果游戏没有结束

while (!end(zuobiao)) {

//落子,并取得坐标

zuobiao = luozi();

//输出棋盘

out();

}

}

/**

* 输出棋盘和棋子

*/

private void out() {

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

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

if (qipan[i][j] == 0) {

System.out.print("  +");

}else if (qipan[i][j] == -1) {

System.out.print("  白");

}else if (qipan[i][j] == 1) {

System.out.print("  黑");

}

}

System.out.println(" ");

}

}

/**

* 落子

*/

private int[] luozi() {

int[] zuobiao;

bushu++;

if (bushu % 2 == 1) {

System.out.println("请黑方落子");

zuobiao = input();

qipan[zuobiao[1]][zuobiao[0]] = 1;

}else {

System.out.println("请白方落子");

zuobiao = input();

qipan[zuobiao[1]][zuobiao[0]] = -1;

}

return zuobiao;

}

/**

* 输入坐标

* @return

*/

private int[] input() {

Scanner sc = new Scanner(System.in);

System.out.println("请输入x轴坐标");

String x = sc.next();

System.out.println("请输入y轴坐标");

String y = sc.next();

//如果没有通过验证,则再次执行input(),递归算法

if (!validate(x, y)) {

return input();

}

int int_x = Integer.valueOf(x);

int int_y = Integer.valueOf(y);

return new int[] {int_x, int_y};

}

/**

* 校验数据

* @param x

* @param y

* @return

*/

private boolean validate(String x, String y) {

Integer int_x = null;

Integer int_y = null;

//异常处理的方式判断字符串是否是一个整数

try {

int_x = Integer.valueOf(x);

int_y = Integer.valueOf(y);

} catch (NumberFormatException e) {

System.out.println("坐标格式错误,坐标应为整数");

return false;

}

if (int_x 0 || int_y 0 || int_x = qipan[0].length || int_y = qipan.length) {

System.out.println("坐标越界");

return false;

}

if (qipan[int_y][int_x] == 0) {

return true;

} else {

System.out.println("坐标上已有棋子");

}

return false;

};

/**

* 结束条件

* @return

*/

private boolean end(int[] zuobiao) {

if (zuobiao == null) {

return false;

}

//计数器

//表示棋盘上经过最近落子坐标的4条线上的连续(和最近落子颜色相同的)棋子的个数

//如果某条线上连续的棋子大于等于4(加上最近落子本身,大于等于5),则游戏结束,符合五子棋规则

int[] jieguo = new int[4];

int x = zuobiao[0];

int y = zuobiao[1];

//定义八个方向

final int[][] fangxiang = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};

//最近落子的坐标上的棋子颜色

int number = qipan[y][x];

//搜索最近落子坐标为中心最远4的距离

for (int i = 1; i = 4; i++) {

//每次搜索不同的距离都搜索八个方向

for (int j = 0; j fangxiang.length; j++) {

//约定如果某个方向为null时,不再搜索这个方向。关键字continue是跳过本次(一次)循环的意思

if (fangxiang[j] == null) {

continue;

}

int mubiao_x = x + i * fangxiang[j][0];

int mubiao_y = y + i * fangxiang[j][1];

//如果搜索坐标相对于棋盘越界,则不再搜索这个方向

if (mubiao_y = qipan.length || mubiao_y 0 || mubiao_x = qipan[0].length || mubiao_x 0) {

fangxiang[j] = null;

continue;

}

//如果最近落子坐标上的值等于目标坐标上的值(颜色相同),则计数器上某条线加1

//否则认为这个方向没有棋子或有别的颜色的棋子,不再搜索这个方向

if (number == qipan[mubiao_y][mubiao_x]) {

jieguo[j % 4]++;

}else {

fangxiang[j] = null;

}

}

}

//查看计数器上是否有比3更大的数(查看是否有一方胜出)

for (int i : jieguo) {

if (i 3) {

System.out.println("游戏结束");

if (bushu % 2 == 1) {

System.out.println("黑方胜");

} else {

System.out.println("白方胜");

}

return true;

}

}

//没有胜出者的情况下,查看棋盘上是否还有空位置,如果有,则游戏可以继续

for (int[] arr : qipan) {

for (int i : arr) {

if (i == 0) {

return false;

}

}

}

//如果没有空位置,则平局

System.out.println("游戏结束,平局");

return true;

}

}

怎样运行JAVA源代码

类似这样的?

啊,那个ABC和“原来的src”你就无视他吧,那是我后填上去的。。。这是开发手机游戏的程序WTK自动生成的目录样式(啊,也可能是eclipse生成的,但我只用过WTK)。简单说,src文件夹是装源代码的,res是装资源的,bin是装编译后的文件——jar和jad的。看样子你的bin文件夹是空的,也对,编译后的东西不属于源码嘛~你的这套文件很全,那只要安一个WTK然后把这些文件夹放在一个新的文件夹里——比如“文件夹A”——,然后把这个文件夹A放进你安装WTK的目录下的apps文件夹里,再运行WTK——打开项目——选中“文件夹A”——点击生成按钮,然后就可以去“文件夹A”的bin文件夹里找生成好的jar和jad了。 当然,运行WTK要有JDK,还要设置环境变量。不过你都能编译单个java文件了,这些应该已经做好了吧写是这么写了,不过很麻烦。安不安WTK看你了。不然你把下载链接给我,我下一套代码编译好了给你吧,正好我也想学习一下别人的代码。其实我很想看看那套代码,麻烦给我个下载链接吧 ……orz

5条大神的评论

  • avatar
    访客 2022-07-11 上午 12:07:56

    urn false; } public void run(){ running = true; while (running){ try{

  • avatar
    访客 2022-07-11 上午 01:52:26

    ng = false; } private Node createFood(){ int x = 0; int y = 0; do{ Random r = new Random(); x = r.nextInt(m

  • avatar
    访客 2022-07-11 上午 02:03:53

    1, nodeHeight-1); } public void updateScore(){ String s = "Score: " + snakeMod

  • avatar
    访客 2022-07-11 上午 05:26:13

    ("  白"); }else if (qipan[i][j] == 1) { System.out.print("  黑"); } } System.out.println(" "); } } /** * 落子 */ private

  • avatar
    访客 2022-07-10 下午 11:18:12

    dd(labelHelp, BorderLayout.CENTER); labelHelp = new JLabel("SPACE or P for pause",JLabel.CENTER); pane

发表评论