为什么看不见游戏里的飞机
package mscClassRoomEdit.com.msc.one.core;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class PlanesOne {
public static void main(String[] args) {
// TODO Auto-generated method stub
//这边创建了一个界面
Frame w = new Frame("FishC游戏系列-打飞机源码分析-springwindyike-冒圣春");
w.setSize(800, 1000);
//创建一个面板
MyPane mp = new MyPane();
//添加一个面板
w.add(mp);
//这边添加键盘侦听器
w.addKeyListener(mp);
mp.addKeyListener(mp);
Thread t = new Thread(mp);
t.start();
w.show();
}
}
class MyPane extends JPanel implements Runnable, KeyListener {
int count = 1000;// 发射子弹的数目
int flag1 = 0;// 标志flag为1时发射子弹
int m = 0; // 记录已发射子弹的数目
int max = 2000;// 记录得分
int x = 400;// 下面飞机的坐标
int y = 900;
int x1[] = new int;// 上面飞机的坐标
int[] y1 = new int;
int x2[] = new int;// 发射子弹的坐标
int[] y2 = new int;
public MyPane() {// 上面飞机随即分布
for (int i = 0; i < 15; i++) {
x1 = (int) (Math.random() * 780);
y1 = (int) (Math.random() * 100);
}
}
/**
* Graphics 这个参数呢,是画笔,要想画图形,传它就行
*/
public void paint(Graphics g) {
super.paint(g);
//这边无非就是在画飞机了!
Image img2 = new ImageIcon("1.jpg").getImage();
g.drawImage(img2, 0, 0, 800, 1000, null);
Image img1 = new ImageIcon("2.png").getImage();// 画下面的飞机
g.drawImage(img1, x, y, 30, 40, null);
if (max > 0) {
Font f = new Font("", Font.BOLD, 16);
g.setFont(f);
g.setColor(Color.RED);
g.drawString("成绩是:" + max, 20, 20);
for (int i = 0; i < 10; i++) {
Image img3 = new ImageIcon("4.png").getImage();// 画上面的飞机
g.drawImage(img3, x1, y1, 30, 40, null);
}
if (m == count)
m = 0;
if (flag1 == 1) {// 画新子弹的位置
x2 = x;
y2 = y;
m++;
flag1 = 0;
}
for (int i = 0; i < m; i++) {
g.setColor(Color.RED);
g.fillOval(x2 + 5, y2, 15, 15);
}
} else {
Font ff = new Font("", Font.BOLD, 36);
g.setFont(ff);
g.setColor(Color.RED);
g.drawString("GAME OVER", 100, 200);
}
}
public void run() {
while (true) {
for (int i = 0; i < 10; i++) {// 飞机随机下落
y1++;
if (y1 > 950) {
y1 = 0;
x1 = (int) (Math.random() * 780);
max -= 50;
}
}
for (int j = 0; j < m; j++) {// 子弹移动的过程
y2 -= 10;
}
for (int i = 0; i < m; i++) { // 子弹击中飞机的过程
if (y2 > 0) {
for (int j = 0; j < 10; j++) {
if (0 <= (y2 - y1) && (y2 - y1) <= 10) {
if (-15 <= (x2 - x1) && (x2 - x1) <= 10) {
max += 10;
y2 = -1;
x1 = (int) (Math.random() * 780);
y1 = 0;
}
}
}
}
}
if (max < 0) {
return;
}
try {
Thread.sleep(35);
} catch (Exception e) {
}
repaint();
}
}
public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub
}
//键盘侦听程序了
public void keyPressed(KeyEvent e) {// 控制下面飞机移动
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x -= 10;
if (x < 0) {
x = 0;
}
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
y -= 10;
if (y < 0) {
y = 0;
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
x += 10;
if (x > 780) {
x = 780;
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
y += 10;
if (y > 980) {
y = 980;
}
}
repaint();
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
flag1 = 1;
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
} //这边无非就是在画飞机了!
Image img2 = new ImageIcon("1.jpg").getImage();
g.drawImage(img2, 0, 0, 800, 1000, null);
Image img1 = new ImageIcon("2.png").getImage();// 画下面的飞机
这里的1.jpg 2.png指的应该都是飞机图片你看下你本地是不是没有图片,或者图片路径不对
页:
[1]