Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Chapter 14

Listing C-9. pong.nbc

#include "NXTDefs.h"
// -------------- Variable Declarations --------------
#define I2CPort IN_1
#define I2CAddress 0x90

dseg segment
// a to d channels
   Ch2 byte
   Ch3 byte
// syscall structures
   lswArgs TCommLSWrite
   lscsArgs TCommLSCheckStatus
   lsrArgs TCommLSRead
// Graphic structures
   ball TDrawGraphic
   lpad TDrawGraphic
   rpad TDrawGraphic
// Various Variables
   z sword
   x byte 6
   y byte 30
   xvel byte 1
   yvel byte 1
   rpady byte
   lpady byte
   rscore byte 0
   lscore byte 0
dseg ends
// ---------------- Program Code ----------------
thread main
// Initialize variables
   set ball.Options, 0
   set lpad.Options, 0
   set lpad.Location.Y, 0
   set lpad.Location.X, 5
   set rpad.Options, 0
   set rpad.Location.Y, 0
   set rpad.Location.X, 95
// Initialize I2C
   mov lswArgs.Port, I2CPort
   mov lsrArgs.Port, I2CPort
   mov lscsArgs.Port, I2CPort
   arrbuild lswArgs.Buffer, I2CAddress, 0x04 // auto incr
   set lswArgs.ReturnLen, 5
   set lsrArgs.BufferLen, 5
   call Atod4   // Get new values
   div lpady, Ch3, 4   // Pot on CH3 and scale
   mov lpad.Filename, 'N.RIC'  // erase left pad
   syscall DrawGraphic lpad
   mov lpad.Location.Y, lpady  // draw left pad
   mov lpad.Filename, 'P.RIC'
   syscall DrawGraphic lpad
   div rpady, Ch2, 4   // Pot on CH2 and scale
   mov rpad.Filename, 'N.RIC'  // erase right pad
   syscall DrawGraphic rpad
   mov rpad.Location.Y, rpady  // draw right pad
   mov rpad.Filename, 'P.RIC'
   syscall DrawGraphic rpad
   mov ball.Filename, 'W.RIC'  // erase ball
   syscall DrawGraphic ball
   add x, x, xvel              // move ball
   add y, y, yvel
   mov ball.Location.X, x      // draw ball
   mov ball.Location.Y, y
   mov ball.Filename, 'B.RIC'
   syscall DrawGraphic ball
chk0:  // off left side?
   brcmp LT, chk1, x, 96
   set x, 6
   set y, 30
   add lscore, lscore, 1
   jmp razz
chk1:  // off right side?
   brcmp GT, chk2, x, 2
   set x, 94
   set y, 30
   add rscore, rscore, 1
razz:  // noise and score
   PlayTone(100, 500)
   wait 500
   jmp top
chk2:  // bounce off top?
   brcmp LT, chk3, y, 60
   set yvel, −1
chk3:  // bounce off bottom?
   brcmp GT, chk4, y, 1
   set yvel, 1
chk4:  // bounce off left paddle?
   brcmp NEQ, chk5, x, 5
   add z, lpady, 6
   sub z, z, y
   abs z, z
   brcmp GT, chk5, z, 10
   set xvel, 1
   PlayTone(600, 50)
chk5:  // bounce off right paddle?
   brcmp NEQ, chk6, x, 93
   add z, rpady, 6
   sub z, z, y
   abs z, z
   brcmp GT, chk6, z, 10
   set xvel, −1
   PlayTone(600, 50)
   wait 15  // 10 fast - 30 slow
   jmp top

subroutine Atod4
   syscall CommLSWrite, lswArgs
   syscall CommLSCheckStatus, lscsArgs
   brtst GT, Atod4Wait, lscsArgs.Result
   brcmp NEQ, Atod4top, lscsArgs.BytesReady, 5
   syscall CommLSRead, lsrArgs
   index Ch2, lsrArgs.Buffer, 3
   index Ch3, lsrArgs.Buffer, 4


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free 10-Day Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint