重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java随机方向行走代码,java随机方向行走代码是什么

用JAVA编写二维随机行走问题

public static void main(String[] args) {

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了高邮免费建站欢迎大家使用!

// int[] a = new int[]{1,2,3,4};//1 2 3 4 代表上下左右移动

Random r = new Random();

int i = 100;

int[] avgs = new int[4];

Arrays.fill(avgs, 0);//给avgs赋值为0;

for(i = 100;i0;i--){

int walk = r.nextInt(4)+1;

//这里就按得到的walk走

switch (walk) {

case 1:

//上走一步

avgs[0]++;

break;

case 2:

//下走一步

avgs[1]++;

break;

case 3:

//左走一步

avgs[2]++;

break;

case 4:

//右走一步

avgs[3]++;

break;

default:

//错误不走

break;

}

}

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

System.out.println(j+"方向走了多少步:"+avgs[j]+" ");

}

}

java 随机步法?

给你写了一个,没怎么打注释,哪里看不明白再问,望采纳

public class Demo{

char[][] chars = new char[10][10];

int char_x = 0;

int char_y = 0;

public static void main(String[] args){

Demo demo = new Demo();

//System.out.println((int)'A');

for(int i = 66;i91;i++){

demo.go((char)i);

}

demo.print();

}

//填充chars

public Demo(){

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

for(int j = 0 ; j  10 ; j++){

chars[i][j] = '.';

}

}

chars[char_x][char_y]='A';

}

//生成随机方向,并判断是否可以移动

public void go(char c){

int direction;

do{

direction = (int)(Math.random()*4);

switch(direction){

case 0:

if(isOut(char_x+1,char_y)isDot(char_x+1,char_y)){

char_x++;

}else{continue;}

break;

case 1:

if(isOut(char_x-1,char_y)isDot(char_x-1,char_y)){

char_x--;

}else{continue;}

break;

case 2:

if(isOut(char_x,char_y+1)isDot(char_x,char_y+1)){

char_y++;

}else{continue;}

break;

case 3:

if(isOut(char_x,char_y-1)isDot(char_x,char_y-1)){

char_y--;

}else{continue;}

break;

}

break;

}while(true);

chars[char_x][char_y] = c;

}

//判断位置是否在数组内,如果在,返回true

public boolean isOut(int x,int y){

return (x=0  x10  y=0  y10);

}

//判断要移动的位置是否为点,若是,则返回true

public boolean isDot(int x,int y){

return chars[x][y]=='.';

}

public void print(){

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

for(int j = 0;j10;j++){

System.out.print(chars[i][j]+"\t");

}

System.out.println();

}

}

}

在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 做一个物体移动的方向问题

Math类有很多数学方面的方法

比如

//三角函数

public static double sin(double a)

public static double cos(double a)

public static double tan(double a)

//反三角函数

public static double asin(double a)

public static double acos(double a)

public static double atan(double a)

//弧度、角度转换

public static double toRadians(double angdeg)

public static double toDegrees(double angrad)

由于sin,cos等方法参数都是弧度制,所以先使用toRadians()方法把角度转换成弧度。

比如想获得 sin30°的值

double d=Math.sin(Math.toRadians(30.0));

java 里如何实现打这样一张图载入后,上下左右控制人物行走呢?

这个叫人物行走方位图吧,一般多用于制作RPG类游戏,比较常见的一种做法是,取坐标:你定义一个矩形,刚好框住一个人,这样就把一个人物动作取出来了,然后控制框的坐标移动,取下一个动作,直到一个动作循环完成!一楼的办法也可以,不过是切重新切图片,比较麻烦,载入的时候,也比较麻烦!


当前标题:java随机方向行走代码,java随机方向行走代码是什么
本文网址:http://cqcxhl.com/article/hsoppc.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP