Advertisement

Can anyone find the problem here???

Started by May 05, 2001 05:22 PM
7 comments, last by Brackus 23 years, 6 months ago
Okay, first off, the code is in turing because i have no good, new c or c++ books or compiler, but i figure, when youre trying to make the computer do basic things, who cares, so here it is, currently, i am setting 2 starting locations for the computer and players dots, I just have a basic loop that tests for the computers coords in relation to the players, and changes the cpu''s movement. Now, i havent used turing in a long time, so I am just using the coding guidelines i used when i made pong for my class. There are no compiler errors, but the computer just moves right across the screen until it is past the players x coordinate, then it moves back to the left until i just get bored and ctrl-break the program. heres the code!!! var playerx : int := 100 var playery : int := 350 var cpux : int := 450 var cpuy : int := 80 var endar : int var cpuxchange : int := 1 var cpuychange : int := 1 drawfilloval (cpux, cpuy, 5, 5, 1) drawfilloval (playerx, playery, 5, 5, 5) endar := 0 loop delay (5) drawfilloval (cpux, cpuy, 5, 5, 0) if playerx < cpux and playery < cpuy then cpuxchange := -cpuxchange cpuychange := -cpuychange end if if playerx < cpux and playery > cpuy then cpuxchange := -cpuxchange cpuychange := cpuychange end if if playerx > cpux and playery < cpuy then cpuychange := -cpuychange cpuxchange := cpuxchange end if if playerx > cpux and playery > cpuy then cpuxchange := cpuxchange cpuychange := -cpuychange end if cpux := cpux + cpuxchange cpuy := cpuy + cpuychange drawfilloval (cpux, cpuy, 5, 5, 1) if cpux = playerx and cpuy = playery then endar := 1 end if end if exit when endar = 1 end loop
Mess With the Best, go down like the rest, but please visit:http://members.tripod.com/nu_bgameprogramming/
I am not sure I completely understand this language at the moment, but I think I see the problem...

With your program, the computer *always* moves. The direction it moves to, depends on the location of the player. But when it is alligned with the player, it still moves!

Here are you rules, except that player x and y are now 0.

if x > 0 then xchange = 1
if x < 0 then xchange = -1
if y > 0 then ychange = 1
if y < 0 then ychange = -1
x = x + xchange
y = y + ychange

Say it is on x = -2, y = -1 of the player. Your program will add one to both x and y.

Then it is on x = -1, y = 0 of the player. Your program will now add one to x, because x is smaller then 0... But y is the right number. However, your program doesnt interact with this, and keeps moving down:

Then it is on x = 0, y = 1 of the player. x shouldnt be changed, but there is no code for that, so it will move in the same direction along the x-axle. Since y > 0, y will become one step smaller.

Then it is on x = 1, y = 0.

And it will keep circling around the centre...

The solution?

if x > 0 then xchange = 1
if x < 0 then xchange = -1
if y > 0 then ychange = 1
if y < 0 then ychange = -1
x = x + xchange
y = y + ychange
xchange = 0
ychange = 0

Only two extra lines *should* do the job...
Advertisement
I think this should be it...

**********************************
**********************************


var playerx : int := 100
var playery : int := 350

var cpux : int := 450
var cpuy : int := 80

var endar : int
var cpuxchange : int := 0
var cpuychange : int := 0

drawfilloval (cpux, cpuy, 5, 5, 1)
drawfilloval (playerx, playery, 5, 5, 5)

endar := 0

loop
delay (5)

cpuxchange := 0
cpuychange := 0

drawfilloval (cpux, cpuy, 5, 5, 0)

if playerx < cpux then
cpuxchange := -1
end if

if playerx > cpux then
cpuxchange := 1
end if

if playery < cpuy then
cpuychange := -1
end if

if playery > cpuy then
cpuychange := 1
end if

cpux := cpux + cpuxchange
cpuy := cpuy + cpuychange

drawfilloval (cpux, cpuy, 5, 5, 1)

if cpux = playerx and cpuy = playery then
endar := 1
end if
end if

exit when endar = 1
end loop
Ah yes, I see now that the way I was testing for coords, that I was doing too much in one step, better leave the x coords by themselves in their own tests, same with the y''s.

Anyway, i dont know how much you, or anyone else who may read this know, but I am just slowly trying to work my way up with cpu movements, and collision detection, so I can get a small football game working!!!

First question: In a simple (x,y) plane, is there a better way of moving the computer because it looks kinda off in terms of what a cpu might do.

Second question: Lets say I insert a set wall, about 30x5 pixels in dimension, how would i have the cpu not hit it, or, know to go around it, this I feel is extremely important!!!

Lastly(for now): Is there a more efficient(being what all game programmers strive for) way to move the cpu, and have it check positions, what I use above is fine for now, but I worry what would happen in a 5 vs 5 (or more) football game, even if I was just moving dots around???

Thanks for any help already given, and any, given after this!!!
Mess With the Best, go down like the rest, but please visit:http://members.tripod.com/nu_bgameprogramming/
quote: Original post by Brackus
First question: In a simple (x,y) plane, is there a better way of moving the computer because it looks kinda off in terms of what a cpu might do.


You could compute the angle between the cpu dot and player dot and move along that.

Alternatively, you could use knowledge of the players trajectory vector to computer the angle along which you could travel. Ie, compute where you think the player is going to be after your next move and head for that spot!

quote:
Second question: Lets say I insert a set wall, about 30x5 pixels in dimension, how would i have the cpu not hit it, or, know to go around it, this I feel is extremely important!!!


Collision dectection is not a trivial problem. In the worst case scenario you need to keep a list of all objects in the game world and at each timestep, check that each is not sharing space, or trying to move into a space, with any other object. However, you don''t normally need to go to this extreme. You can break the world up into regions and check first if two objects share a region and then check if they are colliding. There''s a good article in ''Game Programmers Gems'' on this very issue. Check it out.

quote:
Lastly(for now): Is there a more efficient(being what all game programmers strive for) way to move the cpu, and have it check positions, what I use above is fine for now, but I worry what would happen in a 5 vs 5 (or more) football game, even if I was just moving dots around???


I''m thinking!

Tim
Is there a better way to move around? Actually, no.

Pathway finding is pretty simple in a football game: you look for your destination, then you go there.

The current method you are using is great for following someone. That is, when you keep updating on your targets location.

In a football game, you will do the same. The only think you might want to change, is the goal it is walking towards. Instead of just walking to a ball or player, look at their distance and heading, and calculate a place where you can intercept them. Head there, but keep recalculating that place...

That''s it, as far as I know...
Advertisement
Hmmm, some posts slightly encouraging, most not, but I am up to a challenge Okay, firstly, where is the artice you speak of in game programming gems, i searched basically this whole site, didnt find it unless you mean a new book, and if you do, darn!

Okay, I dont really wanna worry about going where the player is heading to, just following is fine, as I just want to learn basics first, much the reason for little dots, instead of 3d fully rendered models like most newbies aim for... anyways, about this cpu heading along a line, if I am using a graphics setting where the screen is in coords like 640x480 and looks like this

480
|
|
|
240
|
|
|
00---------320-----------640

basically anyway, how does one make the cpu "move along a line???" like, the only line that would not allow the cpu to cheat in a game by moving too many squares is from (34,34) to (35,35) or like (33, 35) both are diagonals i think, so buy moving in anything else than a 45 degree angle, i am lost for how....

Thats all i seem able to think of right now, thanks!!!
Mess With the Best, go down like the rest, but please visit:http://members.tripod.com/nu_bgameprogramming/
I''m not going to post the pseudocode, but the answer to this question deals with the way lines are drawn. Basically, the answer is that you find out whether the next "real" point - say, the y-value of the line at the next pixel column - in the line is closer to the top pixel or the bottom pixel, then move to that pixel. Read Mark Feldman''s Bresenham article for some material on this.

Otherwise, you could just restrict the player''s movement to one the 4 cardinal directions or the 8 "gamepad points" (cardinal and semi-cardinal dirns?) and then move according to which angle is closest to the actual angle between some line (probably one of the main axes) and the direction to the destination.

Oh, and Geta might be around later, so now''s the time to learn: search and THEN ask.

Hope that helps,

ld
No Excuses
\clarification{
That Geta comment was meant to be gently-poking-fun, not calling-down-to-the-dirt, BTW
}

ld
No Excuses

This topic is closed to new replies.

Advertisement