Author: davedes (posted 2012-06-05 04:41:59, viewed 209 times)
| 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
| package org.newdawn.slick.tests;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
public class TestGame extends BasicGame {
public static void main(String[] args) throws SlickException {
new AppGameContainer(new TestGame(), 800, 600, false).start();
}
public TestGame() {
super("My Game");
}
private float x = 50, y = 50;
private Image img;
private Sound sfx;
private final float MOVE_SPEED = 0.3f;
@Override
public void init(GameContainer container) throws SlickException {
img = new Image("testdata/icon.png");
sfx = new Sound("testdata/cbrown01.wav");
}
@Override
public void render(GameContainer container, Graphics g) throws SlickException {
g.drawImage(img, x, y);
}
@Override
public void update(GameContainer container, int delta) throws SlickException {
Input in = container.getInput();
float s = 0;
if (in.isKeyDown(Input.KEY_LEFT))
s = -MOVE_SPEED * delta;
else if (in.isKeyDown(Input.KEY_RIGHT))
s = MOVE_SPEED * delta;
float newX = x + s;
if (newX > 0 && newX + img.getWidth() < container.getWidth())
x = newX;
else if (!sfx.playing())
sfx.play();
}
} |
Special syntax:
- To highlight a line (yellow background), prefix it with '@@'
- To indicate that a line should be removed (red background), prefix it with '-'
- To indicate that a line should be added (green background), prefix it with '+'
- To post multiple snippets, seperate them by '~~~~'
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|