返回信息流入门java,想写个小游戏,可以通过键盘实现物体8个方向移动,写了一段代码想实现这个功能,写完后发现当改变方向(比如正在按D,突然改按A)时,物体总会听顿一下,大概一秒钟时间吧,代码逻辑上我觉得没有什么问题,不知道哪里不对,望指正!!
package com.AvoidBullet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Plane extends JFrame{
MyPanel myplane=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Plane plane1=new Plane();
}
public Plane()
{
myplane=new MyPanel();
this.add(myplane);
this.addKeyListener(myplane);
this.setSize(800,800);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements KeyListener
{
static boolean judgeUp;
static boolean judgeDown;
static boolean judgeLeft;
static boolean judgeRight;
public boolean isJudgeUp() {
return judgeUp;
}
public void setJudgeUp(boolean judgeUp) {
this.judgeUp = judgeUp;
}
public boolean isJudgeDown() {
return judgeDown;
}
public void setJudgeDown(boolean judgeDown) {
this.judgeDown = judgeDown;
}
public boolean isJudgeLeft() {
return judgeLeft;
}
public void setJudgeLeft(boolean judgeLeft) {
this.judgeLeft = judgeLeft;
}
public boolean isJudgeRight() {
return judgeRight;
}
public void setJudgeRight(boolean judgeRight) {
this.judgeRight = judgeRight;
}
MyPlane mp=null;
public MyPanel()
{
mp=new MyPlane(50,50);
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 800, 800);
this.DrawPlane(mp.getX(), mp.getY(), g);
}
public void DrawPlane(int x,int y,Graphics g)
{
g.setColor(Color.CYAN);
g.fill3DRect(x+5, y-5, 40, 5,false);
g.setColor(Color.CYAN);
g.fillOval(x+20, y-20, 10, 35 );
}
@Override
public void keyReleased(KeyEvent arg0) {
if(arg0.getKeyCode()==KeyEvent.VK_W)
{
judgeUp=false;
}if(arg0.getKeyCode()==KeyEvent.VK_S)
{
judgeDown=false;
}if(arg0.getKeyCode()==KeyEvent.VK_A)
{
judgeLeft=false;
}if(arg0.getKeyCode()==KeyEvent.VK_D)
{
judgeRight=false;
}
}
@Override
public void keyPressed(KeyEvent arg0){
if(arg0.getKeyCode()==KeyEvent.VK_W)
{
judgeUp=true;
}if(arg0.getKeyCode()==KeyEvent.VK_S)
{
judgeDown=true;
}if(arg0.getKeyCode()==KeyEvent.VK_A)
{
judgeLeft=true;
}if(arg0.getKeyCode()==KeyEvent.VK_D)
{
judgeRight=true;
}
this.planeMove();
this.repaint();
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void planeMove()
{
if(this.isJudgeUp())
{
this.mp.moveUp();
}if(this.isJudgeDown())
{
this.mp.moveDown();
}if(this.isJudgeLeft())
{
this.mp.moveLeft();
}if(this.isJudgeRight())
{
this.mp.moveRight();
}
}
// @Override
// public void run() {
// // TODO Auto-generated method stub
// while(true)
// {
// try {
// Thread.sleep(30);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
// @Override
// public void keyReleased(KeyEvent arg0) {
// // TODO Auto-generated method stub
//
// }
}
class FPlane
{
int x=0;
int y=0;
int speed=10;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
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 FPlane(int x,int y)
{
this.x=x;
this.y=y;
}
}
class MyPlane extends FPlane
{
public MyPlane(int x,int y)
{
super(x,y);
}
public void moveUp()
{
y-=speed;
}
public void moveDown()
{
y+=speed;
}
public void moveLeft()
{
x-=speed;
}
public void moveRight()
{
x+=speed;
}
public void moveRightUp()
{
x+=speed;
y-=speed;
}
public void moveRightDown()
{
x+=speed;
y+=speed;
}
public void moveLeftUp()
{
x-=speed;
y-=speed;
}
public void moveLeftDown()
{
x-=speed;
y+=speed;
}
}
这是一条镜像帖。来源:北邮人论坛 / java / #56474同步于 2017/6/2
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
java新人求指导
aq222po
2017/6/2镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。