Author Topic: Load and diplay an image  (Read 1983 times)

cvirus

  • Guest
Load and diplay an image
« on: November 16, 2012, 02:16:45 PM »
Same as in the old forum but with minor change.

Code: [Select]
--' comments in ESGL are like --' it works without the ' but that way it will
-- not be like this comment above

--' open a window with 320 x and 240 y windowed with caption
openwindow(320,240,0,"Ship Movement")
--' chose a background color in rgb format
backcolor(0,0,255)

--' function for the frames per second
setframetimer(60)

--' set the color key for the image that will be loaded, in this case 0,0,0 or black
colorkey(0,0,0)
--' function to load the image and path
local player=loadimage("ship.bmp")

--' declare a var that will be the x position of the ship
--' then grab the screenwidt and divide by 2, could be 240 instead
local player_x =screenwidth()/2

--' same thing here but this time we subtract the image height
local player_y =screenheight()-imageheight(player)
local speed = 3
--' this for now is not needed but is a bool variable
--' we are using it to check if the ship is alive or not, case it's alive then
--' print it on the screen
local visible = true

ESC = 27
--' main loop, this will repeat until a certain key or instruction is pressed or passed
repeat
--' clear the screen
cls()
--' declare a var to get the key that the user is pressing
key=getkey()
--' now we are trying to test what key was presses
if key == 276 then
   --' if it was key left then the player goes back 3 pix.
   player_x = player_x -speed
end
if key == 275 then
   --' if it was right then goes forward 3 pix
   player_x = player_x + speed
end
if key == 273 then
   --'if it was up
   player_y = player_y - speed
end
if key == 274 then
   --' if it was down
   player_y = player_y + speed
end
--' if the ship is alive then we print it in the screen
if visible then
   putimage(player_x,player_y,player)
end
--' function to put everything in the screen
sync()
--' wait until the esc key is pressed and if it was pressed then
until key == ESC
--' close the window and go away.... :)
closewindow() 

cvirus

  • Guest
Re: Load and diplay an image
« Reply #1 on: November 16, 2012, 02:50:27 PM »
Same thing but this time with tables.
Code: [Select]
local Wnd={x = 320,y = 240,w = 0,caption = "Ship Movement"}
local paint={R=rnd()*256,G=rnd()*256,B=rnd()*256}
local keys={ESC=27}

openwindow(Wnd.x,Wnd.y,Wnd.w,Wnd.caption)
backcolor(paint.R,paint.G,paint.B)
setframetimer(60)

--' set the color key for the image that will be loaded, in this case 0,0,0 or black
colorkey(0,0,0)
--' function to load the image and path
player = {image = loadimage("ship.bmp"),x = Wnd.x/2-64/2,y = Wnd.y-64,speed = 3,visible = true}

--' main loop, this will repeat until a certain key or instruction is pressed or passed
repeat
--' clear the screen
cls()
--' declare a var to get the key that the user is pressing
key=getkey()
--' now we are trying to test what key was presses
if key == 276 then
   --' if it was key left then the player goes back 3 pix.
   player.x = player.x -player.speed
end
if key == 275 then
   --' if it was right then goes forward 3 pix
   player.x = player.x + player.speed
end
if key == 273 then
   --'if it was up
   player.y = player.y - player.speed
end
if key == 274 then
   --' if it was down
   player.y = player.y + player.speed
end
--' if the ship is alive then we print it in the screen
if player.visible then
   putimage(player.x,player.y,player.image)
end
--' function to put everything in the screen
sync()
--' wait until the esc key is pressed and if it was pressed then
until key == keys.ESC
--' close the window and go away.... :)
closewindow()