Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Newbie & Debugging Questions / Painting an object to another object before the JFrame?
|
on: 2012-04-04 22:46:36
|
Is this possible? For I am trying it, and currently it is not working.. Won't do as last time and post all the code  Here's the object with the paintComponent(): 1 2 3 4 5 6 7 8 9
| public Platform() { } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.gray); g.fillRect(0, 0, getBounds().width, getBounds().height); |
The Platform-object is created first here: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| private class Level extends JPanel implements KeyListener, ActionListener { Platform ground = new Platform(); Platform p1 = new Platform(); Platform p2 = new Platform(); Platform p3 = new Platform(); public Level() { setPreferredSize(new Dimension(400, 400)); ground.setBounds(0, 380, 400, 20); p1.setBounds(150, 200, 100, 20); p2.setBounds(100, 300, 200, 20); p3.setBounds(100, 360, 40, 20); add(ground); add(p1); add(p2); add(p3); } |
(This private class does also have a paintComponent(), is that the problem..?) Then the Level object is added to the JFrame in the superclass. The mechanics of the platformer works just as I want it now, but having invisible platforms bothers me a bit : D
|
|
|
|
|
3
|
Game Development / Newbie & Debugging Questions / Re: Umm.. Hi?
|
on: 2012-03-24 17:20:06
|
Instead of going through a loop to figure out what grid the player is in, you could simply divide the player's X and Y by the grid's width and height and cast to int to figure what grid the player is in.
Um, I'm doing my second attempt now, but not sure if I understood everything here.. By grid, do you mean one tile (20 x 20 px), or the entire thing (400, 400px)? I'm norwegian, so I don't know if 'grid' refers to the entire eh, system, or just one tile.. EDIT: Fixed it. My brain just takes some time to get started in the weekends 
|
|
|
|
|
5
|
Game Development / Newbie & Debugging Questions / Re: Umm.. Hi?
|
on: 2012-03-24 01:43:52
|
Yay! Thank you for a quick reply!  Here is the entire code, so you can compile and run if you so wish. 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
| import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.net.URL; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer;
public class PlatformTest extends JFrame implements ActionListener{ Level level1 = new Level(); Rectangle[][] levelGrid = new Rectangle[20][20]; Boolean[][] physicGrid = new Boolean[20][20]; int gridX, gridY, i, j; final int size = 20; int x = 200, y = 0, xSpd, ySpd, dir, degrees, rot, yB; boolean moving, jumping, onFloor; public PlatformTest() { setTitle("PlatformTest"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400,400); setResizable(false); for (i = 0; i < 20; i++) { for (j = 0; j < 20; j++) { levelGrid[i][j] = new Rectangle(gridX, gridY, size, size); physicGrid[i][j] = false; gridX += 20; if (gridX == 400) { gridX = 0; gridY += 20; } } } addKeyListener(level1); add(level1); new Timer(10, this).start(); setVisible(true); } public static void main(String args[]) { new PlatformTest(); }
@Override public void actionPerformed(ActionEvent ae) { repaint(); } private class Level extends JPanel implements ActionListener, KeyListener { public Level() { setPreferredSize(new Dimension(400,400)); new Timer(10, this).start(); } @Override public void paintComponent(Graphics g) { g.setColor(Color.gray); g.fillRect(0, 0, 400, 400); g.setColor(Color.black); for (int a = 15; a < 20; a++) { int yA = 9; int aX = (int)levelGrid[yA][a].getX(); int aY = (int)levelGrid[yA][a].getY(); physicGrid[yA][a] = true; g.fillRect(aX, aY, 20, 20); } for (int a = 5; a < 15; a++) { int yA = 11; int aX = (int)levelGrid[yA][a].getX(); int aY = (int)levelGrid[yA][a].getY(); physicGrid[yA][a + 1] = true; g.fillRect(aX, aY, 20, 20); } for (int a = 1; a < 11; a++) { int yA = 15; int aX = (int)levelGrid[yA][a].getX(); int aY = (int)levelGrid[yA][a].getY(); physicGrid[yA][a + 1] = true; g.fillRect(aX, aY, 20, 20); } for (int a = 12; a < 15; a++) { int yA = 17; int aX = (int)levelGrid[yA][a].getX(); int aY = (int)levelGrid[yA][a].getY(); physicGrid[yA][a + 1] = true; g.fillRect(aX, aY, 20, 20); } for (int a = 1; a < 19; a++) { int yA = 18; int aX = (int)levelGrid[yA][a].getX(); int aY = (int)levelGrid[yA][a].getY(); physicGrid[yA][a] = true; g.fillRect(aX, aY, 20, 20); } g.setColor(Color.black); g.fillOval(x, y, 20, 20); }
@Override public void actionPerformed(ActionEvent ae) { int posX = 0, posY = 0; Position pos = new Position(posX, posY); Rectangle pRect = new Rectangle(x, y, 20, 20); int aX = 0, aY = 0; while (aY < 20) { if (pRect.intersects(levelGrid[aX][aY])) { pos.setPos(aX, aY); } aX++; if (aX == 20) { aX = 0; aY++; } } if (x < 400 && x > 0) { x += xSpd; } y += ySpd; if (physicGrid[pos.getPosY()][pos.getPosX()]) { ySpd = 0; onFloor = true; } else { ySpd = 2; onFloor = false; } if (moving) { if (dir == 1 && x > 0) { if (!physicGrid[pos.getPosY() - 1][pos.getPosX() - 1]) { xSpd = -2; System.out.println(physicGrid[pos.getPosY() - 1][pos.getPosX() - 1]); } } if (dir == 2 && x < 380) { if (!physicGrid[pos.getPosY() - 1][pos.getPosX() + 1]) { xSpd = 2; System.out.print(pos.getPosX() + " "); System.out.println(pos.getPosY()); } } } else { xSpd = 0; } if (jumping) { if (!physicGrid[pos.getPosY() - 1][pos.getPosX()]) { if (yB - 50 < y && y > 0) { ySpd = -3; } else { ySpd = 0; } } else { ySpd = 0; } } if (y > 420) { x = (int)levelGrid[2][10].getX();; y = (int)levelGrid[2][10].getY(); } } @Override public void keyTyped(KeyEvent ke) {} @Override public void keyPressed(KeyEvent ke) { switch (ke.getKeyCode()) { case KeyEvent.VK_LEFT: moving = true; dir = 1; break; case KeyEvent.VK_RIGHT: moving = true; dir = 2; break; case KeyEvent.VK_SPACE: if (onFloor) { jumping = true; yB = y; } default: break; } } @Override public void keyReleased(KeyEvent ke) { switch (ke.getKeyCode()) { case KeyEvent.VK_LEFT: moving = false; break; case KeyEvent.VK_RIGHT: moving = false; break; case KeyEvent.VK_SPACE: jumping = false; break; default: break; } } } private class Position { int posX, posY; public Position(int posX, int posY) { this.posX = posX; this.posY = posY; } public void setPos(int posX, int posY) { this.posX = posX; this.posY = posY; } public int getPosX() { return posY; } public int getPosY() { return posX; } } } |
As you can see, I have made a mess, trying to fix things.. And I messed up somehow on handling the axises properly... The problems is in the TODO in the head comment, but what is most urgent to fix for me, is to avoid being able to roll into a platform from the side. So, should I scrap this code, and start anew in a different way, or can this work?
|
|
|
|
|
6
|
Game Development / Newbie & Debugging Questions / Umm.. Hi?
|
on: 2012-03-24 01:10:47
|
I started learning Java quite recently, after my elder brother who studies computer engineering gave me his Java textbook. I made 'HelloWorld' in notepad, and then moved on to TextPad to make the compiling easier, but when I started using several classes, I moved on yet again, now to NetBeans. Probably a bit too early, but oh well... Right now, I'm trying to make small games to practice, but somehow, there is always some sort of problem, and could really need help from experienced programmers, in addition to the book. Right now, I'm trying to write a basic platformer with move and jump functions. I have created a grid-system using an array of rectangles, and then trying to wrap the program around this. It works fairly well, but somethings is a bit ...odd. I just thought I'd ask first, can I post the entire game code here for feedback? Or will I get killed and banned and cursed? It's just one .java-file yet, on aprox. 250 lines.. I have read around on the forum a bit now, and everyone here seems very friendly and nice (and so many skilled people!). I hope I will become a part of your comunity, and one day, an asset, that can help those who at that time is at the level I am at now 
|
|
|
|
|