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;assumingdisplaytopyvertical=0to239andleftxhorizontal=0to319;usingarduinonano#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;-----------------------------;nowrenamecolourstomakeitfastertotypecolours#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=whGLCDRotate(landscape)GLCDCLSwhdimleftgoals,rightgoals,goalasbytedimball_x,ball_y,last_ball_x,last_ball_yasWorddimball_width,ball_heightasBytedimball_xdirection,ball_ydirectionasWorddimleftbat_y,rightbat_yasworddimleftbat_x,rightbat_x,last_leftbat_y,last_rightbat_yasword;dimleftbat_height,rightbat_heightasBytedimleftgoal_ytop,leftgoal_ybottom,rightgoal_ytop,rightgoal_ybottomasWorddimtop_edge,left_edgeasbyte;theyarezerodimbottom_edge,right_edgeasWord;screenresolutiondimtmpasbytedimcountasword'dimleftpot,rightpotasByte'leftgoals=0:rightgoals=0;;*****start******************do;goal=0;leftbat_x=64:rightbat_x=256;;----drawleftbatatpotvalueleftbat_y=scale(ReadAD(ADC1),0,255,10,239-8);----leftpotwiperifleftbat_y>=239-8-32then;----leftbathitbottomleftbat_y=239-8-32elseifleftbat_y<=10thenleftbat_y=10;----leftbathittopendiflast_leftbat_y=leftbat_y;;----drawrightbatatpotvaluerightbat_y=scale(ReadAD(ADC2),0,255,10,239-8);----rightpotwiperifrightbat_y>=239-8-32then;----rightbathitbottomrightbat_y=239-8-32elseifrightbat_y<=10thenrightbat_y=10;----rightbathittopendif;last_rightbat_y=rightbat_y;;;----initball--------ball_x=160:ball_y=112;----midscreenlast_ball_x=ball_x:last_ball_y=ball_y;;----balldirectionatStartup--------------;;----ballxdirection---------------------randomizereadad(an0)tmp=Randomiftmp.1=0then;israndomoddorevennumberball_xdirection=8elseball_xdirection=0-8;-8....mathworksendif;;----ballydirection-----------------------tmp=Randomiftmp.0=0then;israndomoddorevennumberball_ydirection=4elseball_ydirection=0-4;-4mathworksendif;;----drawborder-----------------------filledbox8,10,311,231,bk;;----drawleftgoal--------------------FilledBox0,80,8,160,bk;;----drawrightgoal---------------------FilledBox311,80,319,160,bk;;----printscores/goals----------GLCDDrawString(8,0,"LEFT",bk)GLCDDrawString(248,0,"RIGHT",bk)GLCDPrint(48,0,leftgoals,bk)GLCDPrint(296,0,rightgoals,bk);------endofinitialise;----------------------------------------;do;--------mainloopleftbat_y=scale(ReadAD(ADC1),0,255,10,200);-----leftpotwiperifleftbat_y-last_leftbat_y>8thenleftbat_y=leftbat_y+8elseiflast_leftbat_y-leftbat_y>8thenleftbat_y=leftbat_y-8endif;ifleftbat_y>=239-8-32then;----leftbathitbottomleftbat_y=239-8-32elseifleftbat_y<=10thenleftbat_y=10;----leftbathittopendif;;----eraseleftbatGLCDBackground=bkforcount=last_leftbat_ytolast_leftbat_y+24step8;----draw4spacesbackgroundcolourGLCDDrawChar(leftbat_x,count,32)nextcount;----drawleftbatGLCDBackground=whforcount=leftbat_ytoleftbat_y+24step8;----draw4spacesforegroundcolourGLCDDrawChar(leftbat_x,count,32)nextcount;last_leftbat_y=leftbat_y;----updateleftbatlasty;---------------------------;rightbat_y=scale(ReadAD(ADC2),0,255,10,200);----readrightbatpotifrightbat_y-last_rightbat_y>8thenrightbat_y=rightbat_y+8elseiflast_rightbat_y-rightbat_y>8thenrightbat_y=rightbat_y-8endififrightbat_y>=239-8-32then;----leftbathitbottomrightbat_y=239-8-32elseifrightbat_y<=10thenrightbat_y=10;----rightbathittopendif;;----eraserightbatGLCDBackground=bkforcount=last_rightbat_ytolast_rightbat_y+24step8;----draw4spacesbackgroundcolourGLCDDrawChar(rightbat_x,count,32)nextcount;;----drawrightbatGLCDBackground=whforcount=rightbat_ytorightbat_y+24step8;----draw4spacesforegroundcolourGLCDDrawChar(rightbat_x,count,32)nextcount;last_rightbat_y=rightbat_y;----updaterightbatlastyifball_xdirection<32767then;--goingright;----checkballhitrightwall-----ifball_x+ball_xdirection>=312thenball_xdirection=1+notball_xdirection;--hitrightwall,goleftifball_y>=80thenifball_y<=152thenleftgoals++:goal=1;--THISNEEDSGOTOStart!endifendifendif;;----checkifballhitleftbatback------ifball_x+7<leftbat_xthenifball_x+ball_xdirection+8>=leftbat_xthenifball_y+7+ball_ydirection>=leftbat_ythenifball_y+ball_ydirection<=leftbat_y+31thenball_xdirection=1+notball_xdirection;--hitbackofleftbat,goleftendifendifendifendif;;----checkifballhitrightbatfront------ifball_x+8<rightbat_xthenifball_x+ball_xdirection+8>=rightbat_xthenifball_y+7+ball_ydirection>=rightbat_ythenifball_y+ball_ydirection<=rightbat_y+31thenball_xdirection=1+notball_xdirection;--goleft;----changeydirectionrandomdependingonydirectionifball_ydirection<32767thenrandomizereadad(an0)ball_ydirection=random/42+1elserandomizereadad(an0)ball_ydirection=0-random/42endifendifendifendifendif;else;----ballgoingleft;;----checkballhitleftwall------ifball_x+ball_xdirection<=8thenball_xdirection=1+notball_xdirection;--gorightifball_y>=80thenifball_y<=152thenrightgoals++:goal=1;--THISNEEDSGOTOStart!endifendifendif;;----checkifballhitrightbatback------ifball_x>rightbat_x+8thenifball_x+ball_xdirection<=rightbat_x+8thenifball_y+7+ball_ydirection>=rightbat_ythenifball_y+ball_ydirection<=rightbat_y+31thenball_xdirection=1+notball_xdirection;--ballhitrightbatback,gorightendifendifendifendif;;----checkifballhitleftbatfront------ifball_x>leftbat_x+8thenifball_x+ball_xdirection<=leftbat_x+8thenifball_y+7+ball_ydirection>=leftbat_ythenifball_y+ball_ydirection<=leftbat_y+31thenball_xdirection=1+notball_xdirection;--goright;changeydirectionrandomdependingonydirectionifball_ydirection<32767thenrandomizereadad(an0)ball_ydirection=random/42+1elserandomizereadad(an0)ball_ydirection=0-random/42endifendifendifendifendifendif;-------------;----checkifballhittoporbottomborder----------------------ifball_ydirection<32767thenifball_y+ball_ydirection>=224then;--goingdown,hitbottomball_ydirection=1+notball_ydirection;--goupendifelseifball_y+ball_ydirection<=10thenball_ydirection=1+notball_ydirection;--goingup,hittop,godownendif;;----moveballx------------------------------ball_x=ball_x+ball_xdirection;;----movebally--------------------------------ball_y=ball_y+ball_ydirection;;----eraseoldball---------------------------------GLCDBackground=bkGLCDDrawChar(last_ball_x,last_ball_y,32);;----plotnewball---------------------------------------GLCDBackground=whGLCDDrawChar(ball_x,ball_y,32);;-----updatelastballx,y-------------------last_ball_x=ball_x:last_ball_y=ball_y;wait100msloopuntilgoal=1;----goalscoredsorestart;loop;----backtoinitialise/start'
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 fires up.
The background is white. Your video is black.
You sure this is the right code?
Last edit: Anobium 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
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
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