ANSWERS: 2
  • In general, what you are trying to do is known as "pathing", and is a fairly complicated problem. If the game is asteroids, you have the trajectories of all of the objects on the screen, so you can predict where they will be in the future. You also know where object C is. Your programming task is find a location where you can fire a shot at A (probably assuming A won't move till then), And make a path to that location, not hitting anything else on the way, then turn and shoot. Rinse and repeat. The tricky part is finding a point to shoot from. One simple starting point would be to try the location you're at now, if there isn't a path to shoot A, try picking a random location not too far away from your current location, predict where the objects will be when you get there, and see if you will have a shot at that time. If you do, check to see if you will run into any object on your way to that spot. If either of those checks fail, try another random location nearby. I don't think anyone can give you C++ code to that that task here. It's just too complicated, plus you'd have to show us what code you are starting from. Good luck. You might want to buy a book on computer game programming. You might find it interesting.
  • Make a function for when the player fires ... everytime the fire button is pressed, have the command that actually fires, and then have the function check for intersecting trajectories of both the movement of your target and the fired bullets to see if "player_fired_on_target" ... if true, have it call another function that alters either the speed or direction of your target (or both), if false, it just exits, does nothing, and lets your bullets miss, and returns to the main game loop that waits for the player to aim & fire again.

Copyright 2018, Wired Ivy, LLC

Answerbag | Terms of Service | Privacy Policy