Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Game Development
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Game Development
Only part of the land shows up when I try to make it move.
Aaron Collins
Greenhorn
Posts: 3
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
For some reason, its fine when I don't make it move, but when I make it move, it only displays some of the land (in a diagonal
pattern
)
Here is my paint method:
public void paint(Graphics g) { super.paintComponent(g); // sky g.drawImage(Sky, 0, 0, 1600, 800, this); // clouds // for (int i = 0; i < cx.length; i++) { // g.setColor(Color.white); // g.fillOval(cx[i], cy[i], 200, 75); // } // land for (int i = 0; i < levelpixelstotal; i++) { xDisplay += 80; if (xDisplay >= 1600) { xDisplay = 0; yDisplay += 80; } if (leveldata[i] == Dirtid) { g.drawImage(Dirt, (xDisplay += moveland), (yDisplay += moveland), 80, 80, this); } if (leveldata[i] == Grassid) { g.drawImage(Grass, (xDisplay += moveland), (yDisplay += moveland), 80, 80, this); } } // character g.drawImage(Character, x, y, 80, 160, this); xDisplay = -80; yDisplay = 80; moveland++; }
And if you need more information, here is my full code:
package Window; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Formatter; import java.util.Scanner; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class Game extends JPanel implements Runnable, WindowListener, KeyListener { private static final long serialVersionUID = 1L; long milliseconds = System.currentTimeMillis(); long fpswait = 1000 / 32; public boolean running; int movingdir; int speed = 7; public int closingfalures = -1; public int moveland = 160; public int ranonce = 0; public int[] cx = new int[] { 100, 185, 275, 220, 250, 350 }; public int[] cy = new int[] { 100, 75, 75, 100, 100, 100 }; public int cloudspeed = 1; BufferedImage Sky = null; BufferedImage Dirt = null; BufferedImage Grass = null; BufferedImage Character = null; int frictionjumping = 1; int numberoftimesjumped = 0; int frictionwalking = 5; public static final int levelwidth = 160; public static final int levelheight = 240; public int levelpixelstotal = (levelheight * levelwidth); public int[] leveldata = new int[levelpixelstotal]; public int jumpingpower = 10; public int xDisplay = -80; public int yDisplay = 80; boolean trytostopagain; public int gravity = 1; public int levelloadednothingcount = 0; static final public int Airid = 0; static final public int Grassid = 1; static final public int Dirtid = 2; static final int WIDTH = 1600; static final int HEIGHT = 800; static final String TITLE = "SandBox Pre Alpha 2.0"; public int x = 800; public int y = 490; int volleft = 1; int volright = 1; public int volup = 1; public int voldown = 1; public boolean isupkeypressed = false; public boolean isleftkeypressed = false; public boolean isrightkeypressed = false; int volmultiplier = 1; boolean starting; public static void main(String[] args) throws IOException { Game game = new Game(); JFrame frame = new JFrame(); frame.add(game); frame.pack(); frame.setTitle(TITLE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(WIDTH, HEIGHT); frame.setLocationRelativeTo(null); frame.setResizable(false); frame.setVisible(true); frame.addKeyListener(game); frame.setFocusable(true); frame.setFocusTraversalKeysEnabled(true); frame.addWindowListener(game); game.start(); game.startthread(); } public void startthread() { Thread thread = new Thread(this); thread.start(); } public void stop() throws IOException { closingfalures++; running = false; saveLevel(); System.exit(0); } public void start() throws IOException { try { Sky = ImageIO.read(new File("res//background.png")); } catch (IOException e) { JOptionPane.showMessageDialog(null, "WARNING:Error loading Image: " + e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); } try { Dirt = ImageIO.read(new File("res//Dirt.png")); } catch (IOException e) { JOptionPane.showMessageDialog(null, "WARNING:Error loading Image: " + e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); } try { Grass = ImageIO.read(new File("res//Grass.png")); } catch (IOException e) { JOptionPane.showMessageDialog(null, "WARNING:Error loading Image: " + e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); } try { Character = ImageIO.read(new File("res//Character.png")); } catch (IOException e) { JOptionPane.showMessageDialog(null, "WARNING:Error loading Image: " + e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE); } running = true; loadlevel(); System.out.println(levelpixelstotal); } public void update() { try { Thread.sleep(fpswait); movevol(); repaint(); } catch (Exception e) { e.printStackTrace(); } } public void run() { while (running) { update(); } } public void paint(Graphics g) { super.paintComponent(g); // sky g.drawImage(Sky, 0, 0, 1600, 800, this); // clouds // for (int i = 0; i < cx.length; i++) { // g.setColor(Color.white); // g.fillOval(cx[i], cy[i], 200, 75); // } // land for (int i = 0; i < levelpixelstotal; i++) { xDisplay += 80; if (xDisplay >= 1600) { xDisplay = 0; yDisplay += 80; } if (leveldata[i] == Dirtid) { g.drawImage(Dirt, (xDisplay += moveland), (yDisplay += moveland), 80, 80, this); } if (leveldata[i] == Grassid) { g.drawImage(Grass, (xDisplay += moveland), (yDisplay += moveland), 80, 80, this); } } // character g.drawImage(Character, x, y, 80, 160, this); xDisplay = -80; yDisplay = 80; moveland++; } @Override public void windowActivated(WindowEvent e) { } @Override public void windowClosed(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { try { trytostopagain = true; if (trytostopagain) { stop(); if (closingfalures == 1) { System.exit(0); } } } catch (IOException e1) { e1.printStackTrace(); trytostopagain = true; } } @Override public void windowDeactivated(WindowEvent e) { } @Override public void windowDeiconified(WindowEvent e) { } @Override public void windowIconified(WindowEvent e) { } @Override public void windowOpened(WindowEvent e) { } public Object getLevelWidth() { return levelwidth; } public Object getLevelHeight() { return levelheight; } public int getLevelPixelsTotal() { return levelpixelstotal; } public int[] getLevelData(int i) { return leveldata; } public void actionPerformed(ActionEvent e) { } public void keyPressed(KeyEvent e) { int c = e.getKeyCode(); if ((c == KeyEvent.VK_UP) || (c == KeyEvent.VK_SPACE)|| (c == KeyEvent.VK_W)) { isupkeypressed = true; this.move(1); } if ((c == KeyEvent.VK_RIGHT) || (c == KeyEvent.VK_D)) { this.move(3); isrightkeypressed = true; } if ((c == KeyEvent.VK_LEFT) || (c == KeyEvent.VK_A)) { this.move(7); isleftkeypressed = true; } } public void movevol() { if (isleftkeypressed) { x -= speed; //if ((x > 0) && (x < 400) && (isleftkeypressed)) { //} volright -= speed; volleft -= frictionwalking; } if (isrightkeypressed) { x += speed; //if ((x > 1200) && (x < 1600) && (isrightkeypressed)) { //} volleft -= speed; volright -= frictionwalking; } if (volup > 1) { y -= speed; //if ((y > 0) && (y < 200)) { //} voldown -= speed; volup -= frictionjumping; numberoftimesjumped = 1; } if (voldown > 1) { y += speed; //if ((y > 600) && (y < 800)) { //} volup -= speed; voldown -= frictionwalking; } //ground if ((y < 800) && (y > 490)) { y = 490; } if (volup <= 0) { volup = 1; } if (voldown <= 0) { voldown = 1; } if (volright <= 0) { volright = 1; } if (volleft <= 0) { volleft = 1; } volup -= gravity; voldown += gravity; for (int i = 0; i < cx.length; i++) { if (cx[i] > 1600) { cx[i] = -200; } cx[i]++; } } public void move(int movingdir) { if (movingdir == 1) { volup += jumpingpower; } if (movingdir == 3) { volright += speed; } if (movingdir == 7) { volleft += speed; } } public void keyReleased(KeyEvent e) { int c = e.getKeyCode(); if ((c == KeyEvent.VK_UP) || (c == KeyEvent.VK_SPACE)|| (c == KeyEvent.VK_W)) { isupkeypressed = false; } if ((c == KeyEvent.VK_RIGHT) || (c == KeyEvent.VK_D)) { isrightkeypressed = false; } if ((c == KeyEvent.VK_LEFT) || (c == KeyEvent.VK_A)) { isleftkeypressed = false; } } public void keyTyped(KeyEvent e) { } public void loadlevel() { try { Scanner scanner = new Scanner(new File("res//leveldata.txt")); try { while (scanner.hasNext()) { int data = scanner.nextInt(); System.out.println(data); for (int i = 0; i < levelpixelstotal; i++) { leveldata[i] = data; } } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "WARNING:Error loading level: " + e.getMessage() + " Hit ok to create the level", "ERROR", JOptionPane.ERROR_MESSAGE); generatelevel(); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "WARNING:Error loading level: " + e.getMessage() + " Hit ok to create the file", "ERROR!", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); generatelevel(); } ; } public void saveLevel() throws IOException { Formatter formatter = new Formatter("res//leveldata.txt"); try { for (int i = 0; i < levelpixelstotal; i++) { System.out.println(leveldata[i]); formatter.format("%d\n", leveldata[i]); } formatter.flush(); } finally { formatter.close(); } } public void generatelevel() { for (int i = 0; i < levelpixelstotal; i++) { if (i > (levelpixelstotal / 2)) { leveldata[i] = Dirtid; } if ((leveldata[i] == Dirtid) && (i > ((levelwidth * levelheight) / 2)) && (i > (levelwidth * (levelheight) / 2) + levelwidth)) { leveldata[i] = Grassid; } } try { stop(); } catch (IOException e) { e.printStackTrace(); } } }
Aaron Collins
Greenhorn
Posts: 3
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
nvm I fixed the problem
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Trying to do a one spot battleship program with computer players.
a baffling problem
slight problem using JScrollPane
How to make card game like rummy card game.
unreported exception java.io.IOexception; must be caught or declared to be thrown
More...