Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Newbie & Debugging Questions / How do I make mouse movement natural?
|
on: 2012-03-28 02:22:52
|
I need to make the mouse movement in my game have a natural feel to it like any other FPS. Also, how could I loop the mouse around in the screen so the mouse doesn't go off the screen when I move it? I'll only post the classes which I think will have to do with this problem. 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
| package me.kenneth.game;
import java.awt.Canvas; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Point; import java.awt.Toolkit; import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import me.kenneth.graphics.Screen; import me.kenneth.input.Controller; import me.kenneth.input.InputHandler;
import java.awt.image.BufferStrategy; import java.awt.image.DataBufferInt; import java.awt.image.MemoryImageSource;
public class Window extends Canvas implements Runnable { public static final long serialVersionUID = 1L; public static final int WIDTH = 800; public static final int HEIGHT = 600; public static final String TITLE = "MineGame Pre-Alpha 0.2"; private Thread thread; private boolean running = false; private BufferedImage img; private Screen render; private int pixels[]; private Game game; private InputHandler input; private int newX = 0; private int oldX = 0; private int fps = 0; public Window() { Dimension dim = new Dimension(WIDTH, HEIGHT); setPreferredSize(dim); setMinimumSize(dim); setMaximumSize(dim); render = new Screen(WIDTH, HEIGHT); img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData(); game = new Game(); input = new InputHandler(); addKeyListener(input); addFocusListener(input); addMouseListener(input); addMouseMotionListener(input); } public void start() { if (running) return; running = true; thread = new Thread(this); thread.start(); } public synchronized void stop() { if (!running) return; running = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); System.exit(0); } } public void run() { int frames = 0; double unprocessedSeconds = 0; long previousTime = System.nanoTime(); double secondsPerTick = 1 / 60.0; int tickCount = 0; while (running) { this.requestFocus(); long currentTime = System.nanoTime(); long passedTime = currentTime - previousTime; previousTime = currentTime; unprocessedSeconds += passedTime / 1000000000.0; boolean ticked = false; while (unprocessedSeconds > secondsPerTick) { ticked = true; tick(); unprocessedSeconds -= secondsPerTick; tickCount++; if (tickCount % 60 == 0) { System.out.println(frames + "fps"); fps = frames; previousTime += 1000; frames = 0; } } if (ticked) { } render(); frames++; newX = InputHandler.mouseX; if (newX > oldX) { Controller.turnRight = true; } if (newX < oldX) { Controller.turnLeft = true; } if (newX == oldX) { Controller.turnLeft = false; Controller.turnRight = false; } oldX = newX; } } private void tick() { game.tick(input.key); } private void render() { BufferStrategy bs = this.getBufferStrategy(); if (bs == null) { createBufferStrategy(3); return; } render.render(game); for (int i = 0; i < WIDTH * HEIGHT; i++) { pixels[i] = render.pixels[i]; } Graphics g = bs.getDrawGraphics(); g.drawImage(img, 0, 0, WIDTH, HEIGHT, null); g.setFont(new Font("Verdana", 0, 20)); g.setColor(Color.WHITE); g.drawString(String.valueOf(fps), 20, 20); g.dispose(); bs.show(); } public static void main(String[] args) { Image cursor = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16, 16, new int[16 * 16], 0, 16)); Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank"); Window game = new Window(); JFrame frame = new JFrame(TITLE); frame.add(game); frame.setResizable(false); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.pack(); frame.getContentPane().setCursor(blank); game.start(); }
} |
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
| package me.kenneth.game;
import java.awt.event.KeyEvent;
import me.kenneth.input.Controller;
public class Game { private int time; public Controller controls; public Game() { time = 0; controls = new Controller(); } public void tick(boolean[] args) { time++; boolean forward = args[KeyEvent.VK_W]; boolean back = args[KeyEvent.VK_S]; boolean right = args[KeyEvent.VK_D]; boolean left = args[KeyEvent.VK_A]; boolean jump = args[KeyEvent.VK_SPACE]; boolean crouch = args[KeyEvent.VK_CONTROL]; boolean run = args[KeyEvent.VK_SHIFT]; controls.tick(forward, back, left, right, jump, crouch, run); } public int getTime() { return time; }
} |
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
| package me.kenneth.input;
public class Controller { public double x, z, y, rotation, xa, za, rotationa; public static boolean turnLeft = false, turnRight = false; public static boolean walk = false, crouchWalk = false, runWalk = false; public void tick(boolean forward, boolean back, boolean left, boolean right, boolean jump, boolean crouch, boolean run) { double rotationSpeed = 0.025; double walkSpeed = 0.5; double jumpHeight = 2; double crouchHeight = 1.3; double xMove = 0; double zMove = 0; if (forward) { zMove++; walk = true; } if (back) { zMove--; walk = true; } if (right) { xMove++; walk = true; } if (left) { xMove--; walk = true; } if (turnLeft) { rotationa -= rotationSpeed; } if (turnRight) { rotationa += rotationSpeed; } if (jump) { y += jumpHeight; run = false; } if (crouch) { y -= crouchHeight; run = false; crouchWalk = true; walkSpeed = 0.2; } if (run) { walkSpeed = 1; runWalk = true; } if (!forward && !back && !right && !left && !run) { walk = false; } if (!crouch) { crouchWalk = false; } if (!run) { runWalk = false; } xa += (xMove * Math.cos(rotation) + zMove * Math.sin(rotation)) * walkSpeed; za += (zMove * Math.cos(rotation) - xMove * Math.sin(rotation)) * walkSpeed; x += xa; y *= 0.5; z += za; xa *= 0.1; za *= 0.1; rotation += rotationa; rotationa *= 0.1; }
} |
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
| package me.kenneth.input;
import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener;
public class InputHandler implements KeyListener, FocusListener, MouseListener, MouseMotionListener {
public boolean[] key = new boolean[68836]; public static int mouseX; public static int mouseY; @Override public void mouseDragged(MouseEvent event) { }
@Override public void mouseMoved(MouseEvent event) { mouseX = event.getX(); mouseY = event.getY(); }
@Override public void mouseClicked(MouseEvent event) { }
@Override public void mouseEntered(MouseEvent event) { }
@Override public void mouseExited(MouseEvent event) { }
@Override public void mousePressed(MouseEvent event) { }
@Override public void mouseReleased(MouseEvent event) { }
@Override public void focusGained(FocusEvent event) { }
@Override public void focusLost(FocusEvent event) { for (int i = 0; i < key.length; i++) { key[i] = false; } }
@Override public void keyPressed(KeyEvent event) { int keyCode = event.getKeyCode(); if (keyCode > 0 && keyCode < key.length) { key[keyCode] = true; } }
@Override public void keyReleased(KeyEvent event) { int keyCode = event.getKeyCode(); if (keyCode > 0 && keyCode < key.length) { key[keyCode] = false; } }
@Override public void keyTyped(KeyEvent event) { }
} |
|
|
|
|
|
4
|
Game Development / Newbie & Debugging Questions / 3D Game Programming Error
|
on: 2012-03-24 06:13:15
|
I'm following this video : http://www.youtube.com/watch?v=RKPEQfkhbAY&feature=channel and when I was done with it, I ran the program and got this: http://imgur.com/eammB It should look staright without the zig zag lines. Here is all of my 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
| package me.kenneth.game;
import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import me.kenneth.graphics.Screen;
import java.awt.image.BufferStrategy; import java.awt.image.DataBufferInt;
public class Window extends Canvas implements Runnable { public static final long serialVersionUID = 1L; public static final int WIDTH = 800; public static final int HEIGHT = 600; public static final String TITLE = "MineGame Pre-Alpha 0.2"; private Thread thread; private boolean running = false; private BufferedImage img; private Screen render; private int pixels[]; private Game game; public Window() { Dimension dim = new Dimension(WIDTH, HEIGHT); setPreferredSize(dim); setMinimumSize(dim); setMaximumSize(dim); render = new Screen(WIDTH, HEIGHT); img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData(); game = new Game(); } private void start() { if (running) return; running = true; thread = new Thread(this); thread.start(); } private void stop() { if (!running) return; running = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); System.exit(0); } } public void run() { int frames = 0; double unprocessedSeconds = 0; long previousTime = System.nanoTime(); double secondsPerTick = 1 / 60.0; int tickCount = 0; boolean ticked = false; while (running) { long currentTime = System.nanoTime(); long passedTime = currentTime - previousTime; previousTime = currentTime; unprocessedSeconds += passedTime / 1000000000.0; while (unprocessedSeconds > secondsPerTick) { tick(); unprocessedSeconds -= secondsPerTick; ticked = true; tickCount++; if (tickCount % 60 == 0) { System.out.println(frames + "fps"); previousTime += 1000; frames = 0; } } render(); frames++; } Toolkit.getDefaultToolkit().sync(); } private void tick() { game.tick(); } private void render() { BufferStrategy bs = this.getBufferStrategy(); if (bs == null) { createBufferStrategy(3); return; } render.render(game); for (int i = 0; i < WIDTH * HEIGHT; i++) { pixels[i] = render.pixels[i]; } Graphics g = bs.getDrawGraphics(); g.drawImage(img, 0, 0, WIDTH, HEIGHT, null); g.dispose(); bs.show(); } public static void main(String[] args) { Window game = new Window(); JFrame frame = new JFrame(TITLE); frame.add(game); frame.setResizable(false); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.pack(); game.start(); }
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package me.kenneth.game;
public class Game { public int time; public Game() { time = 0; } public void tick() { time += 2; }
} |
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
| package me.kenneth.graphics;
import me.kenneth.game.Window;
public class Render { public final int WIDTH; public final int HEIGHT; public final int[] pixels; public Render(int width, int height) { WIDTH = width; HEIGHT = height; pixels = new int[width * height]; } public void draw(Render render, int xOffset, int yOffset) { for (int y = 0; y < render.HEIGHT; y++) { int yPix = y + yOffset; if (yPix < 0 || yPix >= Window.HEIGHT) continue; for (int x = 0; x < render.WIDTH; x++) { int xPix = x + xOffset; if (xPix < 0 || xPix >= Window.WIDTH) continue; int alpha = render.pixels[x + y + render.WIDTH]; if (alpha > 0) { pixels[xPix + yPix * WIDTH] = render.pixels[x + y * render.WIDTH]; } } } }
} |
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
| package me.kenneth.graphics;
import java.util.Random;
import me.kenneth.game.Game;
public class Screen extends Render {
private Render test; private Render3D render3D;
public Screen(int width, int height) { super(width, height); Random random = new Random(); render3D = new Render3D(WIDTH, HEIGHT); test = new Render(256, 256); for (int i = 0; i < 256 * 256; i++) {
test.pixels[i] = random.nextInt() * (random.nextInt(5) / 4);
} }
public void render(Game game) { for (int i = 0; i < WIDTH * HEIGHT; i++) { pixels[i] = 0; }
for (int i = 0; i < 50; i++) { int anim = (int) (Math.sin((game.time + i * 2) % 1000.0 / 100) * 100); int anim1 = (int) (Math.cos((game.time + i * 2) % 1000.0 / 100) * 100);
} render3D.floor(); draw(render3D, 0, 0);
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package me.kenneth.graphics;
public class Render3D extends Render {
public Render3D(int width, int height) { super(width, height); } public void floor() { for (int y = 0; y < HEIGHT; y++) { double yDepth = y - HEIGHT / 2.4; double z = 100.0 / yDepth; for (int x = 0; x < WIDTH; x++) { double xDepth = x - WIDTH / 2; xDepth *= z; int xx = (int) xDepth & 5; pixels[x + y * WIDTH] = xx * 128; } } }
} |
Is it something wrong in the Render3D class?
|
|
|
|
|
5
|
Java Game APIs & Engines / Java 2D / Making a side scroller game
|
on: 2012-03-07 19:05:21
|
|
I want to make a kind of 2D puzzle side-scroller which will use tiles for the map. Do you have any idea on how I could load multiple files like Frame1.txt and Frame2.txt and translate numbers into tiles? Also, how could I make it so when the character wants to go to the next frame, it transitions smoothly?
|
|
|
|
|
6
|
Game Development / Newbie & Debugging Questions / Re: Making My Own "Gaming Library"
|
on: 2012-02-23 17:40:01
|
Also, I'm not having trouble loading images from a folder outside the JAR, but is there a way to load images from or copy images from a JAR? This is what I use to load images from a JAR: 1 2 3 4 5 6 7 8 9 10 11 12 13
| public static Image getImage(String imageName) {
Image imageToReturn;
try {
imageToReturn = new ImageIcon(System.getProperty("user.dir") + File.seperator + "images" + File.seperator + imageName).getImage();
} catch (Exception e) {System.err.println("Error loading image!"); return;}
return imageToReturn;
} |
|
|
|
|
|
7
|
Game Development / Newbie & Debugging Questions / Re: Making My Own "Gaming Library"
|
on: 2012-02-23 17:30:32
|
|
Um... Do you know that I'm talking about Java and not JavaScript? If you know that I'm talking about Java, are you wanting me to use the same ideas but in Java?
Oh, and to the other person, I said I wasn't very imaginative in code because I'm more of a visual person(if that makes sense).
I'm working on a game right now. It will kind of be like Mario but much simpler.
|
|
|
|
|
9
|
Game Development / Newbie & Debugging Questions / Making My Own "Gaming Library"
|
on: 2012-02-22 20:30:22
|
|
Could anyone guid me into making some interfaces and stuff so I can implement it into games I will make? Also, how could I make a game that prints tiles out by reading a picture and decoding it like Notch used on one of his games. Thanks, I know the basics of game making but I'm not very imaginative when it comes to code. I'm more imaginative with visual stuff.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|