PackageCom.swift;ImportJava.awt.Color;Importjava.awt.Dimension;ImportJava.awt.Point;Importjava.awt.event.KeyEvent;ImportCom.rupeng.game.GameCore; Public classPlaywithkeyImplementsRunnable { Public Static voidMain (string[] args) {Gamecore.start (NewPlaywithkey ()); } @Override Public voidrun () {gamecore.setgamesize (800, 345); Gamecore.setgametitle ("Use the keyboard to manipulate the mini-game of Sprite movement"); Gamecore.loadbgview ("Bg.jpg"); //Female Elf intSpritegirl = 0; Gamecore.createsprite (Spritegirl,"Guizi"); Gamecore.playspriteanimate (Spritegirl,"Run",true); Gamecore.setspriteposition (Spritegirl,140, 190); //use an array to place 8 gold coins int[] Coinnum = {1, 2, 3, 4, 5, 6, 7, 8 }; int[] coinxdate = {100, 200, 300, 400, 500, 600, 700, 760 }; int[] Coinydate = {140, 180, 150, 190, 140, 170, 160, 140 }; Boolean[] Coinisdead =New Boolean[8]; intTextnum=0; Gamecore.createimage (0); Gamecore.setimagesource (0, "Bigcoin.png"); Gamecore.setimageposition (0,710, 20); Gamecore.createtext (0, "NUM"); Gamecore.settextcolor (0, Color.White); Gamecore.settextposition (0, 650, 25); Gamecore.createtext (1, Integer.tostring (Textnum)); Gamecore.settextposition (1, 760, 25); Gamecore.settextcolor (1, Color.White); //Another method of initializing arrays boolean[]//Coinisdead={false,false,false,false,false,false,false,false}; for(inti = 0; i < coinnum.length; i++) {gamecore.createsprite (coinnum[i],"Coin"); Gamecore.setspriteposition (Coinnum[i], coinxdate[i], coinydate[i]); Gamecore.playspriteanimate (Coinnum[i],"Rotate",true); } for (;;) { intCodenum =Gamecore.getpressedkeycode (); 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) { if(position.x > 0) {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) { if(Position.x < 730) {System.out.println ("Right"); Gamecore.setspriteflipx (Spritegirl,false); Gamecore.setspriteposition (Spritegirl,++position.x, POSITION.Y); Gamecore.pause (1); } } for(inti = 0; i < coinnum.length; i++) {Point Pcoin=gamecore.getspriteposition (Coinnum[i]); Point Pgirl=gamecore.getspriteposition (Spritegirl); DoubleDistance=math.sqrt ((Math.pow (pcoin.x-pgirl.x), 2) +math.pow ((PCOIN.Y-PGIRL.Y), 2))); System.out.println (distance); if(distance<30) {Coinisdead[i]=true; } if(coinisdead[i]==true) {System.out.println (coinnum[i]); Gamecore.hidesprite (Coinnum[i]); //textnum++; //Gamecore.settext (1, integer.tostring (Textnum)); } } } //Gamecore.pause (10000); }}
Java eat coins game design and production