performance - Banana Drop - C# Game -
i'm working on 2d game. in bananas(picturbox) drops top of screen , have catch them otherwise hits ground , lose points. animate banana, change it's y-location using timer has interval of 5ms(smooth animation).
the y-drop speed changed based on resolution of screen. since banana moving 1px every 5ms on base resolution 720, needs change speed take same time on different resolutions.
code:
private void timer_tick(object sender, eventargs e) { double xnanas = nanas.location.x; double ynanas = nanas.location.y; ynanas += 1 * this.height / 720; nanas.location = new point((int)xnanas, (int)ynanas); nanas.refresh(); } problem:
1. when resolution changed small number ex. 800x600. double converts int , banana doesn't move though (1 * 600 / 720) rounded 1.
2. since speed rounded time takes banana hit ground varies drastically! 16.7 on 1920x1080 , 10.6 on 1280x720. how make same?
tried:
changing interval rather speed. still doesn't hit ground @ exact same time. can't change interval large number makes animation choppy.
your approach not right, can see vary speed because int not have decimal , speed changes.
an easy solution store in class positions doubles , logic in double, cast double in class int after doing calculations , set picture box position, in way double increment @ same speed independently of screen size.
here example float logic (enough needs):
pointf realposition = pointf.empty; //initialize real position of pb. private void timer_tick(object sender, eventargs e) { realposition.y += 1.0f * this.height / 720.0f; //note .0f instruct compiler these must float operations nanas.location = point.round(realposition); nanas.refresh(); } also have source of error, forms timers aren't exact, if ui busy vary it's speed, real game normal not use timer, loop , check time elapsed , multiply speed in seconds.
Comments
Post a Comment