精灵跳跃练习
2011-06-20 11:48
162 查看
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; /** * 游戏人物的跳跃练习 * @author tiger */ public class Screen extends JPanel implements Runnable, KeyListener{ private static final long serialVersionUID = 1L; public static int width = 500; public static int height = 400; public static Map map; public static Player player; public Screen() { map = new Map(); player = new Player(); player.x = width / 2; player.y = height / 2; this.setPreferredSize(new Dimension(width, height)); this.addKeyListener(this); this.setFocusable(true); new Thread(this).start(); } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.white); g.fillRect(0, 0, width, height); map.draw(g); player.draw(g); } /** * @param args */ public static void main(String[] args) { JFrame frame = new JFrame("act tiger"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Screen()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static final int FPS = 20; public static final int FPC = 1000/FPS; private boolean LEFT = false; private boolean RIGHT = false; private boolean UP = false; @Override public void run() { while(true) { try { long time1 = System.currentTimeMillis(); // 改变方向 if (LEFT) { player.left(); } else if (RIGHT) { player.right(); } if (UP) { player.jump(); } player.update(); map.update(player); this.repaint(); long time2 = System.currentTimeMillis(); long sleepTime = Math.max(20, FPC - (time2 - time1)) ; Thread.sleep(sleepTime); } catch (Exception e) { e.printStackTrace(); } } } @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); switch(key) { case KeyEvent.VK_LEFT: LEFT = true; break; case KeyEvent.VK_RIGHT: RIGHT = true; break; case KeyEvent.VK_UP: //只有新按键后,才触发跳跃。 //j2se按下键后,会不断的触发按键事件一直到松开按键,即不断的调用到本方法,所以才要这样处理。j2me应该不需要。 if(isNewUpEvent == true) { isNewUpEvent = false; UP = true; player.isJump = false; //使得在人物的jump()方法中不受isJump的限制。 } break; } } private boolean isNewUpEvent = true; @Override public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); switch(key) { case KeyEvent.VK_LEFT: LEFT = false; break; case KeyEvent.VK_RIGHT: RIGHT = false; break; case KeyEvent.VK_UP: UP = false; isNewUpEvent = true; break; } } @Override public void keyTyped(KeyEvent e) { } } import java.awt.Color; import java.awt.Graphics; public class Player { public double x; public double y; private int head_size = 8; private int neck_length = 6; private int body_size = 10; private int leg_length = 4; public boolean isJump = false; //用来支持连续跳跃 /** * x,y 为脖子底部坐标 */ public void draw(Graphics g) { int x = (int) (this.x - Screen.map.offsetX); int y = (int) (this.y - Screen.map.offsetY); g.setColor(Color.green); //draw head g.fillArc(x - head_size / 2, y - neck_length - head_size, head_size, head_size, 0, 360); //draw neck g.drawLine(x, y, x, y - neck_length); //draw body g.fillRect(x - body_size / 2, y, body_size, body_size); //draw leg g.drawLine(x - 5, y + body_size, x - 7, y + body_size + leg_length); g.drawLine(x + 5, y + body_size, x + 7, y + body_size + leg_length); //draw test g.setColor(Color.red); g.drawLine(x, y, x + 6, y); } /**变化速度*/ private double vx, vy; /**y轴加速度*/ private double y_jiasudu = 0.6; private int x_speed = 6; private int y_speed = 8; private int jump_counter = 0; //当前连续跳了几次 private int jump_max = 3; //连续跳跃最多次数 public void left() { vx -= x_speed; } public void right() { vx += x_speed; } public void jump() { if(isJump || jump_counter >= jump_max) { return; } isJump = true; vy = -y_speed; jump_counter ++; } public void update() { x += vx; vx = 0; vy += y_jiasudu; y += vy; if(this.isTopCollision()) //头碰到了砖块 { vy = 0; } if(this.isBottomCollision()) //脚碰到了砖块 { vy = 0; isJump = false; this.y = (((int)y + body_size + leg_length) / Map.tile_size) * Map.tile_size - (body_size + leg_length); jump_counter = 0; return; } } private double getTopY() { return y - neck_length - head_size; } private double getBottomY() { return y + body_size + leg_length; } /** * 是否头部撞到砖块 * @return */ public boolean isTopCollision() { return this.isCollisionTile(x - head_size / 2, getTopY()) || this.isCollisionTile(x + head_size / 2, getTopY()); } /** * 是否脚底撞到砖块 * @return */ public boolean isBottomCollision() { return this.isCollisionTile(x - 7, getBottomY()) || this.isCollisionTile(x + 7, getBottomY()); } /** * 判断某坐标处是否有砖块 * @param x * @param y * @return */ public boolean isCollisionTile(double x, double y) { int row = (int) (y / Map.tile_size); int column = (int) (x / Map.tile_size); return Screen.map.isTile(row, column); } } [code] import java.awt.Color; import java.awt.Graphics; public class Map { public static int tile_size = 25; public int offsetX = 0; public int offsetY = 0; /** * 地图数据。为0表示活动空间,为1表示阻挡活动的砖块。 */ public int[][] data = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1}, {1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1}, {1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1}, {1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1}, {1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; /** * 判断某处是否砖块 * @param row * @param column * @return */ public boolean isTile(int row, int column) { if(row >= data.length || column >= data[row].length ) { return false; } return data[row][column] == 1; } public int getMapWidth() { return data[0].length * tile_size; } public int getMapHeight() { return data.length * tile_size; } public void draw(Graphics g) { int row = offsetY / tile_size; int column = offsetX / tile_size; for (int i = row; i < data.length; i++) { for (int j = column; j < data[i].length; j++) { if(data[i][j] == 1) { g.setColor(Color.black); g.fillRect(j * tile_size - offsetX, i * tile_size - offsetY, tile_size, tile_size); } } } } // public void left(int distance) // { // if(this.getMapWidth() <= Screen.width) // { // return; // } // offsetX -= distance; // checkOffsetX(); // } // // public void right(int distance) // { // if(this.getMapWidth() <= Screen.width) // { // return; // } // // offsetX += distance; // checkOffsetX(); // } // // public void up(int distance) // { // if(this.getMapHeight() <= Screen.height) // { // return; // } // offsetY -= distance; // checkOffsetY(); // } // // public void down(int distance) // { // if(this.getMapHeight() <= Screen.height) // { // return; // } // checkOffsetY(); // } // public void update(Player player) { this.offsetX = (int) (player.x - Screen.width / 2); this.offsetY = (int) (player.y - Screen.height / 2); checkOffsetX(); checkOffsetY(); } public void checkOffsetX() { if(offsetX > this.getMapWidth() - Screen.width) { offsetX = this.getMapWidth() - Screen.width; } if(offsetX < 0) { offsetX = 0; } } public void checkOffsetY() { if(offsetY > this.getMapHeight() - Screen.height) { offsetY = this.getMapHeight() - Screen.height; } if(offsetY < 0) { offsetY = 0; } } }
[/code]
相关文章推荐
- cocos2d-js 开发基础练习代码(1)——熟悉场景加载,精灵,动画使用等
- cocos2d-x精灵的跳跃
- [习题]综合练习 -- SqlDataSource精灵就能完成,[山寨版] Dynamic Data WebSite
- 【NOIP2017练习】跳跃切除子序列(模拟)
- 算法练习---跳跃游戏二(动态规划)
- 3.1.2 跳跃的精灵——小工具
- Unity 2D游戏开发教程之游戏中精灵的跳跃状态
- 【Cocos2dx】精灵触摸跳跃功能
- Unity 2D游戏开发教程之游戏中精灵的跳跃状态
- Android人物精灵动画练习
- z-index,标签之间的嵌套规则,精灵图,整站练习(css初始化)
- Oracle 触发器练习
- [RTT例程练习] 2.1 问题引入 线程抢占导致的临界区问题
- OpenGL 练习07 变换矩阵
- 关于精灵进程的一些思考和代码片段
- (八十)第五章编程练习
- Exynos4412裸机开发综合练习
- mysql-(六)-实战练习
- 计蒜客 跳跃游戏 贪心
- SDUT-3468 广度优先搜索练习之神奇的电梯(BFS)