Jump to content

Problem with simple Space Invaders clone

- - - - -

  • Please log in to reply
6 replies to this topic

#1
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I'm developing a simple space invaders clone.
Everything works as it should except for the movement of the enemy ships.
They move back and forth going down a little bit when they reach the edges of the window.
There is one variable that controls their position on the x axis, and for every frame I compute its value like this:

xpos+=direction*elapsedMillis*xspeed;

there direction is either -1 or +1, elapsedMillis is of course the time since the last frame, and xspeed is a float equal to 0.1.

There are 2 problems:
1 - The ships seem to move a little bit faster when direction==1 (i.e. they're moving leftwards)
2 - When a sound is played, elapsedMillis goes from 16 to 10 (exactly, it goes down!) and the ships move faster while the sound is playing

I have no clue at all why this is happening.
Can someone help me?
Simone

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I put a sort of frame lock and the problem was solved (the main loop executes every 16 ms, so about 60 fps).
But if I set xspeed to 0.05f, the issue shows up again. The ships move faster when heading leftwards!
How can I solve the problem without increasing xspeed?

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
So... you just use a while(true) loop?

You normally want to use a
Thread.sleep(20);
To let the loop pause a while. Because you calculate the speed with the use of the time now and time of the previous loop this should have no impact on how fast the ships are moving but this is 100 times less stressful for your computer.

#4
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I do this:


looper=new TimerTask();

gameTimer = new Timer();

gameTimer.schedule(looper, 0, 25);

Isn't it the same?
Anyway I noticed that the issue happens when time*speed is about 1.0. Now that I use 25 millis and speed=0.05 the ships move properly (0.05*25~=1.25, which is > 1).

I still can't understand why they move faster leftwards when time*speed~=1.0 though

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Nooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!

You should always separate your logic code and your rendering code in two separate loops that are executed on two separate threads, and these threads (if you're an idiomatic type of guy) should perform their operations based on a shared, thread-safe model object. Your render thread loop should run using while (true), and run as fast as possible (it can also count the number of times it refreshes in a second, to get a frames per second value), then your logic loop should be controlled very carefully. I'd say instead of using oxano's advice of Thread.sleep(), I'd use something closer to what you've got, except you may want to consider scheduleAtFixedRate() for the timer instead, depends on your needs. This way you could have a TimerTask that represents everything you need to execute logic-wise, and use a Runnable to encapsulate the display rendering code and run it on a new Thread. Remember that Thread has no useful members to stop execution of the Thread once it's started, so your class implementing Runnable should have it's own way of exiting the while loop cleanly.
Wow I changed my sig!

#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I'll make 2 separate threads, it seems like a good solution indeed ;D
Thanks for the tip

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

should perform their operations based on a shared, thread-safe model object.
Why's that? As far as I know the thread that's going to draw the frames will only be using getters and never change data (I think). So thread safety shouldn't be really big issue there, should it?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users