Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1945 simplified
#1
[Image: ?attachment_id=1703]

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  Big Grin ... 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  Sad

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


Attached Files
.zip   1945simplified.zip (Size: 16.2 KB / Downloads: 6)
Reply
#2
(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  Big Grin ... 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  Sad

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.
Reply
#3
Regarding collision, "just" check if two images overlap. It's easier to understand if you draw some on a paper Smile

Code:
for x=0 to 2
        dead = bulletX + width(2) > enemyX[x] and bulletX < enemyX[x] + width(3) and
            bulletY + height(2) > enemyY[x] and bulletY < enemyY[x] + height(3)
        if enemyY[x]>480 or dead then
            enemyX[x]=rnd(640)
            enemyY[x]=-rnd(50)
         else
            enemyY[x]=enemyY[x]+speed
         endif
      next
Reply
#4
Oh man. This game brings back memories... 1945. Classic.

If memory serves correctly, I may have the Naalaa5 version, floating around on a drive somewhere. If I can find it, you can use it for reference or even convert it to Naalaa7... Either way I will look forward to playing your game. Classic.
Logic is the beginning of wisdom.
Reply
#5
(11-28-2023, 06:17 PM)Marcus Wrote: Regarding collision, "just" check if two images overlap. It's easier to understand if you draw some on a paper Smile
...

Your collision detection code is wonderful, it works like magic. 
Now when a bullet hits an enemy, the enemy disappeares  Big Grin Big Grin Big Grin

Thanks also for your advice to avoid flickering graphics by 'set redraw off' and 'redraw'  Cool

Code:
'------ revised code to reflect some improvements -------

'INITIALIZATION
set window "1945 Simplified", 640, 480
set redraw off 'faster, removes flickering graphics
load image 1,"data_1945/plane.png"
load image 2,"data_1945/bullet.png"
load image 3,"data_1945/enemy.png"
load image 4,"data_1945/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
        'collision detection if bullet and enemy image overlap
        dead = bulletX + width(2) > enemyX[x] and bulletX < enemyX[x] + width(3) and
            bulletY + height(2) > enemyY[x] and bulletY < enemyY[x] + height(3)
        if enemyY[x]>480 or dead 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
      redraw 'faster, removes flickering graphics
      wait 40
loop
Reply
#6
(11-29-2023, 03:49 AM)1micha.elok Wrote:
(11-28-2023, 06:17 PM)Marcus Wrote: Regarding collision, "just" check if two images overlap. It's easier to understand if you draw some on a paper Smile
...

Your collision detection code is wonderful, it works like magic. 
Now when a bullet hits an enemy, the enemy disappeares  Big Grin Big Grin Big Grin

Thanks also for your advice to avoid flickering graphics by 'set redraw off' and 'redraw'  Cool

Code:
'------ revised code to reflect some improvements -------

'INITIALIZATION
set window "1945 Simplified", 640, 480
set redraw off 'faster, removes flickering graphics
load image 1,"data_1945/plane.png"
load image 2,"data_1945/bullet.png"
load image 3,"data_1945/enemy.png"
load image 4,"data_1945/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
        'collision detection if bullet and enemy image overlap
        dead = bulletX + width(2) > enemyX[x] and bulletX < enemyX[x] + width(3) and
            bulletY + height(2) > enemyY[x] and bulletY < enemyY[x] + height(3)
        if enemyY[x]>480 or dead 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
      redraw 'faster, removes flickering graphics
      wait 40
loop


No problems! I've added two more versions of your program, but you can ignore them for now if they seem confusing Smile

In 1945_2.n7 I've replaced your two arrays, enemyX and enemyY, with a 2D array, so that the x value of enemy n is in enemy[n][0] and the y value is in enemy[n][1].

1945_3.n7 is a modification of 1945_2.n7, but instead of using enemy[n][0] and enemy[n][1] for position, I use enemy[n].x and enemy[n].y, which makes it easier to read the code.


The . (dot) notation, as in enemy[n].x, really isn't a complicated thing. In n7 you can use both numbers and strings as "keys" to tables. An array is what I call a table where only numeric keys (indexes) are used.

This is an array:

Code:
hello = []
hello[0] = 13
hello[1] = 12
pln "1: " + hello[1]

But you can also do this:

Code:
hello = []
hello["what"] = 13
hello["weird"] = 14
pln "weird: " + hello["weird"]

And the above is the exact same thing as:

Code:
hello = []
hello.what = 13
hello.weird = 14
pln "weird: " + hello.weird

I hope I'm not confusing you or anone else.


Attached Files
.n7   1945_2.n7 (Size: 1.74 KB / Downloads: 3)
.n7   1945_3.n7 (Size: 1.75 KB / Downloads: 5)
Reply
#7
(11-29-2023, 06:01 PM)Marcus Wrote: ...
The . (dot) notation, as in enemy[n].x, really isn't a complicated thing. In n7 you can use both numbers and strings as "keys" to tables. An array is what I call a table where only numeric keys (indexes) are used.
...

Thank you for sharing the .(dot) notation ...
It improves the readibility of the source code...
I tried to use it and it worked well  Big Grin 
I love NAALAA  Cool
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)