Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Performance Tuning / Re: Best motion algorithm
|
on: 2012-03-24 15:33:34
|
Guys I have modified my code through a 4k skeleton but I have the problem that my image moves too fast. Now place the code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| package Gioco;
import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException;
import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer;
public class Gioco extends Applet implements Runnable { private boolean[] keyPress = new boolean[32768]; private final int SPRITE_DIM = 32; private final int COLONNE = 20; private final int RIGHE = 20; private final int APPLET_WIDTH = RIGHE*SPRITE_DIM; private final int APPLET_HEIGHT = COLONNE*SPRITE_DIM; private final int JUMP = SPRITE_DIM; private JLabel[] arraylabel; private ImageIcon imgTexture; private JPanel jp; private Image image; public void start(){ enableEvents(8); new Thread(this).start(); } public void run(){ int playerX = 0; int playerY=0; final int VK_LEFT = 0x25; final int VK_RIGHT = 0x27; final int VK_UP = 0x26; final int VK_DOWN = 0x28; final int VK_ATTACK = 0x42; final int MILLISECONDI = 100; ImageIcon imgPers; JLabel labelPers; boolean isControlled=true; int x=0; int y=0; int inc_x; int inc_y; ImageIcon immagine; try { image = ImageIO.read(new File("C:/Users/vincenzo/Desktop/img/grass09.jpg")); } catch (IOException e1) { e1.printStackTrace(); } setLayout(null); setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT)); imgPers = new ImageIcon("C:/Users/vincenzo/Desktop/img/persT.PNG"); labelPers = new JLabel(imgPers); labelPers.setBounds(0, 0, 32, 32); labelPers.setOpaque(false); jp = new BackgroundPanel(image); jp.setLayout(null); jp.setBounds(0, 0, APPLET_WIDTH, APPLET_HEIGHT); jp.add(labelPers); createImageIcon(); createPanelImage(); add(jp); long nextFrameStartTime = System.nanoTime(); while(true){ do{ nextFrameStartTime += 16666667; x = playerX; y = playerY; if (keyPress[VK_UP]) { y -= JUMP; } else if (keyPress[VK_DOWN]) { y += JUMP; } else if (keyPress[VK_LEFT]) { x -= JUMP; } else if (keyPress[VK_RIGHT]) { x += JUMP; } if ((x>=0) && (x<APPLET_WIDTH)) playerX=x; if ((y>=0) && (y<APPLET_HEIGHT)) playerY=y; }while(nextFrameStartTime < System.nanoTime()); labelPers.setBounds(playerX, playerY,32,32); while(nextFrameStartTime - System.nanoTime() > 0) { Thread.yield(); } } } public void processKeyEvent(KeyEvent keyEvent) { final int VK_LEFT = 0x25; final int VK_RIGHT = 0x27; final int VK_UP = 0x26; final int VK_DOWN = 0x28; final int VK_ATTACK = 0x42; final int VK_W = 0x57; final int VK_S = 0x53; final int VK_A = 0x41; final int VK_D = 0x44;
int k = keyEvent.getKeyCode(); if (k > 0) { k = k == VK_W ? VK_UP : k == VK_D ? VK_RIGHT : k == VK_A ? VK_LEFT : k == VK_S ? VK_DOWN : k; keyPress[(k >= VK_LEFT && k <= VK_DOWN) ? k : VK_ATTACK] = keyEvent.getID() != 402; } } private void createImageIcon() { int i; int j; arraylabel = new JLabel[RIGHE]; imgTexture = new ImageIcon("C:/Users/vincenzo/Desktop/img/erbaTR.PNG"); for(i=0;i<RIGHE;i++){ arraylabel[i]= new JLabel(imgTexture); arraylabel[i].setSize(32,32); } } private void createPanelImage(){ for(int i=0;i<RIGHE;i++){ arraylabel[i].setBounds(i*JUMP, JUMP*5, 32, 32); jp.add(arraylabel[i]); } } class BackgroundPanel extends JPanel { private Image img;
public BackgroundPanel (Image img) { super (new BorderLayout ()); this.img = img; }
public void paintComponent (Graphics g) { super.paintComponent (g);
g.drawImage (img, 0, 0, this); } } } |
|
|
|
|
|
4
|
Game Development / Performance Tuning / Re: Best motion algorithm
|
on: 2012-03-23 14:12:34
|
now place the code, maybe it's better 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| package Immagini;
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException;
import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer;
public class MovimentoFrame extends JFrame{ public static final int MILLISECONDI = 100; public static final int SPRITE_DIM = 32; public static final int COLONNE = 20; public static final int RIGHE = 20; public static final int APPLET_WIDTH = RIGHE*SPRITE_DIM; public static final int APPLET_HEIGHT = COLONNE*SPRITE_DIM; public static final int JUMP = SPRITE_DIM; private JLabel[] arraylabel; private ImageIcon imgPers; private JLabel labelPers; private JPanel jp; private boolean isControlled=true; private int x; private int y; private int inc_x; private int inc_y; private Timer timer; private Image image; private ImageIcon immagine; private ImageIcon imgTexture; public MovimentoFrame(int x0, int y0){ x=x0; y=y0; inc_x=inc_y=0; try { image = ImageIO.read(new File("/home/vincenzo/Scrivania/grass09.jpg")); } catch (IOException e) { e.printStackTrace(); } getContentPane().setLayout(null); getContentPane().setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT)); imgPers = new ImageIcon("/home/vincenzo/Scrivania/persT.PNG"); labelPers = new JLabel(imgPers); labelPers.setBounds(0, 0, 32, 32); labelPers.setOpaque(false); jp = new BackgroundPanel(image); jp.setLayout(null); jp.setBounds(0, 0, APPLET_WIDTH, APPLET_HEIGHT); jp.add(labelPers); createImageIcon(); createPanelImage(); getContentPane().add(jp); addKeyListener(new DirectionKeyListener()); timer = new Timer(MILLISECONDI, new ActionListener() { public void actionPerformed(ActionEvent evt) { int x1 = x + inc_x; int y1 = y + inc_y; if ((x1>=0) && (x1<APPLET_WIDTH)) x=x1; if ((y1>=0) && (y1<APPLET_HEIGHT)) y=y1; aggiorna(); } }); } private class DirectionKeyListener extends KeyAdapter { public void keyPressed(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: inc_y=-JUMP; isControlled=false; break; case KeyEvent.VK_DOWN: inc_y=JUMP; isControlled=false; break; case KeyEvent.VK_LEFT: inc_x=-JUMP; isControlled=false; break; case KeyEvent.VK_RIGHT: inc_x=JUMP; isControlled=false; break; default: break; } } public void keyReleased(KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: inc_y=0; break; case KeyEvent.VK_LEFT: case KeyEvent.VK_RIGHT: inc_x=0; break; } } } public void aggiorna() { labelPers.setBounds(x, y,32,32); if(!isControlled) controll(); repaint(); } public void controll(){ for(int i=0;i<RIGHE;i++) if(labelPers.getLocationOnScreen().equals(arraylabel[i].getLocationOnScreen())){ System.out.println(arraylabel[i].getX()+" "+arraylabel[i].getY()); break; } isControlled = true; } class BackgroundPanel extends JPanel { private Image img;
public BackgroundPanel (Image img) { super (new BorderLayout ()); this.img = img; }
public void paintComponent (Graphics g) { super.paintComponent (g);
g.drawImage (img, 0, 0, this); } } private void createImageIcon() { int i; int j; arraylabel = new JLabel[RIGHE]; imgTexture = new ImageIcon("/home/vincenzo/Scrivania/erbaTR.PNG"); for(i=0;i<RIGHE;i++){ arraylabel[i]= new JLabel(imgTexture); arraylabel[i].setSize(32,32); } } private void createPanelImage(){ for(int i=0;i<RIGHE;i++){ arraylabel[i].setBounds(i*JUMP, JUMP*5, 32, 32); jp.add(arraylabel[i]); } } public static void main(String[] args) { MovimentoFrame c= new MovimentoFrame(0,0); c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.pack(); c.setResizable(false); c.setVisible(true); c.timer.start(); c.setLocationRelativeTo(null); System.out.println(c.getSize()); } } |
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|