1 Attachment(s)
Better solution for moving my rectangle? (simple game)
Hi,
I am creating a simple game in Java which currently looks like this:
Attachment 1252
The blue rectangle on the right is moving towards the blue square (the blue square is my hero which I can move around with the arrow keys). If I get hit by the blue rectangle my health gets reduced. What I would like to ask is if there's any better function to move my rectangle than the one I use below:
Code :
if(level == 0) {try {
while (true) {
skott.x -= skottspeed;
Thread.sleep(1 * 100);
level = 1;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
skott.x decides the x value of the rectangle which moves (also known as skott) towards the hero and here it takes minus speed which is a declared variable further up in my code and level is set to 1. I'm not really sure yet how thread.sleep is used but I think there's a better way moving my rectangle than the way I have done it. I would really appreciate some input since I'm quite new to programming a game.
Thanks,
John Z
Re: Better solution for moving my rectangle? (simple game)
I'm not really sure what the question is. Does this work? Where is your SSCCE? What do you mean by "better"?
I would use a Swing Timer instead of threading.
1 Attachment(s)
Re: Better solution for moving my rectangle? (simple game)
Hey,
I'm sorry I wasn't clear enough. It works yeah. However, I don't feel comfortable using something I'm not completely aware of what it actually does. Therefor I'm looking for an alternative I can use instead of Thread.sleep. I will try get to the bottom with the problem I've got.
Currently I only understand how to make the rectangle come towards me once. Once it has past my hero it continues the x directions forever. So basically at this point what the game does is moving a rectangle to the left all the time and the hero only has one chance to try avoid it. Not really a game with meaning yet.
What I think of doing is to make it once the big rectangle has past the hero/exited the window it gets removed completely and in 10 seconds it comes back again to the same position and starts attacking the hero again. I will make an image trying displaying the idea better.
Attachment 1253 (Please click to enlarge the image)
Thanks in advance for your help. :)
Re: Better solution for moving my rectangle? (simple game)
Sounds good to me. And it sounds like you should be using a Swing Timer. Every time the Timer fires (30 seconds a second is a reasonable framerate), just check the location of the rectangle and either move it forward or respawn it.
Re: Better solution for moving my rectangle? (simple game)
Thanks for your help. Gonna give it a try right away. Respawning it I use repaint();?
Re: Better solution for moving my rectangle? (simple game)
Quote:
Originally Posted by
JohnZ
Thanks for your help. Gonna give it a try right away. Respawning it I use repaint();?
I'm not sure what you mean by that. Calling repaint() simply tells the component to paint itself using paintComponent(). So you override paintComponent to display a frame of your game.
Then every time the Swing Timer fires, you do something like this:
1- Update every game Object. The OO way to do this is to iterate over each Object and call an update function of each.
2- call repaint
Then in paintComponent(), you simply iterate over each Object and paint it, perhaps by calling each Object's paint function.