Well now that I got my feet wet posting on this forum, I'll ask another question.

I've been working my way through the programming book (very nice book by the way), and I am trying to do my own take on the mouse and cheese game. I'm using sprites on the graphscreen, with the xLib commands DRAWSPRITE, DRAWTILEMAP, etc.

Anyway, I am having trouble getting the mouse to die when it collides with the enemy. The mouse will only die if sitting still and the enemy lands on it. If I press the arrow keys with decent timing, I can go right through the enemy without being harmed.

Here is a gif slowed down to 50%


My code is outrageously huge I think, but I am still a newb at this and havent discovered all the tricks of the trade as far as simplifying things.

The code (requires doors to run):

Code:

ClrDraw
FnOff
AxesOff
PutSprite("00BB00DD00EE0077",8,0,1,8,0,0,0
PutSprite("182442BEC2829CE0",16,0,1,8,0,0,0
PutSprite("7E81A54224997E81",24,0,1,8,0,0,0
PutSprite("007F41495D49417F",32,0,1,8,0,0,0
PutSprite("E03C2222121D0300",40,0,1,8,0,0,0
PutSprite("0000001824241800",48,0,1,8,0,0,0
StorePic 1
ClrDraw
{7,11->dim([C]
Fill(0,[C]
For(X,1,11
1->[C](1,X
1->[C](7,X
End
For(Y,1,7
1->[C](Y,1
1->[C](Y,11
End
For(X,5,8
1->[C](4,X
End
1->[C](2,3
1->[C](3,3
1->[C](6,7
If M=1
4->[C](6,3
If M=2
0->[C](6,3
2->[C](6,10
6->[C](6,2
6->[C](2,4
6->[C](5,4
6->[C](2,7
6->[C](5,10
ClrDraw
0->K
3->L
8->A
8->B
95->T
16->D
40->C
40->F
40->E
32->N
64->M
99->H
8->R
8->S
8->Q
Repeat K=45
DrawTileMap(2,0,0,11,7,0,11,0,7,1,0,0,1
DrawText(20,0,56,"  H: 99          S: 2785          LV: 3          x2     
For(I,0,48,6
DrawText(16,89,I,"  ...
End
DrawSprite(A,B,1,8,1,5,0,3,0,1
DrawSprite(C,D,1,8,1,3,0,3,0,1
If |LSET(3)=2
DrawSprite(E,F,1,8,1,3,0,3,0,1
If |LSET(3)=3
Then
DrawSprite(E,F,1,8,1,3,0,3,0,1
DrawSprite(M,N,1,8,1,3,0,3,0,1
End
Repeat K=45 or (|LSET(3)=3 and A=M and B=N) or (1=max(|LSET(3)={2,3} and A=E and B=F) or (A=C and B=D) or (2=[C](B/8+1,A/8+1) or H<=0 or T=35
getKey->K
RunIndicator(0
If K=34
Then
DrawSprite(A,B,1,8,1,5,0,3,0,1
B+8(1!=[C]((B+8)/8+1,A/8+1->B
DrawSprite(A,B,1,8,1,5,0,3,0,1
End
If K=25
Then
DrawSprite(A,B,1,8,1,5,0,3,0,1
B-8(1!=[C]((B-8)/8+1,A/8+1->B
DrawSprite(A,B,1,8,1,5,0,3,0,1
End
If K=24
Then
DrawSprite(A,B,1,8,1,5,0,3,0,1
A-8(1!=[C](B/8+1,(A-8)/8+1->A
DrawSprite(A,B,1,8,1,5,0,3,0,1
End
If K=26
Then
DrawSprite(A,B,1,8,1,5,0,3,0,1
A+8(1!=[C](B/8+1,(A+8)/8+1->A
DrawSprite(A,B,1,8,1,5,0,3,0,1
End
If 3=[C](A/8+1,B/8+1)
Stop
If max(|LSET(3)={1,2,3}
Then
DrawSprite(C,D,1,8,1,3,0,3,0,1
C+R->C
If 1=[C](D/8+1,(C+R)/8+1
~R->R
DrawSprite(C,D,1,8,1,3,0,3,0,1
End
If max(|LSET(3)={2,3}
Then
DrawSprite(E,F,1,8,1,3,0,3,0,1
E-S->E
If 1=[C](F/8+1,(E-S)/8+1
~S->S
DrawSprite(E,F,1,8,1,3,0,3,0,1
End
If max(|LSET(3)={3}
Then
DrawSprite(M,N,1,8,1,3,0,3,0,1
N+Q->N
If 1=[C]((N+Q)/8+1,M/8+1)
~Q->Q
DrawSprite(M,N,1,8,1,3,0,3,0,1
End
End
If (|LSET(3)=3 and ((A=M and B=N) or (A=E and B=F) or (A=C and B=D))) or (|LSET(3)=2 and ((A=C and B=D) or (A=E and B=F))) or (|LSET(3)=1 and A=C and B=D)
Then
Disp "POISON KILLS
Disp "LOSE 1 LIFE
L-1->L
Disp L
Pause "LIVES REMAIN
End
End


Some things, (A,B) are the coordinates of the mouse, (C,D), (E,F) and (M,N) are the coordinates of the enemy poison.

The value of |LSET(3) will determine the number of poisons that appear on screen.

I can explain further segments of the code if needed. I would really appreciate any advice you can give.
You might need to change where you are checking for the collision, or you might need to check for collision a few different times.

Sadly, I never delved too deeply into using xlib, so most of the code above makes little sense in the sense of using the functions of xlib. The code itself doesn't look too bad.

What lines are you having collision detected at?
When they collide, won't their coordinates be the same by the set up you have? If so, you can check if the mouse has the same coordinates as any of the enemies:

Code:

A={C,E,M
If sum(Ans*B={D,F,N
<<collision code>>
Xeda, could you explain what that code means?
tifreak8x wrote:
What lines are you having collision detected at?


If (|LSET(3)=3 and ((A=M and B=N) or (A=E and B=F) or (A=C and B=D))) or (|LSET(3)=2 and ((A=C and B=D) or (A=E and B=F))) or (|LSET(3)=1 and A=C and B=D)
Basically, {M,E,C} is one of the coordinates of all the enemies. You can check if any of them are equal to A by doing A={M,E,C}. If E=A, then the list would be {0,1,0}. Now do the same with the other coordinate. Now to see if both coordinates match, I should actually have just used ' and ' instead of multiplication (it works the same, but ' and ' is faster):

Code:

A={M,E,C
Ans and B={N,F,D

Now if you use sum() on that result, you will get zero if there are no collisions, else you will get a non-zero number. So to put it all together:

Code:

A={M,E,C
If sum(Ans and B={N,F,D
<collision code>

Now you might be worried about when you have fewer than 3 enemies, but if that is the case, just set their coordinates off the screen and you will never collide with them Smile
Thanks for the explanation. I'll give it a try. I actually managed on my own to eliminate the "going through the enemy" problem, but then my program slowed to a snail's pace. The check that you provided seems more efficient and faster.
Another question: When I have all three enemies moving about, the game is slow to the point that it just isn't going to work.

Is it just asking too much to have three enemies moving simultaneously on a screen? Or might I be writing it in such a way that it all slows down?
It seems likely to me that when trying to simulate three different enemies and their directions, not to mention getting input from the user and simulating that sprite too, you're going to start running into performance issues pretty fast.
Roguebantha wrote:
It seems likely to me that when trying to simulate three different enemies and their directions, not to mention getting input from the user and simulating that sprite too, you're going to start running into performance issues pretty fast.

If the enemy data isn't being stored in an array of some sort. But I wouldn't doubt that it would get quite slow.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement