Results 1 to 4 of 4
Thread: Simple Game - Need Help
- 06-18-2012, 04:25 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Simple Game - Need Help
This is a simple game I'm trying to make. I'm trying to get it to where it will shoot a bullet and once you release the space bar i want it to keep going for a small amount of time but it won't. And occasionally it will crash
Java Code:package game.practice; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; @SuppressWarnings("serial") public class GamePractice extends JFrame implements Runnable { int x, y, xDirection, yDirection; static int bulletX; static int bulletY; Image dbImage; Graphics dbg; static boolean isShooting = false; public void run(){ try{ while(true){ move(); Thread.sleep(5); } }catch(Exception e){ System.err.println(e.getMessage()); } } public void move(){ x += xDirection; y += yDirection; if(x <= 0){ x = 0; } if(x >= 800){ x = 800; } if(y <= 0){ y = 0; } if(y >= 600){ y = 600; } } public void setXDirection(int xdir){ xDirection = xdir; } public void setYDirection(int ydir){ yDirection = ydir; } public class AL extends KeyAdapter{ public void keyPressed(KeyEvent e){ int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_LEFT){ setXDirection(-1); } if(keyCode == KeyEvent.VK_RIGHT){ setXDirection(+1); } if(keyCode == KeyEvent.VK_UP){ setYDirection(-1); } if(keyCode == KeyEvent.VK_DOWN){ setYDirection(+1); } if(keyCode == KeyEvent.VK_SPACE){ isShooting = true; } } public void keyReleased(KeyEvent e){ int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_LEFT){ setXDirection(0); } if(keyCode == KeyEvent.VK_RIGHT){ setXDirection(0); } if(keyCode == KeyEvent.VK_UP){ setYDirection(0); } if(keyCode == KeyEvent.VK_DOWN){ setYDirection(0); } if(keyCode == KeyEvent.VK_SPACE){ try { Thread.sleep(3000); } catch (InterruptedException e1) { e1.printStackTrace(); } bulletX = x; bulletY = y; isShooting = false; } } } public GamePractice(){ addKeyListener(new AL()); setTitle("Mediocre Game -- Pre-Alpha -- Version 2.0 -- Created By: Mitchell Butterfield"); setSize(800, 600); setLocationRelativeTo(null); setResizable(false); setVisible(true); setBackground(Color.BLUE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); x = 400; y = 400; } public void paint(Graphics g){ dbImage = createImage(getWidth(), getHeight()); dbg = dbImage.getGraphics(); paintComponent(dbg); if(isShooting == true){ paintBullet(dbg); } g.drawImage(dbImage, 0, 0, this); } public void paintComponent(Graphics g){ g.setColor(Color.DARK_GRAY); g.fillRect(x, y, 30, 30); g.setColor(Color.WHITE); g.drawString("X: " + x, 50, 50); g.drawString("Y: " + y, 50, 60); g.drawString("X Direction: " + xDirection, 50, 70); g.drawString("Y Direction: " + yDirection, 50, 80); g.drawString("Is Shooting: " + isShooting, 50, 90); g.drawString("Bullet X: " + bulletX, 50, 100); g.drawString("Bullet Y: " + bulletY, 50, 110); repaint(); } public void paintBullet(Graphics g){ g.fillOval(bulletX, bulletY, 5, 5); bulletY--; } public static void main(String[] args){ GamePractice p = new GamePractice(); Thread t1 = new Thread(p); t1.start(); } }
-
Re: Simple Game - Need Help
Why are you calling paintComponent directly? And why does paintComponent have a repaint call in it? Both are dangerous things to do. Also you shouldn't even be coding in the paint method. It appears that you haven't gone through the Swing graphics tutorials yet, and I recommend that you do this first. Then look into using a Swing Timer.
- 06-18-2012, 05:37 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Re: Simple Game - Need Help
Thank you but the link doesn't seem to be working.
- 06-18-2012, 11:29 AM #4
Re: Simple Game - Need Help
Something seems to have gone wrong with Fubarable's link. Here it is again: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Simple Java Game -- HELP :)
By Gamersunited22 in forum New To JavaReplies: 7Last Post: 04-04-2012, 06:41 PM -
need a simple java game
By Shashwat in forum New To JavaReplies: 1Last Post: 01-03-2011, 02:42 PM -
simple game programming help
By b.m in forum New To JavaReplies: 0Last Post: 11-25-2010, 05:16 PM -
Simple Game example
By mayuresh34 in forum CLDC and MIDPReplies: 1Last Post: 10-08-2009, 01:21 PM -
Simple Dodging Game
By Tb0h in forum New To JavaReplies: 1Last Post: 07-18-2009, 02:30 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks