成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java隨機方向行走代碼,java隨機方向行走代碼是什么

用JAVA編寫二維隨機行走問題

public static void main(String[] args) {

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了高郵免費建站歡迎大家使用!

// 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;

}

//判斷位置是否在數(shù)組內(nèi),如果在,返回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);

//設(shè)置居中

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);// 創(chuàng)建圖像數(shù)組對象

Image imgTemp = imIcon.getImage();// 創(chuàng)建源圖像

// 為源 圖象獲取ImageProducer源

ImageProducer sourceProducer = imgTemp.getSource();

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

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

// 創(chuàng)建圖片分割圖像對象,第一、二個參數(shù)為分割圖像起始坐標(biāo)。后兩個參數(shù)為圖像大小

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);// 創(chuàng)建圖像數(shù)組對象

Image imgTemp = imIcon.getImage();// 創(chuàng)建源圖像

// 為源 圖象獲取ImageProducer源

ImageProducer sourceProducer = imgTemp.getSource();

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

// 創(chuàng)建圖片分割圖像對象,第一、二個參數(shù)為分割圖像起始坐標(biāo)。后兩個參數(shù)為圖像大小

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){//當(dāng)記數(shù)器大于頻率時

count=0;

loopCount++;

if(loopCount = images.length){//圖片下標(biāo)越界時

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類有很多數(shù)學(xué)方面的方法

比如

//三角函數(shù)

public static double sin(double a)

public static double cos(double a)

public static double tan(double a)

//反三角函數(shù)

public static double asin(double a)

public static double acos(double a)

public static double atan(double a)

//弧度、角度轉(zhuǎn)換

public static double toRadians(double angdeg)

public static double toDegrees(double angrad)

由于sin,cos等方法參數(shù)都是弧度制,所以先使用toRadians()方法把角度轉(zhuǎn)換成弧度。

比如想獲得 sin30°的值

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

java 里如何實現(xiàn)打這樣一張圖載入后,上下左右控制人物行走呢?

這個叫人物行走方位圖吧,一般多用于制作RPG類游戲,比較常見的一種做法是,取坐標(biāo):你定義一個矩形,剛好框住一個人,這樣就把一個人物動作取出來了,然后控制框的坐標(biāo)移動,取下一個動作,直到一個動作循環(huán)完成!一樓的辦法也可以,不過是切重新切圖片,比較麻煩,載入的時候,也比較麻煩!

網(wǎng)站欄目:java隨機方向行走代碼,java隨機方向行走代碼是什么
本文來源:http://jinyejixie.com/article32/hsoppc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站網(wǎng)站設(shè)計公司、商城網(wǎng)站App設(shè)計、建站公司、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
黔东| 南汇区| 文昌市| 虎林市| 城步| 洪江市| 察雅县| 临邑县| 罗田县| 嘉荫县| 安国市| 重庆市| 龙南县| 剑川县| 花垣县| 乌兰县| 定南县| 石门县| 昌平区| 清流县| 平顶山市| 松江区| 岗巴县| 东乡县| 梁山县| 两当县| 涿州市| 延边| 景洪市| 蓝山县| 谷城县| 通榆县| 新营市| 宝鸡市| 凤台县| 深州市| 正宁县| 华安县| 霍城县| 闽侯县| 石屏县|