c# - More Fluent Image Movement -
this question has answer here:
- controling moving picturebox 2 answers
so i'm messing around, getting hang of things , i'm making picture box move, have done. show me how make movement more fluent not slow down much?
code:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace meh { public partial class form1 : form { picturebox test = new picturebox(); public form1() { initializecomponent(); formborderstyle = formborderstyle.none; windowstate = formwindowstate.maximized; test.image = properties.resources.platinumgrass; test.location = new point(0, 0); test.width = 32; test.height = 32; this.controls.add(test); keydown += new keyeventhandler(form1_keydown); } private void form1_keydown(object sender, keyeventargs e) { int x = test.location.x; int y = test.location.y; int xmax = screen.primaryscreen.bounds.width - 32; int ymax = screen.primaryscreen.bounds.height - 32; if (e.keycode == keys.right && x < xmax ) x += 20; if (e.keycode == keys.left && x > 0) x -= 20; if (e.keycode == keys.up && y > 0) y -= 20; if (e.keycode == keys.down && y < ymax) y += 20; test.location = new point(x, y); } private void form1_load(object sender, eventargs e) { } } }
i suggest not using winforms game development
but can add these lines form constructor make improvements.
setstyle(controlstyles.resizeredraw, true); setstyle(controlstyles.userpaint, true); setstyle(controlstyles.allpaintinginwmpaint, true); setstyle(controlstyles.optimizeddoublebuffer, true);
also can add loop movement part improve little bit
private void form1_keydown(object sender, keyeventargs e) { int xmax = screen.primaryscreen.bounds.width - 32; int ymax = screen.primaryscreen.bounds.height - 32; (int = 0; < 10; i++) { int x = test.location.x; int y = test.location.y; if (e.keycode == keys.right && x < xmax) x += 2; if (e.keycode == keys.left && x > 0) x -= 2; if (e.keycode == keys.up && y > 0) y -= 2; if (e.keycode == keys.down && y < ymax) y += 2; test.location = new point(x, y); application.doevents(); } }
Comments
Post a Comment