Show Posts
|
|
Pages: [1] 2
|
|
3
|
Game Development / Newbie & Debugging Questions / Re: Nothing Happens in my code:P Maze Code Implementation
|
on: 2012-01-17 06:27:37
|
Why do you think your map generation code is wrong? If you are getting no errors and the window is not popping up, then there is an error with your window initialization  Well before I added the Map generation code the program worked well but when i added it it wouldn't work for some reason. I tested it out both ways with and withought the map generation. The one withought map generation worked and not the one with it.
|
|
|
|
|
4
|
Game Development / Newbie & Debugging Questions / Nothing Happens in my code:P Maze Code Implementation
|
on: 2012-01-17 06:08:59
|
There seems to be a problem with my code. For some reason my window will not popup for some reason. I will only type in the code that seems to be wrong. No error pops up its just that nothing will happen. I just implemented a random maze generation code in the maps and mapdirector files but nothing happens after i implemented the new code. Please help or at least tell me whats wrong. Maps.java1 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
| import java.util.*;
public class Maps{ private int SizeX = 33; private int SizeY = 33; public short RandomMaze[][] = new short[SizeX][SizeY]; private Random r = new Random(); public Maps(){ GenerateMaze(r.nextInt(70) + 30); } public void GenerateMaze(int StraightNess){ int CurrentX = 50; int CurrentY = 50; boolean TimeUp = false; boolean tocheckDirection = false; boolean Moved = false; boolean ChangeDirection = false; boolean Done = false; int Dir = 0; int LastDirection = r.nextInt(4); int Open = 2; int NotOpen = 3; int North = Open; int South = Open; int East = Open; int West = Open; short Flr = 0; short Wall = 1; long LastDrawTimer = 0; for(int y = 0;y < RandomMaze.length;y++){ for(int x = 0;x < RandomMaze[y].length;x++){ RandomMaze[y][x] = 1; } } CurrentX = 50; CurrentY = 50; while(TimeUp != true){ if(tocheckDirection != true){ Moved = false; ChangeDirection = true; if(r.nextInt(100) < StraightNess){ ChangeDirection = false; Dir = LastDirection; } while(Moved != true){ if(ChangeDirection == true){ Dir = r.nextInt(4); LastDirection = Dir; } } ChangeDirection = true; switch(Dir){ case 0: if(North == Open){ Moved = true; CurrentY--; } break; case 1: if(South == Open){ Moved = true; CurrentY++; } break; case 2: if(East == Open){ Moved = true; CurrentX++; } break; case 3: if(West == Open){ Moved = true; CurrentX--; } break; } } RandomMaze[CurrentX][CurrentY] = Flr; LastDrawTimer = System.currentTimeMillis(); } North = NotOpen; South = NotOpen; East = NotOpen; West = NotOpen; if(CurrentY - 2 < 0){ North = NotOpen; }else if(RandomMaze[CurrentX][CurrentY - 1] == Wall && RandomMaze[CurrentX][CurrentY - 2] == Wall){ if(RandomMaze[CurrentX - 1][CurrentY - 1] == Wall && RandomMaze[CurrentX + 1][CurrentY - 1] == Wall){ North = Open; }else{ North = NotOpen; } } if(CurrentY + 2 < 0){ South = NotOpen; }else if(RandomMaze[CurrentX][CurrentY + 1] == Wall && RandomMaze[CurrentX][CurrentY + 2] == Wall){ if(RandomMaze[CurrentX - 1][CurrentY + 1] == Wall && RandomMaze[CurrentX + 1][CurrentY + 1] == Wall){ South = Open; }else{ South = NotOpen; } } if(CurrentX + 2 < 0){ East = NotOpen; }else if(RandomMaze[CurrentX + 1][CurrentY] == Wall && RandomMaze[CurrentX + 2][CurrentY] == Wall){ if(RandomMaze[CurrentX + 1][CurrentY - 1] == Wall && RandomMaze[CurrentX + 1][CurrentY + 1] == Wall){ East = Open; }else{ East = NotOpen; } } if(CurrentX - 2 < 0){ West = NotOpen; }else if(RandomMaze[CurrentX - 1][CurrentY] == Wall && RandomMaze[CurrentX - 2][CurrentY] == Wall){ if(RandomMaze[CurrentX - 1][CurrentY - 1] == Wall && RandomMaze[CurrentX - 1][CurrentY + 1] == Wall){ West = Open; }else{ West = NotOpen; } } if(System.currentTimeMillis() - LastDrawTimer > 100){TimeUp = true;} if(North == NotOpen && South == NotOpen && East == NotOpen && West == NotOpen && TimeUp == false){ Done = false; while(Done != true){ CurrentX = 50; CurrentY = 50; if(RandomMaze[CurrentX][CurrentY] == Flr){Done = true;} } } } } |
MapDirector.java1 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
| import java.util.*; import java.awt.*;
public class MapDirector{ Maps M = new Maps(); Prey prey; ArrayList<Floor> floor = new ArrayList<Floor>(); ArrayList<Wall> wall = new ArrayList<Wall>(); ArrayList<Wall> wallT = new ArrayList<Wall>(); ArrayList<Exit> exit = new ArrayList<Exit>(); private short Maze1[][] = M.RandomMaze; private Random r = new Random(); public static boolean WIN = false; public MapDirector(){ M.GenerateMaze(r.nextInt(70) + 30); prey = new Prey(50,50,500,270); for(int y = 0;y < Maze1.length;y++){ for(int x = 0;x < Maze1[y].length;x++){ if(Maze1[y][x] == 0){floor.add(new Floor(x*50,y*46));} if(Maze1[y][x] == 1){wall.add(new Wall(x*50,y*46,1));wall.add(new Wall(x*50,y*46-4,1)); wall.add(new Wall(x*50,y*46-8,1));wallT.add(new Wall(x*50,y*46-12,1));} if(Maze1[y][x] == 2){floor.add(new Floor(x*50,y*46));} } } } public void WallCollision(Wall w){ Rectangle wall = new Rectangle(w.x,w.y,50,50); Rectangle U = new Rectangle(Prey.x + 20,Prey.y + 10,13,5); Rectangle D = new Rectangle(Prey.x + 20,Prey.y + 20,13,5); Rectangle L = new Rectangle(Prey.x + 17,Prey.y + 20,5,5); Rectangle R = new Rectangle(Prey.x + 30,Prey.y + 20,5,5); if(U.intersects(wall)){Prey.y += 2;Prey.gy -= 2;} else if(D.intersects(wall)){Prey.y -= 2;Prey.gy += 2;} else if(L.intersects(wall)){Prey.x += 2;Prey.gx -= 2;} else if(R.intersects(wall)){Prey.x -= 2;Prey.gx += 2;} } public void Exit(Exit e){ Rectangle exit = new Rectangle(e.x,e.y,50,50); Rectangle PREY = new Rectangle(Prey.x,Prey.y,50,50); if(PREY.intersects(exit)){WIN = true;} } public void paint(Graphics g,Graphics2D g2d){ g2d = (Graphics2D) g; g2d.translate(Prey.gx,Prey.gy); for(Floor f : floor){g2d.drawImage(Floor.floor,f.x,f.y,50,50,null);} for(Wall w : wall){g2d.drawImage(Wall.wall,w.x,w.y,50,50,null);} prey.paint(g,g2d); for(Wall w : wallT){g2d.drawImage(Wall.wall,w.x,w.y,50,50,null);} } } |
|
|
|
|
|
9
|
Game Development / Game Play & Game Design / Re: Slow startup
|
on: 2012-01-15 19:14:46
|
Ok so I changed some code in the wall class, the floor class, and the MapDirector class. MapDirector.java 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
| import java.util.*; import java.awt.*;
public class MapDirector{ Maps M = new Maps(); Prey prey; ArrayList<Floor> floor = new ArrayList<Floor>(); ArrayList<Wall> wall = new ArrayList<Wall>(); ArrayList<Wall> wallT = new ArrayList<Wall>(); private int Maze1[][] = M.Maze1; public MapDirector(){ prey = new Prey(50,50,500,270); for(int y = 0;y < Maze1.length;y++){ for(int x = 0;x < Maze1[y].length;x++){ if(Maze1[y][x] == 0){floor.add(new Floor(x*50,y*46));} if(Maze1[y][x] == 1){wall.add(new Wall(x*50,y*46,1));wall.add(new Wall(x*50,y*46-4,1)); wall.add(new Wall(x*50,y*46-8,1));wallT.add(new Wall(x*50,y*46-12,1));} } } } public void WallCollision(Wall w){ Rectangle wall = new Rectangle(w.x,w.y,50,50); Rectangle U = new Rectangle(Prey.x + 20,Prey.y + 10,13,5); Rectangle D = new Rectangle(Prey.x + 20,Prey.y + 20,13,5); Rectangle L = new Rectangle(Prey.x + 17,Prey.y + 20,5,5); Rectangle R = new Rectangle(Prey.x + 30,Prey.y + 20,5,5); if(U.intersects(wall)){Prey.y += 2;Prey.gy -= 2;} else if(D.intersects(wall)){Prey.y -= 2;Prey.gy += 2;} else if(L.intersects(wall)){Prey.x += 2;Prey.gx -= 2;} else if(R.intersects(wall)){Prey.x -= 2;Prey.gx += 2;} } public void paint(Graphics g,Graphics2D g2d){ g2d = (Graphics2D) g; g2d.translate(Prey.gx,Prey.gy); for(Floor f : floor){g2d.drawImage(Floor.floor,f.x,f.y,50,50,null);} for(Wall w : wall){g2d.drawImage(Wall.wall,w.x,w.y,50,50,null);} prey.paint(g,g2d); for(Wall w : wallT){g2d.drawImage(Wall.wall,w.x,w.y,50,50,null);} } } |
Floor.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.awt.image.*; import java.io.*;
public class Floor{ SpriteLoader SL = new SpriteLoader(); BufferedImage FloorSheet = null; static BufferedImage floor; public int x,y; public Floor(int x,int y){ this.x = x; this.y = y; try { FloorSheet = SL.LoadImage("Images/Floor.png"); } catch (IOException e) { e.printStackTrace(); } SpriteSheetHolder SSH = new SpriteSheetHolder(FloorSheet); floor = SSH.GrabSprite(0,0,50,50); } } |
Wall.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import java.awt.image.*; import java.io.*;
public class Wall{ SpriteLoader SL = new SpriteLoader(); BufferedImage WallSheet = null; static BufferedImage wall; public int x,y,Direction; public Wall(int x,int y,int Direction){ this.x = x; this.y = y; this.Direction = Direction; try { WallSheet = SL.LoadImage("Images/Wall.png"); } catch (IOException e) { e.printStackTrace(); } SpriteSheetHolder SSH = new SpriteSheetHolder(WallSheet); if(Direction == 0){wall = SSH.GrabSprite(0,0,50,50);} else if(Direction == 1){wall = SSH.GrabSprite(50,0,50,50);} } } |
|
|
|
|
|
10
|
Game Development / Game Play & Game Design / Re: Slow startup
|
on: 2012-01-15 18:58:54
|
Usually you would want to create some kind of Art/Asset manager but here you should be safe making a static Image variable in Wall and Floor that all instances share.
I understand what static means, but what will changing the bufferedImages static going to do? Sorry for questioning just curious.
|
|
|
|
|
12
|
Game Development / Game Play & Game Design / Re: Slow startup
|
on: 2012-01-15 06:57:02
|
well first of what is exactly is slow ? do some system.outs and find out what is the slow part, and then by slow, how slow are we talking ?
How can you use System.outs to find the parts that are slow? Is it like System.out.println("ERROR "+e); inside a try and catch block? Or does it have to be something else other than an Exception.
|
|
|
|
|
13
|
Game Development / Game Play & Game Design / Re: Slow startup
|
on: 2012-01-15 05:38:10
|
well first of what is exactly is slow ? do some system.outs and find out what is the slow part, and then by slow, how slow are we talking ?
Well I mean when I double click on the jar it takes some time for it to startup. Like right when I double click on the jar it takes a few seconds then the window pops up.
|
|
|
|
|
14
|
Game Development / Game Play & Game Design / Re: Slow startup
|
on: 2012-01-15 04:45:08
|
|
So then should I lessen the amount of graphics objects in the game? Should I also remove the keyListener in prey.java? Also what happens if I add more than one run method, is it good or bad?
|
|
|
|
|
15
|
Game Development / Game Play & Game Design / Re: Slow startup
|
on: 2012-01-15 03:38:43
|
Here is some code I have alot so sorry... MainWindow.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import javax.swing.*; import java.awt.*;
public class MainWindow extends JFrame{ private static final long serialVersionUID = 1L; MainGamePanel MGP = new MainGamePanel(); public MainWindow(){ this.setTitle("Mind Maze"); this.setSize(1300,700); this.add(MGP); this.setResizable(false); this.setVisible(true); this.setLocationRelativeTo(null); this.toFront(); } public static void main(String[] args){ MainWindow MW = new MainWindow(); } } |
MainGamePanel.java 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
| import java.awt.event.*; import javax.swing.*; import java.awt.*;
public class MainGamePanel extends JPanel{ private static final long serialVersionUID = 1L; SinglePlayer SP = new SinglePlayer(); Thread SinglePlay = new Thread(SP); private boolean Start = false; public MainGamePanel(){ this.add(SP); this.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ int x = e.getX(); int y = e.getY(); if(x > 325 && x < 325 + 70 && y > 235 && y < 235 + 70 && !Start){ StartGame(); } } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} }); } public void StartGame(){ Start = true; SP.running = true; SinglePlay.start(); } public void paint(Graphics g){ if(!Start){ g.setColor(Color.GREEN); g.fillRect(325,235,70,30); g.setColor(Color.BLACK); g.drawRect(325,235,70,30); g.drawString("Play",325+23,235+20); repaint(); }else{ SP.paintComponent(g); repaint(); } } } |
SinglePlayer.java 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
| import java.awt.event.*; import javax.swing.*; import java.awt.*;
public class SinglePlayer extends JPanel implements Runnable{ private static final long serialVersionUID = 1L; private Image dbImage; private Graphics dbg; MapDirector MD = new MapDirector(); public boolean running = false; public SinglePlayer(){ this.setFocusable(true); this.requestFocus(); this.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){MD.prey.keyPressed(e);} public void keyReleased(KeyEvent e){MD.prey.keyReleased(e);} public void keyTyped(KeyEvent e){MD.prey.keyTyped(e);} }); } public void paint(Graphics g){ dbImage = createImage(getWidth(),getHeight()); dbg = dbImage.getGraphics(); paintComponent(dbg); g.drawImage(dbImage,0,0,this); } public void paintComponent(Graphics g){ Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.WHITE); g2d.fillRect(0,0,1300,700); MD.paint(g,g2d); g2d.setColor(Color.BLACK); } public void run(){ while(running){ MD.prey.Movement(); for(int i = 0;i < MD.wallT.size();i++){ MD.WallCollision(MD.wallT.get(i)); } repaint(); try{ Thread.sleep(20); }catch(Exception e){System.out.println("!!!ERROR!!!");} } } } |
MapDirector.java 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
| import java.util.*; import java.awt.*;
public class MapDirector{ Maps M = new Maps(); Prey prey; private int random; ArrayList<Floor> floor = new ArrayList<Floor>(); ArrayList<Wall> wall = new ArrayList<Wall>(); ArrayList<Wall> wallT = new ArrayList<Wall>(); private int Maze1[][] = M.Maze1; public MapDirector(){ prey = new Prey(50,50,500,270); random = 0; random++; for(int y = 0;y < Maze1.length;y++){ for(int x = 0;x < Maze1[y].length;x++){ if(random == 1||random == 5||random == 9||random == 13){ if(Maze1[y][x] == 2){Maze1[y][x] = 0;} if(Maze1[y][x] == 3){Maze1[y][x] = 1;} if(Maze1[y][x] == 4){Maze1[y][x] = 0;} if(Maze1[y][x] == 5){Maze1[y][x] = 1;} } else if(random == 2||random == 6||random == 10||random == 14){ if(Maze1[y][x] == 2){Maze1[y][x] = 1;} if(Maze1[y][x] == 3){Maze1[y][x] = 0;} if(Maze1[y][x] == 4){Maze1[y][x] = 1;} if(Maze1[y][x] == 5){Maze1[y][x] = 0;} } else if(random == 3||random == 7||random == 11||random == 15){ if(Maze1[y][x] == 2){Maze1[y][x] = 1;} if(Maze1[y][x] == 3){Maze1[y][x] = 1;} if(Maze1[y][x] == 4){Maze1[y][x] = 1;} if(Maze1[y][x] == 5){Maze1[y][x] = 0;} } else if(random == 4||random == 8||random == 12||random == 16){ if(Maze1[y][x] == 2){Maze1[y][x] = 0;} if(Maze1[y][x] == 3){Maze1[y][x] = 1;} if(Maze1[y][x] == 4){Maze1[y][x] = 1;} if(Maze1[y][x] == 5){Maze1[y][x] = 1;} } if(Maze1[y][x] == 0){floor.add(new Floor(x*50,y*46));} if(Maze1[y][x] == 1){wall.add(new Wall(x*50,y*46,0));wall.add(new Wall(x*50,y*46-4,0)); wall.add(new Wall(x*50,y*46-8,0));wallT.add(new Wall(x*50,y*46-12,1));} } } } public void WallCollision(Wall w){ Rectangle wall = new Rectangle(w.x,w.y,50,50); Rectangle U = new Rectangle(prey.x + 20,prey.y + 10,13,5); Rectangle D = new Rectangle(prey.x + 20,prey.y + 20,13,5); Rectangle L = new Rectangle(prey.x + 12,prey.y + 20,5,5); Rectangle R = new Rectangle(prey.x + 35,prey.y + 20,5,5); if(U.intersects(wall)){prey.y += 2;prey.gy -= 2;} else if(D.intersects(wall)){prey.y -= 2;prey.gy += 2;} else if(L.intersects(wall)){prey.x += 2;prey.gx -= 2;} else if(R.intersects(wall)){prey.x -= 2;prey.gx += 2;} } public void paint(Graphics g,Graphics2D g2d){ g2d = (Graphics2D) g; g2d.translate(prey.gx,prey.gy); for(Floor f : floor){f.paint(g,g2d);} for(Wall w : wall){w.paint(g,g2d);} prey.paint(g,g2d); for(Wall w : wallT){w.paint(g,g2d);} } } |
Maps.java 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
| import java.util.*; import java.io.*;
public class Maps{ Scanner s; public int Maze1[][] = new int[23][33]; public Maps(){ Maze1(); } public void Maze1(){ try{ s = new Scanner(new File("C:/Users/LordBerzerker97/Desktop/Games/MindMaze/CodeMM/src/MapHolder/Maze1.txt")).useDelimiter(","); }catch(Exception e) {System.out.println(e + "ERROR");} for(int y = 0;y < Maze1.length;y++){ for(int x = 0;x < Maze1[y].length;x++){ if(s.hasNextInt()){ Maze1[y][x] = s.nextInt(); } } if(s.hasNextLine()){ s.nextLine(); } } } } |
Prey.java 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
| import java.awt.event.*; import java.awt.image.*; import java.awt.*; import java.io.*;
public class Prey implements KeyListener{ SpriteLoader SL = new SpriteLoader(); BufferedImage preySheet = null; BufferedImage preyL,preyR,preyU,preyD; public int vx,vy,x,y,gx,gy; public Prey(int x,int y,int gx,int gy){ this.x = x; this.y = y; this.gx = gx; this.gy = gy; try { preySheet = SL.LoadImage("Images/Prey.png"); } catch (IOException e) { e.printStackTrace(); } SpriteSheetHolder SSH = new SpriteSheetHolder(preySheet); preyD = SSH.GrabSprite(0,0,50,50); } public void paint(Graphics g,Graphics2D g2d){ g2d = (Graphics2D) g; g2d.drawImage(preyD,x,y,50,50,null); } public void Movement(){ x += vx; y += vy; gx -= vx; gy -= vy; } public void VelocityX(int vx){this.vx = vx;} public void VelocityY(int vy){this.vy = vy;} public void keyPressed(KeyEvent e){ int KeyCode = e.getKeyCode(); if(KeyCode == KeyEvent.VK_A){VelocityX(-2);} else if(KeyCode == KeyEvent.VK_W){VelocityY(-2);} else if(KeyCode == KeyEvent.VK_S){VelocityY(+2);} else if(KeyCode == KeyEvent.VK_D){VelocityX(+2);} } public void keyReleased(KeyEvent e){ int KeyCode = e.getKeyCode(); if(KeyCode == KeyEvent.VK_A){VelocityX(0);} else if(KeyCode == KeyEvent.VK_W){VelocityY(0);} else if(KeyCode == KeyEvent.VK_S){VelocityY(0);} else if(KeyCode == KeyEvent.VK_D){VelocityX(0);} } public void keyTyped(KeyEvent e){} } |
SpriteLoader.java 1 2 3 4 5 6 7 8 9 10 11 12
| import java.awt.image.*; import javax.imageio.*; import java.net.*; import java.io.*;
public class SpriteLoader{ public BufferedImage LoadImage(String ImagePath) throws IOException{ URL url = this.getClass().getResource(ImagePath); BufferedImage img = ImageIO.read(url); return img; } } |
SpriteSheetHolder.java 1 2 3 4 5 6 7 8 9 10 11 12
| import java.awt.image.*;
public class SpriteSheetHolder{ public BufferedImage SpriteSheetHold; public SpriteSheetHolder(BufferedImage SSH){ this.SpriteSheetHold = SSH; } public BufferedImage GrabSprite(int x,int y,int width,int height){ BufferedImage sprite = SpriteSheetHold.getSubimage(x,y,width,height); return sprite; } } |
Wall.java 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
| import java.awt.image.*; import java.awt.*; import java.io.*;
public class Wall{ SpriteLoader SL = new SpriteLoader(); BufferedImage WallSheet = null; BufferedImage wall; public int x,y,Direction; public Wall(int x,int y,int Direction){ this.x = x; this.y = y; this.Direction = Direction; try { WallSheet = SL.LoadImage("Images/Wall.png"); } catch (IOException e) { e.printStackTrace(); } SpriteSheetHolder SSH = new SpriteSheetHolder(WallSheet); if(Direction == 0){wall = SSH.GrabSprite(0,0,50,50);} else if(Direction == 1){wall = SSH.GrabSprite(50,0,50,50);} } public void paint(Graphics g,Graphics2D g2d){ g2d = (Graphics2D) g; g2d.drawImage(wall,x,y,50,50,null); } } |
Floor.java 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
| import java.awt.image.*; import java.awt.*; import java.io.*;
public class Floor{ SpriteLoader SL = new SpriteLoader(); BufferedImage FloorSheet = null; BufferedImage Floor; int x,y; public Floor(int x,int y){ this.x = x; this.y = y; try { FloorSheet = SL.LoadImage("Images/Floor.png"); } catch (IOException e) { e.printStackTrace(); } SpriteSheetHolder SSH = new SpriteSheetHolder(FloorSheet); Floor = SSH.GrabSprite(0,0,50,50); } public void paint(Graphics g,Graphics2D g2d){ g2d = (Graphics2D) g; g2d.drawImage(Floor,x,y,50,50,null); } } |
Sorry for the lots of code. I tried creating a splash screen once but it didn't work either so I removed it.
|
|
|
|
|
16
|
Game Development / Game Play & Game Design / Slow startup
|
on: 2012-01-15 01:53:21
|
|
Sry guys for asking, but my game has a slow startup and I want create a loading screen to keep the person busy until the game starts up. So does anyone know how to do this or find a faster way to startup the program. My dad said to optimise the programming code and I don't know what he means by that.
|
|
|
|
|
20
|
Game Development / Game Play & Game Design / Dungeon generation 2d array
|
on: 2012-01-13 04:59:16
|
|
Hello guys. I need help with my game again. I am currently trying to figure out how to create randomized dungeons. I currently know how to tile map games and what I really want to know is how to create dungeons. Please give me some steps on how to do it. Or some code will be nice.
|
|
|
|
|
22
|
Game Development / Game Play & Game Design / Java Camera Navigation (no external Libraries)
|
on: 2012-01-12 01:55:33
|
|
Hello guys. I have a question. Does java have any form of 2D camera or at least something to navigate around the map? Also if you guys say that using the keyboard to move the environment around instead of the player, then please tell me how that is going to effect online play, will other players see the environment move around or will it just be one player?.
|
|
|
|
|
23
|
Games Center / 4K Game Competition - 2012 / Re: [WIP] Dawn
|
on: 2012-01-06 22:05:55
|
Well you know you could create 2 versions of the game. One for ASWD and one for QZSD. Or at least make an options menu so people can change that. Just saying that these are also other options. Arrow controls are fine too 
|
|
|
|
|
24
|
Game Development / Newbie & Debugging Questions / JAVA3D Universe problem
|
on: 2012-01-01 23:05:18
|
Sorry to bother but i have a problem with my code. You see something is wrong with my Applet. Every time I try to run the code this error start to pop up in the console. Java3D is an API I am using for 3D graphics. The error: java.lang.UnsatisfiedLinkError: no j3dcore-ogl in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at javax.media.j3d.NativePipeline$1.run(NativePipeline.java:231) at java.security.AccessController.doPrivileged(Native Method) at javax.media.j3d.NativePipeline.loadLibrary(NativePipeline.java:200) at javax.media.j3d.NativePipeline.loadLibraries(NativePipeline.java:157) at javax.media.j3d.MasterControl.loadLibraries(MasterControl.java:987) at javax.media.j3d.VirtualUniverse.<clinit>(VirtualUniverse.java:299) at javax.media.j3d.Canvas3D.<clinit>(Canvas3D.java:3881) at Code.MainGamePanel.<init>(MainGamePanel.java:14) at Code.MainWindow.<init>(MainWindow.java:  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) MainWindow.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package Code;
import java.applet.*; import java.awt.*;
public class MainWindow extends Applet{ private static final long serialVersionUID = 1L; MainGamePanel mgpanel = new MainGamePanel(); private static int FWIDTH = 800; private static int FHEIGHT = 500; public void init(){ setName("Dimensions"); add(mgpanel); setSize(FWIDTH,FHEIGHT); setBackground(Color.white); } public void start(){} public void stop(){} public void destroy(){} } |
MainGamePanel.java This is where the problem is. 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
| package Code;
import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import javax.media.j3d.*; import javax.swing.*; import java.awt.*;
public class MainGamePanel extends JPanel{ private static final long serialVersionUID = 1L; SimpleUniverse universe; public MainGamePanel(){ setLayout(new BorderLayout()); Canvas3D canvas = new Canvas3D(universe.getPreferredConfiguration()); add("Center",canvas); universe = new SimpleUniverse(canvas); BranchGroup scene = createSceneGraph(); universe.getViewingPlatform().setNominalViewingTransform(); scene.compile(); universe.addBranchGraph(scene); } public BranchGroup createSceneGraph(){ BranchGroup Bgroup = new BranchGroup(); ColorCube cube = new ColorCube(0.5f); Bgroup.addChild(cube); return Bgroup; } } |
|
|
|
|
|
25
|
Game Development / Newbie & Debugging Questions / Re: MouseMotionListener with tiles
|
on: 2011-12-23 00:08:40
|
|
Ok so I re-did all the classes that basically needed to be fixed. After that I found out that the x and y coordinates of the Rectangles and Objects: Grass and Rock Were not working correctly. The problem with the highlighting is still and issue. Although the lag problem has been fixed. So basically that's the only problem.
So do you guys know any kind of loop that is used to fill the whole screen?(I prefer a 'for' loop.)
|
|
|
|
|
29
|
Game Development / Newbie & Debugging Questions / MouseMotionListener with tiles
|
on: 2011-12-22 07:01:55
|
Hey all!! There seems to be a problem with my program. The problem is that when the mouse is over a tile it wont draw a yellow rectangle around it unless its the top, first tile. Here is some code: The window/JFrame. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package Holder;
import javax.swing.*;
public class Window extends JFrame{ private static final long serialVersionUID = 1L; GameCanvas GC = new GameCanvas(); public Window(){ this.setTitle("Stellar Factions"); this.setSize(507,529); this.add(GC); this.setResizable(false); this.setVisible(true); this.setLocationRelativeTo(null); } public static void main(String [] args){ new Window(); } } |
The gameCanvas/where I load all the images/UI. 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
| package Holder;
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class GameCanvas extends JPanel implements Runnable{ private static final long serialVersionUID = 1L; Maps m = new Maps(); private Image dbImage; private Graphics dbGraphics; private Rectangle[] rrect = new Rectangle[100]; int hx,hy,bx,by; boolean clicked = false; public GameCanvas(){ this.setFocusable(true); this.requestFocus(true); this.addMouseMotionListener(new MouseAdapter(){ public void mouseMoved(MouseEvent e){ hx = e.getX(); hy = e.getY(); for(int i = 0;i < 100;i++){ rrect[i] = new Rectangle(m.rect[i].x,m.rect[i].y); if(hx > rrect[i].x && hx < rrect[i].x + 50 && hy > rrect[i].y && hy < rrect[i].y + 50){ clicked = true; bx = rrect[i].x; by = rrect[i].y; break; }else{clicked = false;}}} public void mouseDragged(MouseEvent e){} }); Thread t = new Thread(this); t.start(); } public void paint(Graphics g){ dbImage = createImage(getWidth(),getHeight()); dbGraphics = dbImage.getGraphics(); paintComponent(dbGraphics); g.drawImage(dbImage,0,0,this); } public void paintComponent(Graphics g){ m.paint(g); if(clicked){ g.setColor(Color.YELLOW); g.drawRect(bx,by,50,50); } repaint(); } public void run(){ while(true){ try{ Thread.sleep(20); }catch(Exception e){ System.out.println("!!!ERROR!!!"); }}} } |
The tile map 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
| package Holder;
import java.awt.*;
public class Maps { public final int arrayNum = 100; Grass[] grass = new Grass[arrayNum/2]; Rocks[] rocks = new Rocks[arrayNum/2]; Rectangle[] rect = new Rectangle[arrayNum]; int[][] map = {{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,0,0,1,0,0,0}, {0,0,0,0,1,1,0,0,0,0}, {0,0,0,0,1,1,0,0,0,0}, {0,0,0,1,0,0,1,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}}; public void paint(Graphics g){ for(int i = 0;i < grass.length;i++){ for(int y = 0;y < map.length;y++){ for(int x = 0;x < map[y].length;x++){ for(int r = 0;r < 50;r++){ rect[r] = new Rectangle(x,y,50,50); if(map[y][x] == 0){ grass[i] = new Grass(rect[r].x*50,rect[r].y*50); grass[i].paint(g); }} for(int r = 50;r < 100;r++){ rect[r] = new Rectangle(x,y,50,50); if(map[y][x] == 1){ rocks[i] = new Rocks(rect[r].x*50,rect[r].y*50); rocks[i].paint(g); }} }}} } } |
The rock object. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package Holder;
import java.awt.*;
public class Rocks { int x,y; public Rocks(int x,int y){ this.x = x; this.y = y; } public void paint(Graphics g){ g.setColor(Color.GRAY); g.fillRect(x,y,50,50); g.setColor(Color.BLACK); g.drawRect(x,y,50,50); } } |
The Grass object. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package Holder;
import java.awt.*;
public class Grass { int x,y; public Grass(int x,int y){ this.x = x; this.y = y; } public void paint(Graphics g){ g.setColor(Color.GREEN); g.fillRect(x,y,50,50); g.setColor(Color.BLACK); g.drawRect(x,y,50,50); } } |
Please give some suggestions or code.
|
|
|
|
|