i working on spaceship shooter style game. in game there slow moving missiles run on different tasks.
task t4 = new task(() => fireshipcannons());
they don't much; move small bitmap across screen. loop 30 iterations. problem timing off , tasked seem take priority on others. many important note control speed of missiles using thread.sleep(100). because of slowing down, method run few seconds.
i using c#, .net, windows form application
i have spent hours thinking how reword this, , think worded correctly. problem having broad; yes, question may bit broad. got great answer! not indicate question asked correctly?
since using task
s (good), can make them async tasks , use await task.delay(time)
.
async task fireshipcannons(cancellationtoken ct) { (int = 0; < distance; i++) { await task.delay(100, ct); drawbitmap(i); } }
this way not tie 1 thread every bullet. tasks slow because reach limit of threadpool, should lot faster. additionally, should check if task.delay
has taken more 100 ms suggested. if did, draw bullet 2 spots further instead of 1, , reschedule accordingly.
async task fireshipcannons(cancellationtoken ct) { var start = datetime.now; while (bullet.isonscreen) { drawbitmap(bullet); var sleep = (start.add(timespan.frommilliseconds(100.0)) - datetime.now); if (sleep.milliseconds > 0) { task.delay(sleep, ct); } iter = (int)((datetime.now - start).totalmilliseconds / 100 + 0.5); bullet.movesteps(iter); // move iter steps. var start = start.addmilliseconds(100 * it); } }
lastly, might idea not hammer draw function new calls every bullet, instead combine bullets/changes single render function calculate changes , sends them off in 1 go. depend on how draw function implemented.
Comments
Post a Comment