怎么用JAVA编写一个小游戏啊?

2024-11-18 15:15:36
推荐回答(5个)
回答(1):

给我邮箱我发给你,有俄罗斯方块,迷宫等等

回答(2):

直接上网去搜索吧~
网上多的是,
自己没怎么用JAVA编过游戏,就有个黑白棋

回答(3):

上www.verycd.com搜Java游戏视频,有贪食蛇,俄罗斯方块什么的

回答(4):

技术性太强了

回答(5):

最近写的贪吃蛇

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import static java.awt.event.KeyEvent.*;

public class study extends JFrame
{
private static final long serialVersionUID = -2373004342135156305L;
study()
{
setTitle("贪吃蛇");
Dimension l=Toolkit.getDefaultToolkit().getScreenSize();
JLabel jl=new JLabel("0");
getContentPane().add(jl,BorderLayout.NORTH);
jl.setBorder(new TitledBorder("数据"));
getContentPane().add(new drawPane(jl));
getContentPane().add(jl,BorderLayout.NORTH);
JTextArea jta=new JTextArea(6,20);
jta.setText("(1)ADWS或者→←↑↓均可控制方向\n(2)SPACE是暂停,ESC是退出\n(3)ENTER重新开始游戏\n" +
"(4)Q是加速,E是减速(会导致本次得分为0)\n(5)蓝色圆圈加速,红色减速,黑色随机改变速度\n(6)按最短路线前进就是Combo");
jta.setEditable(false);
jta.setLineWrap(true);
jta.setWrapStyleWord(false);
jta.setBorder(new TitledBorder("说明"));
getContentPane().add(new JScrollPane(jta),BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocation(l.width/2-getWidth()/2,l.height/2-getHeight()/2);
setVisible(true);
}
public static void main(String[] args)
{
new study();
}
}
class drawPane extends JPanel implements KeyListener,ActionListener
{
private static final long serialVersionUID = 7331155822777994358L;
private final int dimension=10;
private final int width=300,height=300;
private final int fast=20;
int speed;
double nextScore;
long startTime;
double miles;
int base;
Timer t;
ArrayList snake;
int orientation;
Random r;
int score;
Point food;
Color foodc;
Color of;
JLabel in;
boolean combo;
boolean time;
int vCombo;
int maxCombo;
public void keyReleased(KeyEvent arg0) { }
public void keyTyped(KeyEvent arg0) { }
drawPane(JLabel i)
{
in=i;
of=in.getForeground();
r=new Random();
t=new Timer(1000,this);
addKeyListener(this);
setFocusable(true);
initial();
t.start();
startTime=System.currentTimeMillis();
}
void initial()
{
snake=null;
System.gc();
snake=new ArrayList();
Point tp;
snake.add(tp=new Point(width/2,height/2));
orientation=r.nextInt(4);
food=produceFood();
foodc=produceFoodColor();
base=distance(food,tp);
score=0;
t.setDelay((speed=10)*fast);
miles=0;
combo=true;
time=false;
vCombo=0;
maxCombo=0;
print();
}
Point produceFood()
{
Point t=new Point();
loop:while(true)
{
t.x=r.nextInt(width/dimension)*dimension;
t.y=r.nextInt(height/dimension)*dimension;
for(int i=0;i if(t.equals(snake.get(i)))
continue loop;
return t;
}
}
Color produceFoodColor()
{
switch(r.nextInt(6))
{
case 0:case 1:
return Color.BLUE;
case 4:return Color.RED;
}
return Color.BLACK;
}
void changeSpeed(int i)
{
if(i==1)
{
if(speed>1)
speed-=1;
t.setDelay(speed*fast);
repaint();
}
else
{
if(speed<10)
speed+=1;
t.setDelay(speed*fast);
repaint();
}
}
public void keyPressed(KeyEvent arg0) {
switch(arg0.getKeyCode())
{
case VK_LEFT: case VK_A:
if(orientation!=1)
orientation=3;break;
case VK_RIGHT: case VK_D:
if(orientation!=3)
orientation=1;break;
case VK_UP: case VK_W:
if(orientation!=2)
orientation=0;break;
case VK_DOWN: case VK_S:
if(orientation!=0)
orientation=2;break;
case VK_ENTER:
initial();
t.start();
startTime=System.currentTimeMillis();
break;
case VK_Q:
changeSpeed(1);
base=0;
break;
case VK_E:
changeSpeed(0);
base=0;
break;
case VK_ESCAPE:System.exit(0);break;
case VK_SPACE:
if(t.isRunning())
t.stop();
else
{
t.start();
if(miles==0)
startTime=System.currentTimeMillis();
}
break;
default:
}
}
int distance(Point a,Point b)
{
return Math.abs(a.x-b.x)+Math.abs(a.y-b.y);
}
public void actionPerformed(ActionEvent arg0) {
int x=snake.get(snake.size()-1).x,y=snake.get(snake.size()-1).y;
long st=System.currentTimeMillis();
miles+=1*Math.exp((startTime-st)*0.0001);
switch(orientation)
{
case 0: y-=dimension;break;
case 1: x+=dimension;break;
case 2: y+=dimension;break;
case 3: x-=dimension;break;
}
Point next=new Point(x,y);
if(x>width-dimension||x<0||y>height-dimension||y<0||bump(next))
{
t.stop();
JOptionPane.showMessageDialog(null, "游戏结束\n最终得分: "+score);
initial();
miles=0;
return ;
}
snake.add(next);
if(distance(snake.get(snake.size()-1),food) time=true;
if(time&&distance(snake.get(snake.size()-1),food)>distance(snake.get(snake.size()-2),food))
combo=false;
if(next.equals(food))
{
if(foodc==Color.BLUE)
{
for(int i=0;i<3;i++)
changeSpeed(1);
}
else if(foodc==Color.RED)
{
for(int i=0;i<3;i++)
changeSpeed(0);
}
else
changeSpeed(r.nextInt(2));
score+=miles*500/(st-startTime+1)*base*(1+vCombo*0.2)/100;
Point ofood=food;
food=produceFood();
base=distance(ofood,food);
foodc=produceFoodColor();
startTime=st;
miles=0;
if(combo)
vCombo++;
else
vCombo=0;
if(vCombo>maxCombo)
maxCombo=vCombo;
combo=true;
time=false;
}
else
snake.remove(0);
repaint();
}
boolean bump(Point in)
{
for(int i=0;i if(in.equals(snake.get(i)))
return true;
return false;
}
@Override
protected void paintComponent(Graphics arg0) {
super.paintComponent(arg0);
Color old=arg0.getColor();
arg0.setColor(foodc);
arg0.fillRect(food.x, food.y, dimension, dimension);
arg0.setColor(old);
int i;
for(i=0;i arg0.fillRect(snake.get(i).x, snake.get(i).y, dimension, dimension);
arg0.draw3DRect(snake.get(i).x, snake.get(i).y, dimension, dimension ,true);
print();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(width,height);
}
void print()
{
if(vCombo>0)
{
int red=30+20*vCombo;
if(red>255)
red=255;
in.setForeground(new Color(red,0,0));
}
else
setForeground(of);
in.setText("总分: "+score+" 速度: "+(11-speed) +" Combo: "+vCombo+" MaxCombo: "+maxCombo);
}
}