Menu

pong..the game but not pong

2019-11-03
2019-11-05
  • stan cartwright

    stan cartwright - 2019-11-03

    I was asked if I could write a pong variant.
    This variant gives more problems than pong.
    Poor video. Like pong but goals. https://youtu.be/044cziItJ8Y
    Here is the code that erases and re prints the bats which makes it slower.
    Before I would print a space at the top or bottom of the bat depending on it's direction to make it move but you can move the bat pot more than 8 pixels between loops so using erase whole bats and re print which is a bit flickery and slows the game down.
    I used multi if then statements for speed...if one is false it doesn't check the rest.

    ;pong
    ;assuming display top y vertical=0 to 239 and left x horizontal=0 to 319
    ;using arduino nano
    #chip mega328p,16
    #include <glcd.h>
    #option explicit
    ;--------------
    #define GLCD_TYPE GLCD_TYPE_ILI9341
    
    #define GLCD_DC      portb.0 ;   DIGITAL_8           ' Data command line
    #define GLCD_CS      portb.2 ;   DIGITAL_10          ' Chip select line
    #define GLCD_RESET   portb.1 ;   DIGITAL_9           ' Reset line
    #define GLCD_DI      portb.4 ;   DIGITAL_12          ' Data in | MISO    - Not used therefore not really required
    #define GLCD_DO      portb.3 ;   DIGITAL_11          ' Data out | MOSI
    #define GLCD_SCK     portb.5 ;   DIGITAL_13          ' Clock Line
    
    #define ILI9341_HardwareSPI    ' remove/comment out if you want to use software SPI.
    #define GLCD_EXTENDEDFONTSET1
    ;-----------------------------
    ;now rename colours to make it faster to type colours
    #define bk ILI9341_BLACK
    #define re ILI9341_RED
    #define gr ILI9341_GREEN
    #define bl ILI9341_BLUE
    #define wh ILI9341_WHITE
    #define pu ILI9341_PURPLE
    #define ye ILI9341_YELLOW
    #define cy ILI9341_CYAN
    #define dg ILI9341_D_GRAY
    #define lg ILI9341_L_GRAY
    #define si ILI9341_SILVER
    #define ma ILI9341_MAROON
    #define ol ILI9341_OLIVE
    #define li ILI9341_LIME
    #define aq ILI9341_AQUA
    #define te ILI9341_TEAL
    #define na ILI9341_NAVY
    #define fu ILI9341_FUCHSIA
    ;
    ;GLCDBackground = wh
    GLCDRotate (landscape)
    GLCDCLS wh
    
    dim leftgoals,rightgoals,goal as byte
    dim ball_x,ball_y,last_ball_x,last_ball_y as Word
    dim ball_width,ball_height as Byte
    dim ball_xdirection,ball_ydirection as Word
    dim leftbat_y,rightbat_y as word
    dim leftbat_x,rightbat_x,last_leftbat_y,last_rightbat_y as word
    ;dim leftbat_height,rightbat_height as Byte
    dim leftgoal_ytop,leftgoal_ybottom,rightgoal_ytop,rightgoal_ybottom as Word
    dim top_edge,left_edge as byte ; they are zero
    dim bottom_edge,right_edge as Word ;screen resolution
    dim tmp as byte
    dim count as word
    'dim leftpot,rightpot as Byte
    '
    leftgoals=0:rightgoals=0
    ;
    ;***** start ******************
    do
    ;
    goal=0
    ;
    leftbat_x=64:rightbat_x=256
    ;
    ;----draw left bat at pot value
    leftbat_y=scale (ReadAD(ADC1),0,255,10,239-8) ;----left pot wiper
    if leftbat_y >=239-8-32 then ;----leftbat hit bottom
      leftbat_y=239-8-32
    else if leftbat_y <=10 then
        leftbat_y=10 ;----leftbat hit top
    end if
    last_leftbat_y=leftbat_y
    ;
    ;----draw right bat at pot value
    rightbat_y=scale (ReadAD(ADC2),0,255,10,239-8) ;----right pot wiper
    if rightbat_y >=239-8-32 then ;----rightbat hit bottom
      rightbat_y=239-8-32
    else if rightbat_y <=10 then
        rightbat_y=10 ;----rightbat hit top
    end if
    ;
    last_rightbat_y=rightbat_y
    ;
    ;
    ;----init ball --------
    ball_x=160 :ball_y=112 ;----mid screen
    last_ball_x=ball_x:last_ball_y=ball_y
    ;
    ;----ball direction at Startup--------------
    ;
    ;----ball x direction ---------------------
    randomize readad (an0)
    tmp=Random
    if tmp.1=0 then ;is random odd or even number
     ball_xdirection=8
    else
     ball_xdirection=0-8;-8 .... math works
    end if
    ;
    ;----ball y direction -----------------------
    tmp=Random
    if tmp.0=0 then;is random odd or even number
     ball_ydirection=4
    else
     ball_ydirection=0-4;-4 math works
    end if
    ;
    ;----draw border -----------------------
    filledbox 8,10,311,231,bk
    ;
    ;----draw left goal --------------------
    FilledBox 0,80,8,160,bk
    ;
    ;----draw right goal ---------------------
    FilledBox 311,80,319,160,bk
    ;
    ;----print scores/goals ----------
    GLCDDrawString(8,0,"LEFT",bk)
    GLCDDrawString(248,0,"RIGHT",bk)
    GLCDPrint (48,0,leftgoals,bk)
    GLCDPrint (296,0,rightgoals,bk)
    ;------end of initialise
    ;----------------------------------------
    ;
    do ;--------main loop
    leftbat_y=scale (ReadAD(ADC1),0,255,10,200) ;-----left pot wiper
    
    if leftbat_y - last_leftbat_y > 8 then
      leftbat_y=leftbat_y+8
    else if last_leftbat_y - leftbat_y > 8 then
      leftbat_y=leftbat_y-8
    end if
    ;
    if leftbat_y >=239-8-32 then ;----leftbat hit bottom
      leftbat_y=239-8-32
    else if leftbat_y <=10 then
        leftbat_y=10 ;----leftbat hit top
    end if
    ;
    ;----erase left bat
    GLCDBackground = bk
    for count=last_leftbat_y to last_leftbat_y+24 step 8;----draw 4 spaces back ground colour
      GLCDDrawChar(leftbat_x,count,32)
    next count
    ;----draw left bat
    GLCDBackground = wh
    for count=leftbat_y to leftbat_y+24 step 8;----draw 4 spaces fore ground colour
      GLCDDrawChar(leftbat_x,count,32)
    next count
    ;
    last_leftbat_y=leftbat_y ;----update left bat last y
    ;---------------------------
    ;
    rightbat_y=scale (ReadAD(ADC2),0,255,10,200) ;----read right bat pot
    
    if rightbat_y - last_rightbat_y > 8 then
      rightbat_y=rightbat_y+8
    else if last_rightbat_y - rightbat_y > 8 then
      rightbat_y=rightbat_y-8
    end if
    
    if rightbat_y >= 239-8-32 then ;----leftbat hit bottom
      rightbat_y=239-8-32
    else if rightbat_y <=10 then
        rightbat_y=10 ;----rightbat hit top
    end if
    ;
    ;----erase right bat
    GLCDBackground = bk
    for count=last_rightbat_y to last_rightbat_y+24 step 8;----draw 4 spaces back ground colour
      GLCDDrawChar(rightbat_x,count,32)
    next count
    ;
    ;----draw right bat
    GLCDBackground = wh
    for count=rightbat_y to rightbat_y+24 step 8;----draw 4 spaces fore ground colour
      GLCDDrawChar(rightbat_x,count,32)
    next count
    ;
    last_rightbat_y=rightbat_y ;----update right bat last y
    
    
        if ball_xdirection < 32767 then ;--going right
        ;----check ball hit right wall-----
          if ball_x+ball_xdirection >=312 then
            ball_xdirection=1+not ball_xdirection ;--hit right wall,go left
            if ball_y >= 80 then
              if ball_y <=152 then
                leftgoals ++ : goal=1 ;--THIS NEEDS GOTO Start!
              end if
    
            end if
          end if
      ;
        ;----check if ball hit left bat back------
          if ball_x+7 < leftbat_x then
            if ball_x+ball_xdirection+8 >= leftbat_x then
              if ball_y+7+ball_ydirection >= leftbat_y then
                if ball_y+ball_ydirection <= leftbat_y+31 then
                  ball_xdirection=1+not ball_xdirection ;--hit back of left bat,go left
                end if
              end if
            end if
          end if
      ;
        ;----check if ball hit right bat front------
        if ball_x+8 < rightbat_x then
          if ball_x+ball_xdirection+8 >= rightbat_x then
            if ball_y+7+ball_ydirection >= rightbat_y then
              if ball_y+ball_ydirection <= rightbat_y+31 then
                ball_xdirection=1+not ball_xdirection ;--go left
                ;----change y direction random depending on y direction
                if ball_ydirection < 32767 then
                  randomize readad (an0)
                  ball_ydirection=random/42+1
                else
                  randomize readad (an0)
                  ball_ydirection=0-random/42
                end if
              end if
            end if
          end if
        end if
      ;
        else ;----ball going left
      ;
        ;----check ball hit left wall------
        if ball_x+ball_xdirection <=8 then
          ball _xdirection=1+not ball_xdirection ;--go right
          if ball_y >= 80 then
            if ball_y <=152 then
              rightgoals ++ : goal=1 ;--THIS NEEDS GOTO Start!
            end if
          end if
        end if
      ;
        ;----check if ball hit right bat back------
          if ball_x > rightbat_x+8 then
            if ball_x+ball_xdirection <= rightbat_x+8 then
              if ball_y+7+ball_ydirection >= rightbat_y then
                if ball_y+ball_ydirection <= rightbat_y+31 then
                  ball_xdirection=1+not ball_xdirection ;--ball hit right bat back,go right
                end if
              end if
            end if
          end if
      ;
        ;----check if ball hit left bat front------
          if ball_x > leftbat_x+8 then
            if ball_x+ball_xdirection <= leftbat_x+8 then
              if ball_y+7+ball_ydirection >= leftbat_y then
                if ball_y+ball_ydirection <= leftbat_y+31 then
                  ball_xdirection=1+not ball_xdirection ;--go right
                  ;change y direction random depending on y direction
                  if ball_ydirection < 32767 then
                    randomize readad (an0)
                    ball_ydirection=random/42+1
                  else
                    randomize readad (an0)
                    ball_ydirection=0-random/42
                  end if
              end if
            end if
          end if
        end if
      end if
    ;-------------
    ;----check if ball hit top or bottom border ----------------------
      if ball_ydirection < 32767 then
        if ball_y+ball_ydirection >=224 then ;--going down,hit bottom
          ball_ydirection=1+not ball_ydirection ;--go up
        end if
      else if ball_y+ball_ydirection <=10 then
        ball _ydirection=1+not ball_ydirection ;--going up,hit top,go down
      end if
    ;
    ;----move ball x ------------------------------
    ball_x=ball_x+ball_xdirection
    ;
    ;----move ball y --------------------------------
    ball_y=ball_y+ball_ydirection
    ;
    ;----erase old ball ---------------------------------
    GLCDBackground = bk
    GLCDDrawChar(last_ball_x,last_ball_y,32)
    ;
    ;----plot new ball ---------------------------------------
    GLCDBackground = wh
    GLCDDrawChar(ball_x,ball_y,32)
    ;
    ;-----update last ball x,y -------------------
    last_ball_x=ball_x:last_ball_y=ball_y
    
    ;wait 100 ms
    loop until goal=1;----goal scored so restart
    ;
    loop ;----back to initialise/start
    '
    
     
  • Anobium

    Anobium - 2019-11-04

    Pong fires up.
    The background is white. Your video is black.
    You sure this is the right code?

     

    Last edit: Anobium 2019-11-04
  • stan cartwright

    stan cartwright - 2019-11-04

    To be sure I copied and pasted the forum code into untitled gcb,named and saved then flash hex and works ok. I'm using a nano not a uno board.
    I'll film it being compiled and flashed if you want.

     

    Last edit: stan cartwright 2019-11-04
  • stan cartwright

    stan cartwright - 2019-11-04

    https://www.youtube.com/watch?v=sVrJDv42WnY&feature=youtu.be
    pong being flashed
    ps was that easy or what?
    gcb as it should be.

     

    Last edit: stan cartwright 2019-11-05
  • stan cartwright

    stan cartwright - 2019-11-05

    I let run 10 + minutes and this happened at some point wonder if it was when goals >255.
    Looks "nice" though :) https://www.youtube.com/watch?v=K1BkJT8-lGQ&feature=youtu.beit
    loose bread board wiring

     

    Last edit: stan cartwright 2019-11-13

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.