Java开发小游戏 用键盘控制精灵在游戏中上下左右跑动 窗体小游戏可打包下载,解压后双击start运行
package com.swift; import java.awt.Point; import java.awt.event.KeyEvent; import com.rupeng.game.GameCore; public class PlayWithKey implements Runnable{ public static void main(String[] args) { GameCore.start(new PlayWithKey()); } @Override public void run() { GameCore.setGameSize(800, 345); GameCore.setGameTitle("用键盘操控精灵移动的小游戏"); GameCore.loadBgView("bg.jpg"); int spriteGirl=0; GameCore.createSprite(spriteGirl, "guizi"); GameCore.playSpriteAnimate(spriteGirl, "run", true); GameCore.setSpritePosition(spriteGirl, 140, 190); for(;;) { int codeNum=GameCore.getPressedKeyCode(); System.out.println(codeNum); Point position=GameCore.getSpritePosition(spriteGirl); System.out.println(position.x+" "+position.y); if(codeNum==KeyEvent.VK_UP||codeNum==KeyEvent.VK_W) { if(position.y>135) { System.out.println("up"); GameCore.setSpritePosition(spriteGirl, position.x,--position.y); GameCore.pause(11); } } if(codeNum==KeyEvent.VK_DOWN||codeNum==KeyEvent.VK_S) { if(position.y<190) { System.out.println("down"); GameCore.setSpritePosition(spriteGirl, position.x, ++position.y); GameCore.pause(11); } } if(codeNum==KeyEvent.VK_LEFT||codeNum==KeyEvent.VK_A) { System.out.println("left"); GameCore.setSpriteFlipX(spriteGirl, true); GameCore.setSpritePosition(spriteGirl, --position.x, position.y); GameCore.pause(2); } if(codeNum==KeyEvent.VK_RIGHT||codeNum==KeyEvent.VK_D) { System.out.println("right"); GameCore.setSpriteFlipX(spriteGirl, false); GameCore.setSpritePosition(spriteGirl, ++position.x,position.y); GameCore.pause(1); } } //GameCore.pause(10000); } }
游戏效果如下图:
游戏下载地址:
https://pan.baidu.com/s/1pLPwzBP
Never waste time any more, Never old man be a yong man