| Uutiset | Koodikirjasto | Wiki | Keskustelut | FAQ | Info |
Tuplapuskurointi ja MultipleKey handlingJapppppp 30.09.03 22:41 Yksinkertainen esimerkki kuinka voi esimerkiksi tehdä kahdenpelaajan näppäimistön hallinnan ja grafiikan tuplapuskuroinnin
/* Esimerkki miten voi vaikkapa tehdä kaksinpelin näppäimistön tapahtumien hallinnan ja graafiikan tuplapuskuroinnin. @author Japppppp <HTML> <HEAD> <TITLE> KeyExample </TITLE> </HEAD> <BODY> <APPLET CODE="KeyExample.class" WIDTH=400 HEIGHT=400> </APPLET> </BODY> </HTML> */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class KeyExample extends Applet implements Runnable, KeyListener { Player one; Player two; boolean leftUp = false; boolean leftDown = false; boolean leftLeft = false; boolean leftRight = false; boolean rightUp = false; boolean rightDown = false; boolean rightLeft = false; boolean rightRight = false; Thread gameThread; int sleep = 50; //tuplapuskurointi Graphics puskuri; Image image; public void init() { super.init(); one = new Player(10, 10); two = new Player(300, 300); image = createImage(400,400); puskuri = image.getGraphics(); addKeyListener(this); requestFocus(); } public void keyTyped(KeyEvent e){} public void keyPressed(KeyEvent e) { char c = e.getKeyChar(); one.pressed = c; two.pressed = c; if (c == 'w') { leftUp = true; } if ( c == 's' ) { leftDown = true; } if ( c == 'd') { leftRight = true; } if ( c == 'a' ) { leftLeft = true; } if (c == 'i') { rightUp = true; } if ( c == 'k' ) { rightDown = true; } if ( c == 'l') { rightRight = true; } if ( c == 'j' ) { rightLeft = true; } } public void keyReleased(KeyEvent e) { char c = e.getKeyChar(); if (c == 'w') { leftUp = false; } if ( c == 's' ) { leftDown = false; } if ( c == 'd') { leftRight = false; } if ( c == 'a' ) { leftLeft = false; } if (c == 'i') { rightUp = false; } if ( c == 'k' ) { rightDown = false; } if ( c == 'l') { rightRight = false; } if ( c == 'j' ) { rightLeft = false; } } public void update(Graphics g) { puskuri.setColor(Color.black); puskuri.fillRect(0,0,400,400); puskuri.setColor(Color.white); one.paint(puskuri); two.paint(puskuri); puskuri.drawString("Onex : " +one.x, 10, 200); puskuri.drawString("Oney : " +one.y, 10, 220); puskuri.drawString("Twox : " +two.x, 10, 240); puskuri.drawString("Twoy : " +two.y, 10, 260); puskuri.drawString("LastPressed : " +one.pressed, 10, 280); paint(g); } public void paint(Graphics g) { g.drawImage( image , 0, 0 , null); } public void start() { gameThread = new Thread(this); if(gameThread != null) { gameThread.start(); } } public void run() { while(true) { if(leftUp) one.moveUp(); if(leftDown) one.moveDown(); if(leftLeft) one.moveLeft(); if(leftRight) one.moveRight(); if(rightUp) two.moveUp(); if(rightDown) two.moveDown(); if(rightLeft) two.moveLeft(); if(rightRight) two.moveRight(); repaint(); try { Thread.sleep(sleep); } catch(Exception exc){}; } } public void stop() { gameThread = null; } class Player { public int x; public int y; public int size; public char pressed; public Player(int px, int py) { x = px; y = py; size = 2; pressed = 'a'; } protected void paint(Graphics g) { g.drawOval(x, y, size, size); } public void moveUp() { y-=5; } public void moveDown() { y+=5; } public void moveLeft() { x-=5; } public void moveRight() { x+=5; } } } stWasm1.0 14:28 14.9.05 Hyvä vinkki. |
![]() Haku
|