Retrogamecoding(.org) > EGSL
EGSL programming examples...
(1/1)
Luposian:
To write a simple overhead 2D game, I first need to know how to attach an image (sprite) to the mouse pointer and give it pixel-accurate collision detection with another moving sprite. I found examples of a keyboard-controlled sprite (spinning block wall) colliding with a static sprite (man), but I need the man to be moving (in from any of 8 directions; from off screen towards the center) and the user-controlled sprite to be mouse controlled.
Also, does anyone have (or know where is) the source code for 1945, the overhead shoot-em up? It should be be a good foundation for another game idea I have.
Cybermonkey:
What about the collision example?
--- Code: ---screen (800,600,0,"Collision Test")
rec1={}
rec2={}
rec1.image = createimage (128,32)
rec2.image = createimage (16,64)
startimagedraw (rec1.image)
backcolor (255,0,0)
cls()
stopimagedraw()
startimagedraw (rec2.image)
backcolor (255,255,0)
cls()
stopimagedraw()
rec1.x=450
rec1.y=300
backcolor (0,0,0)
grabmouse()
repeat
cls()
key=getkey()
rec2.x = mousex()
rec2.y = mousey()
putimage (rec1.x,rec1.y,rec1.image)
alphachannel (200)
putimage (rec2.x,rec2.y,rec2.image)
alphachannel (255)
if imagecoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y) then
drawtext (0,0,"Imagecoll detected")
end
if boxcoll (rec2.x,rec2.y,imagewidth (rec2.image), imageheight (rec2.image), rec1.x, rec1.y, imagewidth (rec1.image), imageheight (rec1.image)) then
drawtext (0,10,"Boxcoll detected")
end
if pixelcoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y, 2) then
drawtext (0,20,"Pixelcoll detected")
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,30,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,40,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),-1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,50,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,-1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,60,side)
end
sync()
until key==27
closewindow()
--- End code ---
Luposian:
OKAY! Thank you, CM! This is a good start! And, with a little code cross-referencing, I figured out, not only how to replace the mouse-controlled solid rectangle into a brick wall, as well as change the static image into a sprite (knight), but also how to hide the mouse pointer! Yea!
Now, the next question... in trying to add a 3rd image, I get nothing. Image (grass.bmp) or graphic primitive. Neither shows up. Why is this? I've simply added another set of instructions, just like the other two, but it isn't working. Why is this?
I'm enclosing my progress thus far.
--- Code: ---screen (800,600,0,"Collision Test")
rec1={}
rec2={}
rec3={}
rec1.image = loadimage ("data/sprite.bmp")
rec2.image = loadimage ("data/wall.bmp")
rec3.image = createimage (64,64)
putimage(300,400,rec1.image)
backcolor (255,255,0)
cls()
redraw()
putimage(10,10,rec2.image)
backcolor (255,255,0)
cls()
redraw()
startimagedraw (100,150,rec3.image)
backcolor (255,0,0)
cls()
stopimagedraw()
rec1.x=450
rec1.y=300
backcolor (0,0,0)
mousehide()
grabmouse()
repeat
cls()
key=getkey()
rec2.x = mousex()
rec2.y = mousey()
putimage (rec1.x,rec1.y,rec1.image)
alphachannel (200)
putimage (rec2.x,rec2.y,rec2.image)
alphachannel (255)
if imagecoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y) then
drawtext (0,0,"Imagecoll detected")
end
if boxcoll (rec2.x,rec2.y,imagewidth (rec2.image), imageheight (rec2.image), rec1.x, rec1.y, imagewidth (rec1.image), imageheight (rec1.image)) then
drawtext (0,10,"Boxcoll detected")
end
if pixelcoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y, 2) then
drawtext (0,20,"Pixelcoll detected")
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,30,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,40,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),-1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,50,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,-1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,60,side)
end
redraw()
sync()
until key==27
closewindow()
--- End code ---
Luposian:
Ok, by "commenting" certain lines, I learned some things. One set of instructs was totally unnecessary (redundant) and I finally figured out how to display a 3rd image.
However, some instructions affect things differently than it would seem. For example, why does alphachannel in the first line affect rec2.image, whereas alphachannel in the second line seemingly affects BOTH rec1.image and rec2.image?
And why does rec2.image float OVER rec1.image, but UNDER rec3.image (I really need to relabel the sprites. I want the mouse-pointer controlled one (rec2.image) to be rec1.image)? What gives one image "layer priority" over another, or is it just the way it's coded?
Having fun working on this code... just wish I could have tabs for different files or run two instances of EGSL, so I can easily swap/view/run between programs. Also, why does EGSL always open to the same directory? You can't seem to change it, but I'd like it to be able to remember the last directory you were in, at least.
Latest progress:
--- Code: ---screen (800,600,0,"Collision Test")
rec1={}
rec2={}
rec3={}
rec1.image = loadimage ("data/sprite.bmp")
rec2.image = loadimage ("data/wall.bmp")
rec3.image = loadimage ("data/grass.bmp")
putimage(300,400,rec1.image)
backcolor (255,255,0)
cls()
redraw()
rec1.x=450
rec1.y=300
putimage(300,400,rec3.image)
backcolor (255,255,0)
cls()
redraw()
rec3.x=400
rec3.y=300
backcolor (100,100,100)
mousehide()
grabmouse()
repeat
cls()
key=getkey()
rec2.x = mousex()
rec2.y = mousey()
putimage (rec1.x,rec1.y,rec1.image)
alphachannel (200)
putimage (rec2.x,rec2.y,rec2.image)
alphachannel (255)
putimage (rec3.x,rec3.y,rec3.image)
alphachannel (255)
if imagecoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y) then
drawtext (0,0,"Imagecoll detected")
end
if boxcoll (rec2.x,rec2.y,imagewidth (rec2.image), imageheight (rec2.image), rec1.x, rec1.y, imagewidth (rec1.image), imageheight (rec1.image)) then
drawtext (0,10,"Boxcoll detected")
end
if pixelcoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y, 2) then
drawtext (0,20,"Pixelcoll detected")
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,30,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,40,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),-1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,50,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,-1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,60,side)
end
redraw()
sync()
until key==27
closewindow()
--- End code ---
Luposian:
I want to have a sound occur when the mouse-controlled sprite touches the brick wall sprite. I've coded in the sound trigger, but it's an "if-then" loop, which means as long as the two sprites are touching, the sound is constantly triggered (it's a single explosion sound (sample taken from 1945's /data folder, for testing purposes) but it sounds like a machine gun, actually, because the condition if-then" is always true).
My assumption is that I have to make the stationary sprite disappear, the instant the mouse-controlled sprite touches it, in order to have the sound trigger only once, correct?
How do I make the sprite vanish, when touched?
Here's my code:
--- Code: ---screen (800,600,0,"Collision Test")
rec1={}
rec2={}
rec3={}
rec4={}
rec5={}
rec1.image = loadimage ("data/wall.bmp")
rec2.image = loadimage ("data/sprite.bmp")
rec3.image = loadimage ("data/grass.bmp")
rec4.image = loadimage ("data/sprite.bmp")
rec5.image = loadimage ("data/sprite.bmp")
putimage(300,400,rec1.image)
backcolor (255,255,0)
cls()
redraw()
rec1.x=500
rec1.y=400
putimage(300,400,rec4.image)
backcolor (255,255,0)
cls()
redraw()
rec4.x=550
rec4.y=565
putimage(300,400,rec5.image)
backcolor (255,255,0)
cls()
redraw()
rec5.x=585
rec5.y=565
putimage(300,400,rec3.image)
backcolor (255,255,0)
cls()
redraw()
rec3.x=375
rec3.y=300
backcolor (255,0,0)
mousehide()
grabmouse()
repeat
cls()
key=getkey()
rec2.x = mousex()
rec2.y = mousey()
colour(0, 200, 255)
fillbox(10, 580, 200, 590)
colour(139, 80, 14)
fillbox(150, 110, 650, 500)
putimage (rec1.x,rec1.y,rec1.image)
alphachannel (255)
putimage (rec2.x,rec2.y,rec2.image)
alphachannel (255)
putimage (rec3.x,rec3.y,rec3.image)
alphachannel (255)
putimage (rec4.x,rec4.y,rec4.image)
alphachannel (255)
putimage (rec5.x,rec5.y,rec5.image)
alphachannel (255)
colour(0, 0, 0)
drawtext (350,580,"SCORE: 56000")
redraw()
if imagecoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y) then
-- drawtext (0,0,"Imagecoll detected")
music2 =loadsound ("data/Explosion7.wav")
playsound (music2, 25, 1, 0)
pausemusic(music2)
end
if boxcoll (rec2.x,rec2.y,imagewidth (rec2.image), imageheight (rec2.image), rec1.x, rec1.y, imagewidth (rec1.image), imageheight (rec1.image)) then
drawtext (0,10,"Boxcoll detected")
end
if pixelcoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y, 2) then
drawtext (0,20,"Pixelcoll detected")
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,30,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,40,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),-1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,50,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,-1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,60,side)
end
redraw()
sync()
until key==27
closewindow()
--- End code ---
Navigation
[0] Message Index
Go to full version