day05

我想起来了,day04那天晚上一直在嗑瓜子刷韩剧,还和舍友吃辣条。然后day05刷老友记,day06晚上好像是洗头。昨天休息一天,暑假实习的这些日子和另外三个小姑凉住在一起。和爱吃零食的她们在一起,心情有些明朗。或许也是因为这段时间我比较咸鱼。昨天去图书馆看了一本“禅意”的书。day05的程序是基于day04,将静态的菜换成随机滚动的水珠。加上记分代码。以及昨天是固定的主角一张,今天的程序有主角的四张侧脸照,这意味着按键不同时,小人要换个造型。先看效果图;
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述不觉得小人有点可爱吗
代码如下

package nlday05;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class Client extends Frame {
	public static final int FRAME_WIDTH = 800;
	public static final int FRAME_LENGTH = 600;
	Toolkit tk = Toolkit.getDefaultToolkit();
	Dimension screenSize = tk.getScreenSize();
	int screenWidth = screenSize.width;
	int screenLength = screenSize.height;
	Image image = null;
    People people = null;
    int[] x = new int[10];
    int[] y = new int[10];
    List<Ball> list = new ArrayList<Ball>();
	public static void main(String[] args) {
		new Client();
	}

	public void update(Graphics g) {
		image = this.createImage(FRAME_WIDTH, FRAME_LENGTH);
		Graphics gps = image.getGraphics();
		framePaint(gps);
		g.drawImage(image, 0, 0, null);
	}

	private void framePaint(Graphics g) {
		g.setColor(Color.red);
		g.setFont(new Font("TimesRoman",Font.BOLD,28));
		if(people.getScore() == 100){
			g.drawString("完成任务", 400, 300);
			}
		else{
			g.drawString("得分"+people.getScore(),400, 300);	
		}
        people.draw(g);
       for (int i = 0; i < list.size(); i++) {
		Ball ball = list.get(i);
		ball.draw(g);
		people.collideWithBall(ball);
	}
	}

	public Client() {
		for (int i = 0; i < x.length; i++) {
			x[i] = (int)(Math.random()*800);
			y[i] = (int)(Math.random()*600);
			Ball ball = new Ball(x[i],y[i]);
			list.add(ball);
		}
        people = new People(0,25,Direction.STOP,this);
		this.setVisible(true);
		this.setResizable(false);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		this.setSize(FRAME_WIDTH, FRAME_LENGTH);
		this.setLocation(screenWidth / 2 - FRAME_WIDTH / 2, screenLength / 2 - FRAME_LENGTH / 2);
		this.setBackground(Color.yellow);
		this.setTitle("小人吃球");
		this.addKeyListener(new KeyMonitor());
		new Thread(new PaintThread()).start();
	}

	private class KeyMonitor extends KeyAdapter { // implements也可以
		public void keyPressed(KeyEvent e) {
	     people.keyPressed(e);
		}

		public void keyReleased(KeyEvent e) {
         people.keyReleased(e);
		}
	}

	private class PaintThread implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (true) {
				repaint();
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

package nlday05;

public enum Direction {
    L,U,R,D,STOP
}

这里用到枚举类

package nlday05;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;

public class Ball {
	public static final int WIDTH = 50;
	public static final int LENGTH = 50;
	private Direction dir = Direction.D;
	private int speedx = 6;
	private int speedy = 6;
	private int x, y;
	private static Toolkit tk = Toolkit.getDefaultToolkit();
	private static Image image = null;
	private int n = (int) (Math.random() * 10) + 5;//为什么是5-15?

	static {
		image = tk.getImage(Ball.class.getResource("../pics/ball.png"));
	}

	public Ball(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void draw(Graphics g) {
		g.drawImage(image, x, y, WIDTH, LENGTH, null);
		move();
	}

	private void move() {
		if (n == 0) {
			Direction[] dirs = Direction.values();
			int rn = (int) (Math.random() * 5);
			dir = dirs[rn];
			n = (int) (Math.random() * 10) + 5;
		}
		n--;//是为了让速度慢吗
		switch (dir){
		case L:
			x -= speedx;
			break;
		case U:
			y -= speedy;
			break;
		case R:
			x += speedx;
			break;
		case D:
			y += speedy;
			break;
		}
		if (x < 0) {
			x = 0;
		}
		if (y < 25) {
			y = 25;
		}
		if (x + WIDTH > Client.FRAME_WIDTH) {
			x = Client.FRAME_WIDTH - WIDTH;
		}
		if (y + LENGTH > Client.FRAME_LENGTH) {
			y = Client.FRAME_LENGTH - LENGTH;
		}
	}
	public Rectangle getRect(){
		return new Rectangle(x,y,WIDTH,LENGTH);
	}
}

package nlday05;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class People {
public static final int WIDTH = 50;
public static final int LENGTH = 50;
private int x, y;
private Direction dir;
private Direction oldDir = Direction.D;
private int speedx = 10;
private int speedy = 10;
Client c;
private static Toolkit tk = Toolkit.getDefaultToolkit();
private static Image[] images = null;
private boolean bL, bU, bR, bD = false;
int score = 0;
static {
images = new Image[] { tk.getImage(People.class.getResource("…/pics/left.png")),
tk.getImage(People.class.getResource("…/pics/up.png")),
tk.getImage(People.class.getResource("…/pics/right.png")),
tk.getImage(People.class.getResource("…/pics/down.png")) };
}

public People(int x, int y, Direction dir, Client c) {
	super();
	this.x = x;
	this.y = y;
	this.dir = dir;
	this.c = c;
}

public void draw(Graphics g) {
	switch (dir) {
	case L:
		g.drawImage(images[0], x, y, WIDTH, LENGTH, null);
		break;
	case U:
		g.drawImage(images[1], x, y, WIDTH, LENGTH, null);
		break;
	case R:
		g.drawImage(images[2], x, y, WIDTH, LENGTH, null);
		break;
	case D:
		g.drawImage(images[3], x, y, WIDTH, LENGTH, null);
		break;
	case STOP:
		if (oldDir == Direction.L) {
			g.drawImage(images[0], x, y, WIDTH, LENGTH, null);
		} else if (oldDir == Direction.U) {
			g.drawImage(images[1], x, y, WIDTH, LENGTH, null);
		} else if (oldDir == Direction.R) {
			g.drawImage(images[2], x, y, WIDTH, LENGTH, null);
		} else if (oldDir == Direction.D) {
			g.drawImage(images[3], x, y, WIDTH, LENGTH, null);
		}
		break;
	}
	move();
}

private void move() {
	switch (dir) {
	case L:
		x -= speedx;
		break;
	case U:
		y -= speedy;
		break;
	case R:
		x += speedx;
		break;
	case D:
		y += speedy;
		break;
	}
	if (x < 0) {
		x = 0;
	}
	if (y < 25) {
		y = 25;
	}
	if (x + WIDTH > Client.FRAME_WIDTH) {
		x = Client.FRAME_WIDTH - WIDTH;
	}
	if (y + LENGTH > Client.FRAME_LENGTH) {
		y = Client.FRAME_LENGTH - LENGTH;
	}
}

private void decideDirection() {
	if (bL && !bU && !bR && !bD) {
		dir = Direction.L;
		oldDir = dir;
	} else if (bU && !bR && !bD && !bL) {
		dir = Direction.U;
		oldDir = dir;
	} else if (bR && !bD && !bL && !bU) {
		dir = Direction.R;
		oldDir = dir;
	} else if (bD && !bL && !bU && !bR) {
		dir = Direction.D;
		oldDir = dir;
	} else if (!bR && !bD && !bL && !bU) {
		dir = Direction.STOP;
	}
}

public void keyPressed(KeyEvent e) {
	int keyCode = e.getKeyCode();
	switch (keyCode) {
	case KeyEvent.VK_A:
		bL = true;
		break;
	case KeyEvent.VK_W:
		bU = true;
		break;
	case KeyEvent.VK_D:
		bR = true;
		break;
	case KeyEvent.VK_S:
		bD = true;
		break;
	}
	decideDirection();
}

public void keyReleased(KeyEvent e) {
	int keyCode = e.getKeyCode();
	switch (keyCode) {
	case KeyEvent.VK_A:
		bL = false;
		break;
	case KeyEvent.VK_W:
		bU = false;
		break;
	case KeyEvent.VK_D:
		bR = false;
		break;
	case KeyEvent.VK_S:
		bD = false;
		break;
	}
	decideDirection();
}

public Rectangle getRect() {
	return new Rectangle(x, y, WIDTH, LENGTH);
}

public void collideWithBall(Ball ball) {
	if (this.getRect().intersects(ball.getRect())) {
		score += 10;
		this.c.list.remove(ball);
	}
}

public int getScore() {
	return score;
}

}
赶着回去抢洗澡位置,溜了溜了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值