|
3、几种障碍物的出现障碍物一:螃蟹- package cn.sqc.runday.model;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Paint;
- import java.io.File;
- import javax.imageio.ImageIO;
- import cn.sqc.runday.view.GameFrame;
- public class Barrs_1 {
- private Image image;
- private Image [] images;
- public static final int WIDTH=100;
- public static final int HEIGHT=110;
- private int x,y;
- int index;
- private int speed;
-
- public Barrs_1() {// 螃蟹!
- images = new Image[2];
- try {
- images[0]=ImageIO.read(new File("image/a2.png"));
- images[1]=ImageIO.read(new File("image/a4.png"));
- } catch (Exception e) {
- // TODO: handle exception
- }
- image = images[0];
- x=GameFrame.WIDTH+100;
- y=580;
- speed =30;
- index = 0;
- }
-
- public void step() {//切换图片
- image =images[index++/5%images.length];
- x-=speed;//切换图片实现螃蟹爪子张合的动态效果的同时,使其向左移动
- }
- public void paintBarrs(Graphics g) {
- g.drawImage(image, x,y,WIDTH,HEIGHT, null);
- }
- public boolean outofBounds(){
- return this.x <=-WIDTH;
- }
- public Image getImage() {
- return image;
- }
- public void setImage(Image image) {
- this.image = image;
- }
- public Image[] getImages() {
- return images;
- }
- public void setImages(Image[] images) {
- this.images = images;
- }
- 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 int getIndex() {
- return index;
- }
- public void setIndex(int index) {
- this.index = index;
- }
- public int getSpeed() {
- return speed;
- }
- public void setSpeed(int speed) {
- this.speed = speed;
- }
- public static int getWidth() {
- return WIDTH;
- }
- public static int getHeight() {
- return HEIGHT;
- }
-
- }
复制代码
需要注意的是,在创建后,记得添加set、get方法。以便在面板类中对其障碍物进行操作。 障碍物二:宠物与其称之障碍物,不如说它是个跟着玩家的小跟班。 - package cn.sqc.runday.model;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.event.KeyListener;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import cn.sqc.runday.view.GameFrame;
- public class Barrs_2{ // 宠物!
- private Image image;
- private Image images [] ;
- public static final int WIDTH= 70;
- public static final int HEIGHT = 60;
- private int x,y;
- int index;
- public Barrs_2() {
- init();
- image = images[0];
- x=300;
- y=460;
- }
- public void drop() {
- y ++;
- if(y>=460){
- y = 460;
- }
- }
- public void step(){
- image = images[index++/2%images.length];
- }
- public void paintBarrs(Graphics g) {
- g.drawImage(image, x,y,WIDTH,HEIGHT, null);
- }
- public boolean outofBounds() {
- return this.x<=-WIDTH;
- }
- public void init(){
- images = new Image[6];
- for( int i=0;i<6;i++){
- try {
- images[i]=ImageIO.read(new File ("Image/"+"d"+(i+1)+".png"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public Image getImage() {
- return image;
- }
- public void setImage(Image image) {
- this.image = image;
- }
- public Image[] getImages() {
- return images;
- }
- public void setImages(Image[] images) {
- this.images = images;
- }
- 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 int getIndex() {
- return index;
- }
- public void setIndex(int index) {
- this.index = index;
- }
- public static int getWidht() {
- return WIDTH;
- }
- public static int getHeight() {
- return HEIGHT;
- }
- }
复制代码
障碍物三、导弹- package cn.sqc.runday.model;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.io.File;
- import javax.imageio.ImageIO;
- import cn.sqc.runday.view.GameFrame;
- public class Barrs_3 {// 导弹!
- private Image image;
- private int x,y;
- public static final int WIDTH = 150;
- public static final int HEIGHT=70;
- private int speed;
- public Barrs_3() {
- try {
- image = ImageIO.read(new File("image/daodan.png"));
- } catch (Exception e) {
- // TODO: handle exception
- }
- x=GameFrame.WIDTH+1000;
- y=450;
- speed = 25 ;
- }
- public void step(){
- x-=speed;
- }
- public void paintBarrs(Graphics g) {
- g.drawImage(image, x, y, WIDTH, HEIGHT, null);
- }
- public boolean outofBounds(){
- return this.x<=-WIDTH;
- }
- public Image getImage() {
- return image;
- }
- public void setImage(Image image) {
- this.image = image;
- }
- 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 int getSpeed() {
- return speed;
- }
- public void setSpeed(int speed) {
- this.speed = speed;
- }
- public static int getWidth() {
- return WIDTH;
- }
- public static int getHeight() {
- return HEIGHT;
- }
-
- }
复制代码
障碍物四:鱼叉等障碍物- package cn.sqc.runday.model;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.io.File;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import cn.sqc.runday.view.GameFrame;
- public class Barrs_4 {// 鱼叉障碍物!
- private Image image;
- private Image images[];
- public static final int WIDTH =150;
- public static final int HEIGHT =350;
- private int x,y;
-
- public Barrs_4() {//构造方法
- Random random = new Random();
- images = new Image[4] ;
- try {
- images[0] = ImageIO.read(new File("image/11.png"));
- images[1]= ImageIO.read(new File("image/12.png"));
- images[2]= ImageIO.read(new File("image/13.png"));
- images[3]= ImageIO.read(new File("image/14.png"));
- } catch (Exception e) {
- // TODO: handle exception
- }
- image= images[random.nextInt(4)];
- x=GameFrame.WIDTH+1500;
- y=0;
- }
- public void step(){
- x-=20;
- }
- public void paintBarrs(Graphics g){
- g.drawImage(image, x, y, WIDTH, HEIGHT, null);
- }
- public boolean outofBounds(){
- return this.x<=-WIDTH;
- }
- public Image getImage() {
- return image;
- }
- public void setImage(Image image) {
- this.image = image;
- }
- public Image[] getImages() {
- return images;
- }
- public void setImages(Image[] images) {
- this.images = images;
- }
- 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 static int getWidth() {
- return WIDTH;
- }
- public static int getHeight() {
- return HEIGHT;
- }
-
- }
复制代码
|
|