import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Fly {
//final:最终的,不变的,常量
//static:静态的,类的.
//static      vs.  非static
//类的(属性)      vs.  对象的(属性)
public final static int LEFT_UP=0;
public final static int LEFT_DOWN=1;
public final static int RIGHT_UP=2;
public final static int RIGHT_DOWN=3;//公开的最终静态常量
public final static int UP=4;
private int r;
private int x,y;//圆左上点坐标
private Color color;
private int orientation;//0:左上,1:左下,2:右上,3:右下
private int speed;//移动速度
private int m,n;//下降飞机坐标
private static Image image=new ImageIcon("fire.gif").getImage();
private JPanel panel;
// public Ball(int r,int x,int y,Color color,int orientation){
//  //变量的就近原则,如果局部变得属性和不是局部变量同名时
//  this.r=r;//this.r的r代表private的r  ,r 代表的是构造方法的 int r
//  this.x=x;
//  this.y=y;
//  lor=color;
//  ientation=orientation;
// }
public Fly(){
//在一个类中可以有多个同名不同参数的构造方法,
/
*
* 方法的重载:一个类中,多个方法同名不同参
* 方法的重写:在父子类之间,子类的方法和父类方法同名且同参。
*
*/
}
public void draw(Graphics g){
//g.setColor(color);
//g.fillArc(x, y, 2*r, 2*r, 0, 360);//刻画一个小球的大小 位置和颜
g.drawImage(image,x-15,y-15,50,50,panel);
}
/
/让小球移动一步
public void move(){
switch(orientation){
case LEFT_UP:
x--;
y--;
if(x<=0){
this.setOrientation(RIGHT_UP);
}
if(y<=0){
this.setOrientation(LEFT_DOWN);
}
break;
case LEFT_DOWN:
x--;
y++;
if(x<=0){
this.setOrientation(RIGHT_DOWN);
}
if(y>=Height()-2*r){
this.setOrientation(LEFT_UP);
}
break;
case RIGHT_UP:
x++;
y--;
if(x>=Width()-2*r){
this.setOrientation(LEFT_UP);
}
if(y<=0){
this.setOrientation(RIGHT_DOWN);
}
break;
case RIGHT_DOWN:
x++;
y++;
if(x>=Width()-2*r){
this.setOrientation(LEFT_DOWN);
}
if(y>=Height()-2*r){
this.setOrientation(RIGHT_UP);
}
break;
case UP://向上的小球不需要反弹
y-=2;
break;
}
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
}
public int getOrientation() {
return orientation;
}
public void setOrientation
(int orientation) {
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public JPanel getPanel() {
return panel;
}
public void setPanel(JPanel panel) {
this.panel = panel;
}
public int getM() {
return m;
}
public void setM(int m) {
this.m = m;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
}
import javax.swing.JFrame;
public class ShootFlyFrame extends JFrame {
private ShootFlyPanel panel;
public  ShootFlyFrame(){
panel=new ShootFlyPanel();
this.add(panel);
panel.startRun();
this.addKeyListener(panel);
}
public void showMe(){
this.setSize(480,640);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("鼠标控制小球");
}
public static void main(String[] args){
new ShootFlyFrame().showMe();
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class ShootFlyPanel extends JPanel implements MouseMotionListener,MouseListener,KeyListener{
private int x,y;//小球的左上坐标
private Vector<Fly> allBalls;//可以自动扩容的数组,可以存放任意类型的对象,加了Ball后
// 只能存放Ball类型的数组
private boolean isFire=false;
private Font[] allFonts;
private int[] xx;
private int[] yy;//保存所有雪花的坐标
private static Image image1=new ImageIcon("hero.gif").getImage();
private static Image image2=new ImageIcon("e1_0_1.gif").getImage();
Random random=new Random();
public ShootFlyPanel(){
x=200;
y=180;//给小球赋初始值
allFonts=new Font[4];
//  allFonts[0]=new Font("宋体",Font.BOLD,16);
//  allFonts[1]=new Font("宋体",Font.BOLD,11);
//  allFonts[0]=new Font("宋体",Font.BOLD,14);
/
/  allFonts[0]=new Font("宋体",Font.BOLD,7);
xx=new int[150];
yy=new int[150];
//  Random random=new Random();
for(int i=0;i<150;i++){
xx[i]=Int(1366);
yy[i]=-Int(768);
allBalls=new Vector<Fly>();
//allBalls.elementAt(i);得到i位置的元素
//allBalls.add(new Ball());添加一个元素
//给当前面添加一个鼠标移动事件监听。
this.addMouseMotionListener(this);//当前面板的对象监视当前面板
this.addMouseListener(this);
this.addKeyListener(this);
}
}
// public void addMouseEvent(){
//  //给当前面添加一个鼠标移动事件监听。
//  this.addMouseMotionListener(this);
// }
public void pai
nt(Graphics g){
super.paint(g);
this.setBackground(Color.BLACK);
//g.setColor(Color.BLUE);
//g.fillArc(x-30, y-30, 60, 60, 0, 360);
g.drawImage(image1,x-30,y-30,80,80,this);
for(int i=0;i<150;i++){
g.drawImage(image2, xx[i], yy[i], 60, 60, this);
}
for(int i=0;i<allBalls.size();i++){
Fly plane=allBalls.elementAt(i);
plane.draw(g);
}
}
public void startRun(){
new Thread(){
public void run(){
int count=0;
while(true){
for(int i=0;i<150;i++){
yy[i]++;
if(yy[i]>=640){
yy[i]=0;
}
}
if(isFire&&count%15==0){
Fly ball=new Fly();
//      ball.setR(10);
//      ball.setColor(Color.RED);
ball.setX(x);
ball.setY(y);
ball.setOrientation(Fly.UP);
allBalls.add(ball);
}
/
/判断小球是否越界,若越界则删除
for(int i=0;i<allBalls.size();i++){
Fly ball=allBalls.elementAt(i);
Y()<=0){
}
}
for(int i=0;i<allBalls.size();i++){
Fly ball=allBalls.elementAt(i);
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
count++;
}
}
}.start();
}
public void mouseDragged(MouseEvent e) {
//鼠标拖动
X();
Y();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
// 鼠标移动
X();
Y();
repaint();
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
//
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
isFire=true;
}
public void mouseReleased(MouseEvent arg0) {
isFire=false;
}
@Override
public void keyPressed(KeyEvent e) {
/
/ TODO Auto-generated method stub
KeyCode()){
case KeyEvent.VK_LEFT:
if(x>0){
x-=10;
}
break;
case KeyEvent.VK_RIGHT:
if(x&Width()){
x+=10;
}
break;
case KeyEvent.VK_UP:
if(y>0){
y-=10;
}
break;
case KeyEvent.VK_DOWN:
if(y&Height()){
y+=10;
}简单的java游戏代码
break;
case KeyEvent.VK_SPACE:
isFire=true;
}
//  KeyCode()==VK_LEFT){
// 
//  }
repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
isFire=false;
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
//  KeyChar()==VK_LEFT){
// 
//  }
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。