forums

TNT Basic Forums > Programming Q&A
Problems with sprite collision in pacman...HELP!!!
< Last Thread     Next Thread >
Author
Thread        Post A Reply

05-09-2008 14:25

Posted by:
seanh

Find more posts by seanh

So basically i'm making a pacman game. I drew invisible rectangles (they're not invisible right now) and i have a sprite col rect. If it senses the sprite collision, the velociity is 11 (or -11) in the x or y, so that it is no longer colliding. However, it doesn't move 11! HELP!

'program information section
'sean haber
'pacman - game!
'date modified: March 27

'declaration section

repeat
int pacmanmouth=1
int score
int lives=3
int coordx
int coordy
int pacmanx=310 , pacmany=325
int pacmanvelox, pacmanveloy
int s
int count=1
int buttons
int movingback

'welcome section

window mode 640, 480
load images 128
text font "comic sans MS"
Text face 16
Text size 30
set pen colour yellow
draw text 160, 220, "Welcome to pacman!"
draw text 130, 280, "Click When You're Ready!"
draw frame
wait mouse click
set pen colour black
paint canvas
draw frame

'this is to keep the score
' set pen colour yellow
' draw text 125, 470, as str (score)
' draw frame

'this is to draw the game board
' sprite 1, 0, 0, 0
' paste sprite 1
' draw frame

'the invisible rectangles
set pen colour red
Set Pen transparency 20
frame rect 63, 45 to 132, 71
frame rect 177,45 to 272,72
frame rect 314,19 to 341, 71
frame rect 387,42 to 479, 72
frame rect 522, 42 to 596, 73
frame rect 61, 100 to 132, 113
frame rect 176, 98 to 209, 199
frame rect 201, 140 to 271,155
frame rect 242,102 to 410,115
frame rect 315, 115 to 340, 157
frame rect 385,141 to 454,147
frame rect 454,99 to 477,197
frame rect 246, 182 to 410, 240
frame rect 454, 225 to 480,283
frame rect 176,225 to 202,283
frame rect 246, 267 to 410, 283
frame rect 313, 283 to 340, 323
frame rect 385, 308 to 478, 324
frame rect 176, 308 to 273, 324
frame rect 246, 351 to 410, 367
frame rect 313, 367 to 340, 408
frame rect 523, 100 to 594, 113
frame rect 62,308 to 133,326
frame rect 106, 326 to 133, 366
frame rect 522, 308 to 592, 326
frame rect 522, 325 to 549, 366
frame rect 62, 391 to 273, 410
frame rect 177, 391 to 201, 352
frame rect 383, 391 to 593, 410
frame rect 452, 391 to 477, 351
frame rect 593, 350 to 640, 367
frame rect 0, 350 to 64, 367
frame rect 0, 223 to 134, 282
frame rect 522, 225 to 640, 283
frame rect 523, 140 to 640,195
frame rect 132, 140 to 0,199
draw frame

'this is to make pacman move
pacmanvelox=0
pacmanveloy=0

repeat
sprite 2,pacmanx,pacmany,pacmanmouth
draw frame
if movingback=1 then pacmanvelox=0 and pacmanveloy=0
movingback=0
if pressed (123)
pacmanvelox=-5
pacmanveloy=0
buttons=1
else if pressed (124)
pacmanvelox=5
pacmanveloy=0
buttons=2
else if pressed (125)
pacmanveloy=5
pacmanvelox=0
buttons=3
else if pressed (126)
pacmanveloy=-5
pacmanvelox=0
buttons=4
end if

'this is for the monsters
' coordx=Mouse x
' coordy=Mouse y
' draw text 63, 45, as str (coordx)
' draw text 100, 100, as str (coordy)

'this is for the direction
if buttons=1 and count=1 or buttons=1 and count=2 or buttons=1 and count=3 or buttons=1 and count=4 then pacmanmouth=2
if buttons=2 and count=1 or buttons=2 and count=2 or buttons=2 and count=3 or buttons=2 and count=4 then pacmanmouth=4
if buttons=3 and count=1 or buttons=3 and count=2 or buttons=3 and count=3 or buttons=3 and count=4 then pacmanmouth=3
if buttons=4 and count=1 or buttons=4 and count=2 or buttons=4 and count=3 or buttons=4 and count=4 then pacmanmouth=5
if count=5 or count=6 or count=7 or count=8 then pacmanmouth=1

'this is the sprite collisions
s=0
s=sprite col rect (2, 63, 45 to 132, 71)
if s=1 and buttons=1 then pacmanvelox=30 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-30 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-30 and movingback=1
if s=1 and buttons=4 then pacmanveloy=30 and movingback=1
s=sprite col rect (2, 177,45 to 272,72)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 314,19 to 341, 71)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 387,42 to 479, 72)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 522, 42 to 596, 73)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 61, 100 to 132, 113)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 176, 98 to 209, 199)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 201, 140 to 271,155)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 242,102 to 410,115)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2,315, 115 to 340, 157)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2,385,141 to 454,147)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 454,99 to 477,197)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 246, 182 to 410, 240)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 454, 225 to 480,283)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 176,225 to 202,283)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 246, 267 to 410, 283)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 313, 283 to 340, 323)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 385, 308 to 478, 324)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 176, 308 to 273, 324)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 246, 351 to 410, 367)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 313, 367 to 340, 408)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 523, 100 to 594, 113)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 62,308 to 133,326)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 106, 326 to 133, 366)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 522, 308 to 592, 326)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 522, 325 to 549, 366)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 62, 391 to 273, 410)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 177, 391 to 201, 352)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 383, 391 to 593, 410)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 452, 391 to 477, 351)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 593, 350 to 640, 367)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 0, 350 to 64, 367)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 0, 223 to 134, 282)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 522, 225 to 640, 283)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 523, 140 to 640,195)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
s=sprite col rect (2, 132, 140 to 0,199)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1

pacmany=pacmany+pacmanveloy
pacmanx=pacmanx+pacmanvelox
count=count+1
if count=8 then count=1

'this is to keep the score
set pen colour yellow
draw text 125, 470, as str (score)
draw frame


until mouse button

until mouse button

05-12-2008 02:56

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide


>>
s=sprite col rect (2, 63, 45 to 132, 71)
if s=1 and buttons=1 then pacmanvelox=30 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-30 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-30 and movingback=1
if s=1 and buttons=4 then pacmanveloy=30 and movingback=1
s=sprite col rect (2, 177,45 to 272,72)
if s=1 and buttons=1 then pacmanvelox=11 and movingback=1
if s=1 and buttons=2 then pacmanvelox=-11 and movingback=1
if s=1 and buttons=3 then pacmanveloy=-11 and movingback=1
if s=1 and buttons=4 then pacmanveloy=11 and movingback=1
>>

These are two different rectangles, right?
And you're storing them into the same memory [s].
So when you declare the second rectangle it replaces the one before it? You need to use different variables.

Look up on procedures, they might be able to help you reduce duplicate code.

05-12-2008 06:22

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

actually, nvm...... I just read up the online help

05-13-2008 23:33

Posted by:
seanyboy

Find more posts by seanyboy

i'm still having troubles...does anybody have any ideas?

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.