# Qbasicnews.com

Full Version: Pong AI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Hi all.

Make a self-playing Pong Clone. 2 players, but computer vs. computer. It must combine ANSI characters and graphics (hint, screen 13, each char is 8x8 pixels)
What's ANSI characters?
Quote:What's ANSI characters?

ASCII characters.
Oh.

ASCII characters! Why would you want ASCII characters in a game?! What the score mabey?
Ok, It took me about an hour but here is my entry:
Code:
```RANDOMIZE TIMER CLEAR SCREEN 13 CLS BX% = 160 BY% = 100 IF INT(RND * (1 - 0 + 1) + 0) THEN x% = -1:  ELSE x% = 1 IF INT(RND * (1 - 0 + 1) + 0) THEN y% = -1:  ELSE y% = 1 BXV% = INT(RND * (3 - 1 + 1) + 1) * x% BYV% = INT(RND * (3 - 1 + 1) + 1) * y% R% = 2 score1% = 0: Score2% = 0 Paddle1X% = 4: Paddle1Y% = 90 Paddle2X% = 300: Paddle2Y% = 90 index% = 0 COLOR 4 PRINT "RULES: GO TO 10 POINTS" PRINT "IF AI SYSTEM GOES TO A NO-WIN LOOP IT    RESETS" PRINT "PRESS ANY KEY TO CONTINUE" DO: LOOP WHILE INKEY\$ = "" CLS DO   IF BXV% < 0 THEN    IF BY% > Paddle1Y% + 10 THEN Paddle1Y% = Paddle1Y% + 2    IF BY% < Paddle1Y% + 10 THEN Paddle1Y% = Paddle1Y% - 2    IF Paddle1Y% + 20 > 199 THEN Paddle1Y% = 199 - 20    IF Paddle1Y% < 0 THEN Paddle1Y% = 0   END IF   IF BXV% > 0 THEN    IF BY% > Paddle2Y% + 10 THEN Paddle2Y% = Paddle2Y% + 2    IF BY% < Paddle2Y% + 10 THEN Paddle2Y% = Paddle2Y% - 2    IF Paddle2Y% + 20 > 199 THEN Paddle2Y% = 199 - 20    IF Paddle2Y% < 0 THEN Paddle2Y% = 0   END IF   BX% = BX% + BXV%   BY% = BY% + BYV%   IF BX% > 319 THEN     CLS     PRINT "POINTPLAYER 1!"     PRINT "PRESS ANY KEY TO CONTINUE"     DO: LOOP WHILE INKEY\$ = ""     CLS     BX% = 160: BY% = 100     score1% = score1% + 1     IF INT(RND * (1 - 0 + 1) + 0) THEN x% = -1:  ELSE x% = 1     IF INT(RND * (1 - 0 + 1) + 0) THEN y% = -1:  ELSE y% = 1     BXV% = INT(RND * (3 - 1 + 1) + 1) * x%     BYV% = INT(RND * (3 - 1 + 1) + 1) * y%   END IF   IF BX% < 0 THEN     CLS     PRINT "POINTPLAYER 2!"     PRINT "PRESS ANY KEY TO CONTINUE"     DO: LOOP WHILE INKEY\$ = ""     BX% = 160: BY% = 100     CLS     Score2% = Score2% + 1     IF INT(RND * (1 - 0 + 1) + 0) THEN x% = -1:  ELSE x% = 1     IF INT(RND * (1 - 0 + 1) + 0) THEN y% = -1:  ELSE y% = 1     BXV% = INT(RND * (3 - 1 + 1) + 1) * x%     BYV% = INT(RND * (3 - 1 + 1) + 1) * y%   END IF   IF BY% > 199 THEN BY% = 199: BYV% = BYV% * -1   IF BY% < 0 THEN BY% = 0: BYV% = BYV% * -1   LINE (Paddle1X%, Paddle1Y%)-STEP(3, 20), 1, BF   LINE (Paddle2X%, Paddle2Y%)-STEP(3, 20), 1, BF   CIRCLE (BX%, BY%), R%, 4   oBX% = BX%: oBY% = BY%   oPaddle1X% = Paddle1X%: oPaddle1Y% = Paddle1Y%   oPaddle2X% = Paddle2X%: oPaddle2Y% = Paddle2Y%   LOCATE 1, 10: COLOR 4: PRINT score1%   LOCATE 1, 30: COLOR 4: PRINT Score2%    doY% = 0    doR% = 0    x2% = Paddle1X%    y2% = Paddle1Y%    IF x2% >= BX% AND x2% <= BX% + 4 THEN doY% = 1    IF x2% + 3 >= BX% AND x2% + 3 <= BX% + 4 THEN doY% = 1    IF BX% >= x2% AND BX% <= x2% + 3 THEN doY% = 1    IF BX% + 4 >= x2% AND BX% + 4 <= x2% + 3 THEN doY% = 1    IF doY% = 1 THEN      IF y2% >= BY% AND y2% <= BY% + 4 THEN doR% = 1      IF y2% + 20 >= BY% AND y2% + 20 <= BY% + 4 THEN doR% = 1      IF BY% >= y2% AND BY% <= y2% + 20 THEN doR% = 1      IF BY% + 4 >= y2% AND BY% + 4 <= y2% + 20 THEN doR% = 1    END IF    IF doR% = 1 THEN     BXV% = BXV% * -1    END IF    doY% = 0    doR% = 0    x2% = Paddle2X%    y2% = Paddle2Y%    IF x2% >= BX% AND x2% <= BX% + 4 THEN doY% = 1    IF x2% + 3 >= BX% AND x2% + 3 <= BX% + 4 THEN doY% = 1    IF BX% >= x2% AND BX% <= x2% + 3 THEN doY% = 1    IF BX% + 4 >= x2% AND BX% + 4 <= x2% + 3 THEN doY% = 1    IF doY% = 1 THEN      IF y2% >= BY% AND y2% <= BY% + 4 THEN doR% = 1      IF y2% + 20 >= BY% AND y2% + 20 <= BY% + 4 THEN doR% = 1      IF BY% >= y2% AND BY% <= y2% + 20 THEN doR% = 1      IF BY% + 4 >= y2% AND BY% + 4 <= y2% + 20 THEN doR% = 1    END IF    IF doR% = 1 THEN     BXV% = BXV% * -1    END IF   WAIT &H3DA, 8   FOR i% = -12000 TO 12000: NEXT i%   LINE (oPaddle1X%, oPaddle1Y%)-STEP(3, 20), 0, BF   LINE (oPaddle2X%, oPaddle2Y%)-STEP(3, 20), 0, BF   CIRCLE (oBX%, oBY%), R%, 0   index% = index% + 1   IF index% >= 5000 THEN     CLS     PRINT "TIMEOUTRESET"     PRINT "PRESS ANY KEY TO CONTINUE"     DO: LOOP WHILE INKEY\$ = ""     CLS     index% = 0     BX% = 160: BY% = 100     IF INT(RND * (1 - 0 + 1) + 0) THEN x% = -1:  ELSE x% = 1     IF INT(RND * (1 - 0 + 1) + 0) THEN y% = -1:  ELSE y% = 1     BXV% = INT(RND * (3 - 1 + 1) + 1) * x%     BYV% = INT(RND * (3 - 1 + 1) + 1) * y%   END IF LOOP UNTIL score1% >= 10 OR Score2% >= 10 CLS IF score1% > Score2% THEN PRINT "PLAYER 1 WINS!" ELSE IF score1% < Score2% THEN   PRINT "PLAYER 2 WINS!"   ELSE   PRINT "TIE!" END IF END IF END```
That's sweet Mitth!!!!
Thank you...

I kinda just felt like doing it quick, bout an hour 20 min, needed a break from SpaceMerc.

Thanks

You know that that is a really stable core there, and can easily be converted to a 1 player game, its open source if anyone wants it, just not to use to post in this challenge of course.

So if anyone wants to pick off of what I have there I can explain it to you if you can't understand it, otherwise just take it. Again, I'm not going to make this better, I just did it for a break program, so enjoy!
I guess I wasn't very clear.

By combining ANSI(ASCII, if you please) and graphics I mean:

CHR\$(219) or â (under screen 13, it prints a square 8x8(that's our ball)) and lines, say LINE(0, 40)-(0, 120) for an 80 pix line that is divisible by 8 (80 / 8 for 10 full ASCII spaces) and if

ballpositionx >= linetop AND ballpositiony <=linebottom THEN (then it makes the ball deflect and continue, if it is <> then it adds a goal.

The reason I haven't posted my program is this:

1. The computer I use internet isn't the computer I program on.

2. I have to go into DOS and copy the file to a floppy, then copy it to the inet computer. Then, open it in Notepad and paste it to the site.

I haven't done this because I don't have internet and I have to post at school.

I shall have it tomorrow!
hey! No you wern't clear...but hey! no worries:
Here's my modified entry:
[syntax="qbasic"]
RANDOMIZE TIMER
CLEAR
SCREEN 13
CLS
BX% = 20
BY% = 12
IF INT(RND * (1 - 0 + 1) + 0) THEN x% = -1: ELSE x% = 1
IF INT(RND * (1 - 0 + 1) + 0) THEN y% = -1: ELSE y% = 1
BXV% = INT(RND * (2 - 1 + 1) + 1) * x%
BYV% = INT(RND * (2 - 1 + 1) + 1) * y%
R% = 2
score1% = 0: Score2% = 0
index% = 0
COLOR 4
PRINT "RULES: GO TO 10 POINTS"
PRINT "IF AI SYSTEM GOES TO A NO-WIN LOOP IT RESETS"
PRINT "PRESS ANY KEY TO CONTINUE"
DO: LOOP WHILE INKEY\$ = ""
CLS
DO
IF BXV% < 0 THEN
IF Paddle1Y% + 4 > 23 THEN Paddle1Y% = 23 - 4
END IF
IF BXV% > 0 THEN
IF Paddle2Y% + 4 > 23 THEN Paddle2Y% = 23 - 4
END IF
BX% = BX% + BXV%
BY% = BY% + BYV%
IF BX% > 40 THEN
CLS
COLOR 4
PRINT "POINTPLAYER 1!"
PRINT "PRESS ANY KEY TO CONTINUE"
DO: LOOP WHILE INKEY\$ = ""
CLS
BX% = 20: BY% = 12
score1% = score1% + 1
IF INT(RND * (1 - 0 + 1) + 0) THEN x% = -1: ELSE x% = 1
IF INT(RND * (1 - 0 + 1) + 0) THEN y% = -1: ELSE y% = 1
BXV% = INT(RND * (2 - 1 + 1) + 1) * x%
BYV% = INT(RND * (2 - 1 + 1) + 1) * y%
END IF
IF BX% < 1 THEN
CLS
COLOR 4
PRINT "POINTPLAYER 2!"
PRINT "PRESS ANY KEY TO CONTINUE"
DO: LOOP WHILE INKEY\$ = ""
BX% = 20: BY% = 12
CLS
Score2% = Score2% + 1
IF INT(RND * (1 - 0 + 1) + 0) THEN x% = -1: ELSE x% = 1
IF INT(RND * (1 - 0 + 1) + 0) THEN y% = -1: ELSE y% = 1
BXV% = INT(RND * (2 - 1 + 1) + 1) * x%
BYV% = INT(RND * (2 - 1 + 1) + 1) * y%
END IF

IF BY% > 23 THEN BY% = 23: BYV% = BYV% * -1
IF BY% < 1 THEN BY% = 1: BYV% = BYV% * -1
LOCATE BY%, BX%: COLOR 4: PRINT CHR\$(219)
oBX% = BX%: oBY% = BY%
LOCATE 1, 10: COLOR 4: PRINT score1%
LOCATE 1, 30: COLOR 4: PRINT Score2%
doR% = 0
doY% = 0
IF x2% = BX% - 1 OR x2% = BX% THEN doY% = 1
IF doY% = 1 THEN
IF y2% = BY% THEN doR% = 1
IF y2% = BY% - 1 THEN doR% = 1
IF y2% = BY% - 2 THEN doR% = 1
IF y2% = BY% - 3 THEN doR% = 1
IF y2% = BY% - 4 THEN doR% = 1
END IF
IF doR% = 1 THEN
BXV% = BXV% * -1
END IF
doR% = 0
doY% = 0
IF x2% = BX% + 1 OR x2% = BX% THEN doY% = 1
IF doY% = 1 THEN
IF y2% = BY% THEN doR% = 1
IF y2% = BY% - 1 THEN doR% = 1
IF y2% = BY% - 2 THEN doR% = 1
IF y2% = BY% - 3 THEN doR% = 1
IF y2% = BY% - 4 THEN doR% = 1
END IF
IF doR% = 1 THEN
BXV% = BXV% * -1
END IF
WAIT &H3DA, 8
WAIT &H3DA, 8, 8
LOCATE oBY%, oBX%: COLOR 0: PRINT CHR\$(217)
index% = index% + 1
IF index% >= 1500 THEN
CLS
COLOR 4
PRINT "TIMEOUTRESET"
PRINT "PRESS ANY KEY TO CONTINUE"
DO: LOOP WHILE INKEY\$ = ""
CLS
index% = 0
BX% = 20: BY% = 12
IF INT(RND * (1 - 0 + 1) + 0) THEN x% = -1: ELSE x% = 1
IF INT(RND * (1 - 0 + 1) + 0) THEN y% = -1: ELSE y% = 1
BXV% = INT(RND * (3 - 1 + 1) + 1) * x%
BYV% = INT(RND * (3 - 1 + 1) + 1) * y%
END IF
LOOP UNTIL score1% >= 10 OR Score2% >= 10
CLS
COLOR 4
IF score1% > Score2% THEN
PRINT "PLAYER 1 WINS!"
ELSE
IF score1% < Score2% THEN
PRINT "PLAYER 2 WINS!"
ELSE
PRINT "TIE!"
END IF
END IF
END
[/syntax]
Cool. Here's mine. I made it last night, and ran it for 4 hours and nobody scored! cool?

Code:
```SCREEN 13 RANDOMIZE TIMER CLS A = 20: B = 12: C = 1: D = 1 TOP1 = 40: TOP2 = 40: BOT1 = 120: BOT2 = 120 DO FOR I% = 1 TO 20                              'Change if it's too fast or slow    LOCATE A - C, B - D: PRINT CHR\$(32)    LOCATE A, B: PRINT CHR\$(219)               'Here's the ball    LINE (8, TOP1)-(11, BOT1), , BF    LINE (8, BOT1)-(11, 200), 0, BF    LINE (8, 0)-(11, TOP1), 0, BF    LINE (312, TOP2)-(309, BOT2), , BF    LINE (312, BOT2)-(309, 200), 0, BF    LINE (312, 0)-(309, TOP2), 0, BF NEXT I% IF INKEY\$ > CHR\$(0) THEN GOTO MISS LOCATE A, B: PRINT CHR\$(0) A = A + C B = B + D IF A > 21 OR A < 3 THEN C = -C IF B < 3 THEN IF (A - C) * 8 < TOP1 OR A * 8 > BOT1 THEN GOTO MISS ELSE D = -D IF B > 38 THEN IF (A - C) * 8 < TOP2 OR A * 8 > BOT2 THEN GOTO MISS ELSE D = -D IF B <= 20 THEN    IF D = -1 AND C = 1 THEN TOP1 = TOP1 + 8: BOT1 = BOT1 + 8    IF D = -1 AND C = -1 THEN TOP1 = TOP1 - 8: BOT1 = BOT1 - 8    IF TOP2 < 48 THEN TOP2 = TOP2 + 8: BOT2 = BOT2 + 8    IF TOP2 > 48 THEN TOP2 = TOP2 - 8: BOT2 = BOT2 - 8 END IF IF B >= 20 THEN    IF D = 1 AND C = 1 THEN TOP2 = TOP2 + 8: BOT2 = BOT2 + 8    IF D = 1 AND C = -1 THEN TOP2 = TOP2 - 8: BOT2 = BOT2 - 8    IF TOP1 < 48 THEN TOP1 = TOP1 + 8: BOT1 = BOT1 + 8    IF TOP1 > 48 THEN TOP1 = TOP1 - 8: BOT1 = BOT1 - 8 END IF IF TOP1 <= 0 THEN TOP1 = 0: BOT1 = 80 IF TOP1 >= 120 THEN TOP1 = 120: BOT1 = 200 IF TOP2 <= 0 THEN TOP2 = 0: BOT2 = 80 IF TOP2 >= 120 THEN TOP2 = 120: BOT2 = 200 LOOP MISS: END```

If it's too fast, modify the FOR with the graphics in it.
Pages: 1 2 3 4 5