
正文
Java人左右走代码 java代码整体右移
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java,谁能写个代码,能监听上下左右键的!我想实现游戏角色的左右移动!在线等!不要复制百度上的,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CelMove extends JFrame {
private static final long serialVersionUID = 3171295325956127838L;
CelJPanel cjp;
static int width = 500, height = 380;
public CelMove() {
// 设置方块的初始位置
cjp = new CelJPanel(width / 2, height / 2);
// 设置方块的背景颜色
cjp.setBackground(Color.YELLOW);
// 设置绘制方块的面板的大小
cjp.setSize(width, height);
// 添加鼠标事件 让方块跟着鼠标移动
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();//得到鼠标点击的位置
cjp.lx = p.x;//设置当方块x坐标=点击的x作弊
if (cjp.lx width - 28) {// 28是空出来的一个左右边框大小.为了不让方块移动出了界面
cjp.lx = width - 28;// 如果超过边界.就设置方块的x ,回到边框内
}
if (cjp.lx 0) {
cjp.lx = 0;
}
cjp.ly = p.y;
if (cjp.ly height - 50) {// 50是空出来的一个上下边框大小.为了不让方块移动出了界面
cjp.ly = height - 50;
}
if (cjp.ly 0) {
cjp.ly = 0;
}
// lx,ly坐标设置完成,才执行repaint()重绘
cjp.repaint();
}
// 当拖动鼠标的时候..
@Override
public void mouseDragged(MouseEvent e) {
}
});
// 添加一个键盘事件
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int speed = 10;
// S 和 下箭头 可以向下移动
if (e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN) {
// 这里没有写是否出界的代码.你可以先判断移动后是否会超过边框
cjp.ly = cjp.ly + speed;
cjp.repaint();
}
// W 和 上箭头 可以向上移动
if (e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP) {
cjp.ly = cjp.ly - speed;
cjp.repaint();
}
// A 和 左箭头 可以向左移动
if (e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT) {
cjp.lx = cjp.lx - speed;
cjp.repaint();
}
// D 和 右箭头 可以向右移动
if (e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT) {
cjp.lx = cjp.lx + speed;
cjp.repaint();
}
}
});
// 设置主窗口的相关属性
this.setLayout(null);
this.add(cjp);
this.setTitle("移动方块");
this.setLocation(150, 100);
this.setSize(width, height);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new CelMove();
}
// 绘制方块的类
class CelJPanel extends JPanel {
int lx, ly;
public CelJPanel(int lx, int ly) {
super();
this.lx = lx;
this.ly = ly;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillRect(lx, ly, 20, 20);
}
}
}
你参考下吧,很久前写的
相关问答
Q1: java 里如何实现打这样一张图载入后,上下左右控制人物行走呢?
这个叫人物行走方位图吧Java人左右走代码,一般多用于制作RPG类游戏Java人左右走代码,比较常见的一种做法是Java人左右走代码,取坐标:Java人左右走代码你定义一个矩形Java人左右走代码,刚好框住一个人,这样就把一个人物动作取出来了,然后控制框的坐标移动,取下一个动作,直到一个动作循环完成!一楼的办法也可以,不过是切重新切图片,比较麻烦,载入的时候,也比较麻烦!
Q2: 乞求大神荣光!!机器人的JAVA代码。
public class Bot {
private ArrayListString commands;
public Bot(){
commands = new ArrayList();
}
public ArrayListString getCommands(){
return commands;
}
public static void main(String[ ] args){
Bot bot = new Bot();
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int n = sc.nextInt();
int i = 0;
String command;
sc.nextLine();
while (i n){
command = sc.nextLine();
if (!isCorrectCommand(command)){
System.out.println("每条指令只由L、R和数字组成(数字是0~100之间的整数)。");
}else if (command.length() 256){
System.out.println("每条指令的长度不超过256个字符。");
} else{
bot.getCommands().add(command);
i++;
}
}
i = 0;
while (i n){
System.out.printf("%.2f",bot.go(i));
i++;
}
}
public double go(int index){
int direction = 2;
double x = 0;
double y = 0;
String command = commands.get(index);
String str="0";
int i = 0;
char temp;
while (i command.length()){
temp = command.charAt(i);
if (temp = 47 temp = 57){
str += temp;
}else if (temp == 'L'){
int length = Integer.parseInt(str);
switch(direction){
case 1: x = x - length;break;
case 2: y = y + length;break;
case 3: x = x + length;break;
case 4: y = y - length;break;
}
str = "0";
direction = (direction - 1)%4;
if (direction == 0){
direction = 4;
}
}else{
int length = Integer.parseInt(str);
switch(direction){
case 1: x = x - length;break;
case 2: y = y + length;break;
case 3: x = x + length;break;
case 4: y = y - length;break;
}
str = "0";
direction = (direction + 1)%4;
}
i++;
}
int length = Integer.parseInt(str);
switch(direction){
case 1: x = x - length;break;
case 2: y = y + length;break;
case 3: x = x + length;break;
case 4: y = y - length;break;
}
return Math.sqrt(x * x + y * y);
}
public static boolean isCorrectCommand(String command){
Pattern pt = Pattern.compile("(?:L\\d{1,3}|R\\d{1,3}|\\d{1,3})+");
if (pt.matcher(command).matches()){
return true;
}
return false;
}
}
Q3: 在Java游戏中让一个人物走动的代码是什么?
代码主要为以下:
package com.lovo.game.frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import javax.swing.JFrame;
import com.lovo.game.role.Fire;
import com.lovo.game.role.GameMap;
import com.lovo.game.role.ZhaoYun;
import com.lovo.game.util.TrackerInit;
public class GameStartFrame extends JFrame implements Runnable{
private Image memoryImage;
private Graphics memoryGraphics;
public static boolean isRun = true;
private static GameMap gameMap = new GameMap();
private ZhaoYun zy = new ZhaoYun();
private Fire fire = new Fire();
public GameStartFrame(){
this.setSize(900,700);
this.setVisible(true);
this.setDefaultCloseOperation(3);
//设置居中
this.setLocationRelativeTo(null);
//初始化双缓冲画布、画笔
this.memoryImage = this.createImage(900,700);
this.memoryGraphics = this.memoryImage.getGraphics();
//媒体追踪器
MediaTracker tracker = new MediaTracker(this);
TrackerInit.initImage(tracker);
//启动线程
Thread th = new Thread(this);
th.start();
}
public static void main(String[] args) {
GameStartFrame game = new GameStartFrame();
}
public void run() {
while(isRun){
this.flushFrame();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void flushFrame(){
gameMap.drawMap(memoryGraphics);
zy.drawImage(memoryGraphics);
fire.drawImage(memoryGraphics);
//将双缓冲画布中的图片显示在窗体
this.getGraphics().drawImage(this.memoryImage,0,0,this);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.role;
import java.awt.Graphics;
import java.awt.Image;
public class GameMap {
public static Image mapImg;
public static int mapx;
public void drawMap(Graphics memoryGraphics){
mapx -=5;
if(mapx =-17100){
mapx = -17100;
}
memoryGraphics.drawImage(mapImg,mapx,0,null);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.role;
import java.awt.Graphics;
import java.awt.Image;
import com.lovo.game.util.MoveImageChange;
public class Fire {
public static Image[]fireImage;
private int x =100;
private int y =300;
private int width = 200;
private int height = 130;
private MoveImageChange moveChange = new MoveImageChange(3);
public void drawImage(Graphics memoryGraphics){
Image img = moveChange.imageChange(fireImage);
memoryGraphics.drawImage(img,this.x,this.y,this.width,this.height,null);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.util;
import java.awt.MediaTracker;
import com.lovo.game.role.Fire;
import com.lovo.game.role.GameMap;
import com.lovo.game.role.ZhaoYun;
public class TrackerInit {
public static void initImage(MediaTracker tracker){
//初始化地图图片
GameMap.mapImg = CutImage.getSingleImage("image/map.jpg", tracker);
//赵云
ZhaoYun.zyImage = CutImage.cutOneImage("image/boss/playSpear.png",18, 236, 134,tracker);
//火
Fire.fireImage = CutImage.cutOneImage("image/fireImg.png", 6, 283, 161,tracker);
try {
//0组的图片全部等待加载完毕后,在显示
tracker.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.util;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class CutImage {
public static Image[][] cutManyImage(String filePath, int row, int col,
int imageWidth, int imageHight,MediaTracker tracker) {
Image[][] img = new Image[row][col];
ImageIcon imIcon = new ImageIcon(filePath);// 创建图像数组对象
Image imgTemp = imIcon.getImage();// 创建源图像
// 为源 图象获取ImageProducer源
ImageProducer sourceProducer = imgTemp.getSource();
for (int i = 0; i row; i++) {
for (int j = 0; j col; j++) {
// 创建图片分割图像对象,第一、二个参数为分割图像起始坐标。后两个参数为图像大小
CropImageFilter cropImg = new CropImageFilter(j * imageWidth, i * imageHight,imageWidth, imageHight);
ImageProducer imgProducer = new FilteredImageSource(sourceProducer, cropImg);
img[i][j] = new JFrame().createImage(imgProducer);
tracker.addImage(img[i][j], 0);
}
}
return img;
}
public static Image[] cutOneImage(String filePath,int col,
int imageWidth, int imageHight,MediaTracker tracker) {
Image[] img = new Image[col];
ImageIcon imIcon = new ImageIcon(filePath);// 创建图像数组对象
Image imgTemp = imIcon.getImage();// 创建源图像
// 为源 图象获取ImageProducer源
ImageProducer sourceProducer = imgTemp.getSource();
for (int j = 0; j col; j++) {
// 创建图片分割图像对象,第一、二个参数为分割图像起始坐标。后两个参数为图像大小
CropImageFilter cropImg = new CropImageFilter(j * imageWidth, 0,imageWidth, imageHight);
ImageProducer imgProducer = new FilteredImageSource(sourceProducer, cropImg);
img[j] = new JFrame().createImage(imgProducer);
tracker.addImage(img[j], 0);
}
return img;
}
public static Image getSingleImage(String imgPath,MediaTracker tracker){
Image img = new ImageIcon(imgPath).getImage();
tracker.addImage(img, 0);
return img;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.util;
import java.awt.Image;
public class MoveImageChange {
private int count;
private Image img;
private int frequency;
private int loopCount;
public MoveImageChange(int frequency){
this.frequency=frequency;
}
public MoveImageChange(int frequency,int count){
this.frequency=frequency;
this.count = count;
}
public Image imageChange(Image[] images){
if(img==null){//初始图片为第一张图片
img=images[0];
}
count++;
if(countfrequency){//当记数器大于频率时
count=0;
loopCount++;
if(loopCount = images.length){//图片下标越界时
loopCount=0;
}
img=images[loopCount];
}
return img;
}
public void setLoopCount(int loopCount){
this.loopCount = loopCount;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.lovo.game.role;
import java.awt.Graphics;
import java.awt.Image;
import com.lovo.game.util.MoveImageChange;
public class ZhaoYun {
public static Image[] zyImage;
private int x =600;
private int y =300;
private int width =200;
private int height =130;
private MoveImageChange moveChange = new MoveImageChange(3);
public void drawImage(Graphics memoryGraphics){
Image img = moveChange.imageChange(zyImage);
memoryGraphics.drawImage(img,this.x,this.y,this.width,this.height,null);
关于Java人左右走代码和java代码整体右移的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






