Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Stop pictures from drawing on top of each other
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flawedspirit




PostPosted: Tue Dec 09, 2008 1:01 pm   Post subject: Stop pictures from drawing on top of each other

Note: Download the pictures too for this to work.

Anyway, the code below is supposed to draw the skeletons in random places on the screen, but how do I stop it from drawing a sprite on top of another sprite? Hopefully without 300 lines worth of if statements.

code:

%VARIABLES
var mob1LocX : int
var mob1LocY : int
var mob2LocX : int
var mob2LocY : int
var mob3LocX : int
var mob3LocY : int
var mob4LocX : int
var mob4LocY : int
var mob5LocX : int
var mob5LocY : int
var mob1 : int
var mob2 : int
var mob3 : int
var mob4 : int
var mob5 : int
var mobHeight : int
var mobWidth : int

%LOAD MONSTER PIC ID'S
proc picLoad
    mob1 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
    mob2 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
    mob3 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
    mob4 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
    mob5 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
end picLoad

%DETECT PIC DIMENSIONS
proc picDims
    mobHeight := Pic.Height (mob1)
    mobWidth := Pic.Width (mob1)
    mobHeight := Pic.Height (mob2)
    mobWidth := Pic.Width (mob2)
    mobHeight := Pic.Height (mob3)
    mobWidth := Pic.Width (mob3)
    mobHeight := Pic.Height (mob4)
    mobWidth := Pic.Width (mob4)
    mobHeight := Pic.Height (mob5)
    mobWidth := Pic.Width (mob5)
end picDims

%RANDOMIZE LOCATION VARIABLES
proc mobLoc
    randint (mob1LocX, 1 + mobWidth, maxx - mobWidth)
    randint (mob1LocY, 1 + mobHeight, maxy - mobHeight)
    randint (mob2LocX, 1 + mobWidth, maxx - mobWidth)
    randint (mob2LocY, 1 + mobHeight, maxy - mobHeight)
    randint (mob3LocX, 1 + mobWidth, maxx - mobWidth)
    randint (mob3LocY, 1 + mobHeight, maxy - mobHeight)
    randint (mob4LocX, 1 + mobWidth, maxx - mobWidth)
    randint (mob4LocY, 1 + mobHeight, maxy - mobHeight)
    randint (mob5LocX, 1 + mobWidth, maxx - mobWidth)
    randint (mob5LocY, 1 + mobHeight, maxy - mobHeight)
end mobLoc

%CREATE SPRITES
proc createSprites
    mob1 := Sprite.New (mob1)
    mob2 := Sprite.New (mob2)
    mob3 := Sprite.New (mob3)
    mob4 := Sprite.New (mob4)
    mob5 := Sprite.New (mob5)
end createSprites

%POSITION SPRITES ON SCREEN
proc spritePos
    Sprite.SetPosition (mob1, mob1LocX, mob1LocY, true)
    Sprite.SetPosition (mob2, mob2LocX, mob2LocY, true)
    Sprite.SetPosition (mob3, mob3LocX, mob3LocY, true)
    Sprite.SetPosition (mob4, mob4LocX, mob4LocY, true)
    Sprite.SetPosition (mob5, mob5LocX, mob5LocY, true)
end spritePos

%DRAW SPRITES ON SCREEN
proc drawSprite
    Sprite.Show (mob1)
    Sprite.Show (mob2)
    Sprite.Show (mob3)
    Sprite.Show (mob4)
    Sprite.Show (mob5)
end drawSprite

%MAIN PROGRAM
picLoad
picDims
mobLoc
createSprites
spritePos
drawSprite



Download Me!!!.zip
 Description:
The needed pictures.

Download
 Filename:  Download Me!!!.zip
 Filesize:  6.14 KB
 Downloaded:  87 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Dec 09, 2008 1:12 pm   Post subject: RE:Stop pictures from drawing on top of each other

If you know the position of one image, and the position of another image, how far away must they be from each other so that they don't touch?

also... this seems redundant
Turing:

mob1 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
    mob2 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
    mob3 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
    mob4 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")
    mob5 := Pic.FileNew ("Monsters\\Skeleton\\SkeletonFront.bmp")


a single reference to the image file will do.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: Tue Dec 09, 2008 4:14 pm   Post subject: RE:Stop pictures from drawing on top of each other

Yes, 1 picture will suffice. Multiple sprites can have the same picture.

You could use a for loop to count through the coordinates of previous sprites, and if it is within a certain distance of one, re-randomize. Continue until there are no overlapping pictures.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: