beginner, don't blame :),i'm coding simple "pingpong" game in c#, practice bit because second week of learning language. i've tried make keyevents make "picsschlägerrechts" move , down, wich worked well, can't manage make "restriction" not move out of panel. ideas?
private static bool conditionup ; private static bool conditiondown ; private void frmpingpong_keydown(object sender, keyeventargs e) { { if (!(picschlägerrechts.location.y == 0 && picschlägerrechts.location.y == 249)) { conditiondown = true; conditionup = true; } if (e.keycode == keys.w && conditionup == true) { picschlägerrechts.location = new point(picschlägerrechts.location.x, picschlägerrechts.location.y - ms); if (picschlägerrechts.location.y == 0) { conditionup = false; } } if(e.keycode == keys.s && conditiondown == true) { picschlägerrechts.location = new point(picschlägerrechts.location.x, picschlägerrechts.location.y + ms); if (picschlägerrechts.location.y == 298) { conditiondown = false; } }
you try checks make sure y not go greater or less max/min y
private void frmpingpong_keydown(object sender, keyeventargs e) { var maxy = 298; var miny = 0; if (e.keycode == keys.w) { var newy = picschlägerrechts.location.y - ms; if (newy < miny) { newy = miny; } picschlägerrechts.location = new point(picschlägerrechts.location.x, newy); } else if (e.keycode == keys.s) { var newy = picschlägerrechts.location.y + ms; if (newy > maxy) { newy = maxy; } picschlägerrechts.location = new point(picschlägerrechts.location.x, newy); } }
Comments
Post a Comment