. 1:
Package Sanguozhanji;
// set frame
Import javax.imageio.ImageIO;
Import in javax.swing *;.
Import java.io.IOException;
public class GameFrame extends JFrame {
public GameFrame(){
setTitle("三国游戏"); //设置标题
setSize(1024,480);//设置弹出窗口尺寸
setLocationRelativeTo(null); //Location位置,RelativeTo相对于
//set设置;Default默认值;Close关闭文件;Operation运算;JFrame窗体;EXIT离开;CLOSE结束
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗体时关闭游戏
//设置标题
try {
//IconImage图标;read读取;Resource资源类型;GameFrame游戏窗口
setIconImage(ImageIO.read(GameFrame.class.getResource("")));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
GameFrame frame = new GameFrame(); //frame框架
GamePanel panel = new GamePanel(frame);//panel画板
panel.action(); //调用开始游戏的方法
frame.add(panel); //添加一个画板;add添加
frame.setVisible(true); //显示窗体;Visible 可见的
}
}
2:
Package Penalty for Knights of Valor;
// Get the Picture
Import javax.imageio.ImageIO;
Import java.awt.image.BufferedImage;
Import java.io.IOException;
// read picture tools
public class ImageUtil {
// BufferedImage image buffer; get acquired; Img picture; path paths
public static BufferedImage GETIMG (String path) {
// Load picture; try try; ImageIO: io stream; read reading
the try {
// try to find a picture address
the BufferedImage IMG = ImageIO.read (ImageUtil.class.getResource (path));
return IMG;
// catch the catch; IOException exception
} the catch (IOException E) {
// find the output not cause
e.printStackTrace ();
}
return null;
}
}
3:
Package Penalty for Knights of Valor;
// character Riboud
Import java.awt.image.BufferedImage;
Import java.util.ArrayList;
Import java.util.List;
{class Herolb public
the BufferedImage lbImg; // display image LvBu
int X;
int Y;
int W;
int H;
// lvbu operation
List List1, List2, list3, list4;
// lvbu moves rearward
int dir; // = 0 left; 1 = right; 2 = up; down = 3; = 5 = 4 left on the left; right = 6; 7 = lower right
public Herolb(){
//读取LvBu的图片
lbImg = ImageUtil.getImg("/img/吕布L1.png");
//设置游戏开始的初始值,即显示LvBu初始位置
x = 600;
y = 200;
//显示人物的外形大小
w = lbImg.getWidth();
h = lbImg.getHeight();
dir = 0;
list1 = new ArrayList <BufferedImage>();
for (int i = 1; i<=7;i++){
list1.add(ImageUtil.getImg("/img/吕布L"+i+".png"));
}
list2 = new ArrayList <BufferedImage>();
for (int i = 1; i<=8;i++){
list2.add(ImageUtil.getImg("/img/吕布"+i+".png"));
}
list3 = new ArrayList <BufferedImage>();
for (int i = 1; i<=5;i++){
list3.add(ImageUtil.getImg("/img/a1-L"+i+".png"));
}
list4 = new ArrayList <BufferedImage>();
for (int i = 1; i<=5;i++){
list4.add(ImageUtil.getImg("/img/a1-R"+i+".png"));
}
}
int index = 1;//index索引
//lvbu动起来:Change变换
// 吕布向前动起来
public void LbChange1(){
if(index>=7){
index = 1;
}
lbImg = list1.get(index);
index++;
}
// 吕布向后动起来
public void LbChange2(){
if (index>=8){
index = 1;
}
lbImg = list2.get(index);
index++;
}
//// 吕布向前攻击
public void LbChange3(){
if (index>=5){
index = 0;
}
lbImg = list3.get(index);
index++;
}
// 吕布向后攻击
public void LbChange4(){
if (index>=5){
index = 0;
}
lbImg = list4.get(index);
index++;
}
//吕布攻击赵云;Attack攻击
public void AttackZy(Herozy zhaoyun){
if (x - zhaoyun.x +w <= zhaoyun.w +w ||zhaoyun.x -x +zhaoyun.w <= zhaoyun.w+w
||zhaoyun.x>=x && zhaoyun.x <= x +w ){
//最大值ZhaoYun.w+w;
//最小值w
new Herozy().x -= 20;
System.out.println("1");
}
}
}
4: Zhao character
package Sanguozhanji;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
public class Herozy {
BufferedImage bg;
int x;
int y;
int w;
int h;
int dir;
//
List<BufferedImage> list1,list2;
////赵云活动起来
public Herozy(){
bg = ImageUtil.getImg("/img/right-0.png");
x = 300;
y = 200;
w = bg.getWidth();
h = bg.getHeight();
dir = 0;
list1 = new ArrayList<BufferedImage>();
for (int i = 0;i<8;i++){
list1.add(ImageUtil.getImg("/img/right-"+i+".png"));
}
list2 = new ArrayList<BufferedImage>();
for (int i = 0;i<8;i++){
list2.add(ImageUtil.getImg("/img/left-"+i+".png"));
}
}
//赵云向前移动 ;Change position改变位置
int index = 0;
public void ZyChangeposition1(){
if(index==7){
index = 0;
}
bg= list1.get(index);
index++;
}
// 赵云向h后移动
public void ZyChangeposition2(){
if (index==7){
index = 0;
}
bg= list2.get(index);
index++;
}
}
5:
Package Penalty for Knights of Valor;
// invoke images and figures
import javax.swing.;
import java.awt.;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
// game interface
public class GamePanel the extends the JPanel {
// (. 1) define the background image
the BufferedImage BG;
// the BufferedImage mnImg; (MeiNv) picture defined Mn //
// call molds LvBu
Herolb new new Herolb LB = ();
Herozy = new new Herozy ZY ();
// form
GameFrame GF;
// = the Random R & lt new new the Random ();
// Constructors
public GamePanel (GameFrame gameframe) {
this.gf = gameframe;
// (2): call read pictures of tools
form // declare a background for the prevention of
BufferedImage img;
// read the image
BG = ImageUtil.getImg ( "/ img / bg1.jpg");
// mnImg = ImageUtil.getImg ( "/ img / Bu L1.png ");
//. 1: set the background; background background; color color; green green
//setBackground(Color.green);
the KeyListener ();
}
// dedicated to drawing method, Paint Paint; Paint Code paint + ENTER
// (Graphics g) was brush
@Override
public void Paint (Graphics G) {
, super.paint (G);
/ *
// learn basic operations
@ 2: set color color pen
g.setColor (Color.red);
//. 3: set Font larger digital words larger
g.setFont (new Font ( "italics", Font.BOLD, 50));
// 4: set the text position g.drawString (str, x, y) ; draw drawing
g.drawString ( "Hello everybody", 50, 50);
// 5: draw graphics
// draw a hollow rectangle; draw drawing; Rect rectangle ; x, y position of the upper left corner of the rectangle starting point, width length; height high
g.drawRect (60,60,100,20);
// draw a solid rectangle; Fill full
g.fillRect (60,100,100,20);
// open ovals Videos
g.drawOval (60,150,80,60);
// Videos solid circle
g.setColor (as Color.BLACK.);
g.fillOval (60,220,80,80);
// draw a pentagram pentagram shape may enter inside the Chinese Find
g.setColor (Color.red);
g.setFont (new new the Font ( "italics", Font.BOLD, 100));
g.drawString ( "★", 50,400);
* /
// Videos background
G.drawImage ( BG, 0,0, null);
// import pictures BG, x the abscissa, y coordinates, width and height so that there is no need to make use null artboards directly on the panel.
//g.drawImage(bg,0,0, 1024,480, null);
// drawing;
//g.drawImage(mnImg,600,200,243,131,null);
// if not set the width and height of the image, the system will default to the original image size
//g.drawImage(mnImg , 400,150, null);
// after loading LvBu
g.setColor (Color.red);
g.fillRect (100,0,350,30);
g.fillRect (617,0,350,30);
g.setColor (Color.BLUE) ;
g.fillRect (100,32,350,15);
g.fillRect (617,32,350,15);
g.drawImage(ImageUtil.getImg("/img/lvbu.png"),970,2,90,45,null);
g.drawImage(ImageUtil.getImg("/img/zy.png"),3,2,90,45,null);
g.setColor(Color.yellow);
g.setFont(new Font("宋体",Font.BOLD,50));
g.drawString("VS",505,58);
//画出吕布
g.drawImage(lb.lbImg,lb.x,lb.y,lb.w,lb.h,null);
//画出赵云
g.drawImage(zy.bg,zy.x,zy.y,zy.w,zy.h,null);
}
//KeyListener键盘监听器;即用键盘控制人物移动
public void KeyListener(){
//键盘控制编程步骤:1:创建键盘适配器即键盘的接口
KeyAdapter adapter = new KeyAdapter() {
//keyPressed按下键盘;下面就是监听键盘执行的操作;KeyEvent键盘事件
@Override
public void keyPressed(KeyEvent e) {
//当按下键盘按键时会执行的代码;char字符类型;
char c = e.getKeyChar();
zy.ZyChangeposition1();
// int key=e.getKeyCode(); if (key==KeyEvent.VK_UP) { 键盘向上移动
if (c=='a'){
//向左移动
zy.x-=3;
zy.ZyChangeposition2();
if (zy.x<=0){
zy.x=0;
//zy.dir = 'd';
}
}else if (c=='d'){
//向右移动
zy.x+=3;
//设置人物边界
if(zy.x>=1024-zy.w){
zy.x=1024-zy.w;
// zy.dir = 'a';
}
}else if(c=='w'){
//向上移动
zy.y-=3;
if (zy.y<=0){
zy.y=0;
// zy.dir = 'x';
}
}else if(c=='x'){
//向下移动
zy.y +=3;
if (zy.y>=480-zy.h){
zy.y=480-zy.h;
//zy.dir = 'w';
}
}
// repaint刷新
repaint();
}
};
//2:将键盘适配器加入到监听器;add添加;
gf.addKeyListener(adapter);
}
//开始游戏的方法;action动作
public void action() {
//创建并启动线程 控制lvbu移动;线程的固定格式;new Thread(){ public void run(){//需要线程做什么事情,就写在在run方法里面}}.start();
new Thread() {
public void run() {//run运动
//吕布移动知道循环次数用for ,不知道用while
while (true) {
lb.LbChange1();//Change 变化
//声明一个随机数对象
Random random = new Random();//Random随机
lb.dir = random.nextInt(9 + 1);//bound边界
if (lb.dir == 0) {
lb.x -= 3;
if (lb.x <= 0) {
lb.x = 0;
//lb,dir = 1;
}
} else if (lb.dir == 1) {
lb.LbChange2();
lb.x += 3;
if (lb.x >= 1024 - lb.w) {
lb.x = 1024 - lb.w;
//lb.dir = 0;
}
} else if (lb.dir == 2) {
lb.y -= 3;
if (lb.y <= 0) {
lb.y = 0;
}
} else if (lb.dir == 3) {
lb.y += 3;
if (lb.y >= 480 - lb.h) {
lb.y = 480 - lb.h;
}
//4左上
} else if (lb.dir == 4) {
lb.x -= 3;
lb.y -= 3;
//5左下
} else if (lb.dir == 5) {
lb.x -= 3;
lb.y += 3;
//6右上
} else if (lb.dir == 6) {
lb.x += 3;
lb.y -= 3;
//7右下
} else if (lb.dir == 7) {
lb.x += 3;
lb.y += 3;
} else if (lb.dir == 8) {
lb.LbChange3();
} else if (lb.dir == 9) {
lb.LbChange4();
}
//线程休眠
try {
Thread.sleep(100);
//重新调用paint方法,将lvbu刷新到新的位置;repaint重画
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}