(11-28-2023, 09:17 AM)1micha.elok Wrote: Air fighter 1945 in a very very simplified version
The main structure of code is divided in two sections :
1. Initialization
load image : plane
load image : bullet
load image : enemy
load image : background
2. Main Program (Looping)
Draw the plane
Define keys : Esc to quit, Left and Right to move the plane
Draw enemies
Bullet is auto shoot
Still have miss lots of features ... somebody please add some code to detect collision between a bullet and an enemy, I am running out of idea on how to code collision detect
Code:'INITIALIZATION
set window "1945 Simplified", 640, 480
load image 1,"data/plane.png"
load image 2,"data/bullet.png"
load image 3,"data/enemy.png"
load image 4,"data/black.png"
speed=4
planeX=640/2 ; planeY=400
bulletX = planeX+15 ; bulletY = planeY
enemyX = [] ; enemyY = []
enemyX[0] = 40 ; enemyY[0] = -10
enemyX[1] = 300 ; enemyY[1] = -50
enemyX[2] = 400 ; enemyY[2] = -30
'MAIN PROGRAM
do
'SET BACKGROUND
cls
draw image 4,0,0
'THE PLANE
draw image 1,planeX,planeY
'ESC TO QUIT, LEFT AND RIGHT KEYS TO MOVE THE PLANE
if keydown(KEY_ESCAPE) then end
if keydown(KEY_LEFT) then planeX=planeX-speed
if keydown(KEY_RIGHT) then planeX=planeX+speed
'ENEMIES
for x=0 to 2
if enemyY[x]>480 then
enemyX[x]=rnd(640)
enemyY[x]=-rnd(50)
else
enemyY[x]=enemyY[x]+speed
endif
next
for x=0 to 2
draw image 3,enemyX[x],enemyY[x]
next
'BULLET, AUTO SHOOT
if bulletY < 480 then
bulletY = bulletY - speed*3
endif
if bulletY = 100 then
bulletX = planeX+15
bulletY = planeY
endif
draw image 2,bulletX,bulletY
'WAIT BEFORE LOOPING
wait 40
loop
Looking good!
But may I suggest that you add 'set redraw off' on the line after 'set window' and 'redraw' before 'wait 40'?
Naalaa has a hidden image (a back buffer) the same size as the window. When you draw something (an image, a line of text or anything) it is drawn on the hidden image. If you haven't called 'set redraw off', every time you draw something the entire hidden image is copied to the window. If you have called 'set redraw off', the hidden image is only copied to the window when you manually call 'redraw'. This is much faster and removes "flickering" graphics.