MY FIRST GAME!!!!!! PONG!!!!!!

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

MY FIRST GAME!!!!!! PONG!!!!!!

Post by Frosty480 » Mon Aug 27, 2007 12:03 pm

Ok, well, after working for almost 3 days, I've finally finished my first game!!!! Its pong, and its beautiful!!!

Here it is -

Code: Select all

'Setup the screen...
SCREEN 7, 0, 1, 0
LINE (0, 0)-(320, 200), 0, BF

'DIMing the needed arrays for graphic storage
DIM p1(3, 25)
DIM p2(3, 25)
DIM ball(14, 12)
DIM p1c(55)

'Needed variables
p1y = 75
p2y = 75
ballx = 150
bally = 30
score = 0
scorex = 8
scorey = 1
scorewrdx = 2
scorewrdy = 1
xadj = 2
yadj = 2

'Load the paddles into memory
LINE (10, 10)-(15, 65), 1, BF
GET (10, 10)-(15, 65), p1
GET (10, 10)-(15, 65), p2
LINE (0, 0)-(320, 200), 0, BF

'Load the ball into memory
CIRCLE (20, 20), 7, 4
PAINT (20, 20), 4
GET (13, 14)-(27, 26), ball
LINE (0, 0)-(320, 200), 0, BF

'Main game loop
DO
press$ = INKEY$                 'Store keypresses
LINE (0, 0)-(320, 200), 0, BF   'Clear the screen. WAY faster than CLS
LOCATE scorewrdy, scorewrdx     '  Print
PRINT "Score: "                 '        the
LOCATE scorey, scorex           '              score
PRINT score                     '
WAIT &H3DA, 8                   'Just something to make the graphics good

IF p1y >= 5 AND press$ = CHR$(0) + CHR$(72) THEN p1y = p1y - 3      'Player 1
IF p1y <137>= 300 THEN xadj = -2          ' from going off the screen)
IF ballx <= 5 THEN xadj = 2             '

IF ballx = 18 AND bally <p1y>= p1y - 2 THEN  '
xadj = 2                                                       ' Ball bouncing
score = score + 1                                              ' for player 1's
END IF                                                         ' paddle

IF ballx = 290 AND bally <p2y>= p2y - 2 THEN ' Ball bouncing
xadj = -2                                                      ' for computer's
END IF                                                         ' paddle

ballx = ballx + xadj              'The bouncing part
bally = bally + yadj              'of the "Ball bouncing"

IF ballx = 10 THEN                 'Checks to see if the ball went off the
LINE (0, 0)-(320, 200), 0, BF      'players side of the screen
PRINT "You lost!!!"                'Is so, print "You lost"
PCOPY 1, 0                         'copy page 1 to page 0
FOR delay = 1 TO 50000: NEXT       'Just a delay
END
END IF

IF score = 20 THEN                 'Checks to see if you've won
SLEEP 1                            'If so, wait 1 sec
LINE (0, 0)-(320, 200), 0, BF      'Clear screen
LINE (0, 0)-(320, 200), 2, BF      'Fill screen green
LOCATE 12, 12                      'Move cursor to 12,12
COLOR 4                            'Set the color to red (4)
PRINT "You won!!!!"                'Print "You won!"
PCOPY 1, 0                         'Copy page 1 to page 0
SLEEP 3                            'Wait 3 seconds
COLOR 15                           'Set the color back to white (15)
END
END IF

IF ballx = 300 THEN                'Checks to see if the computer missed
LINE (0, 0)-(320, 200), 0, BF      'If so, clear screen
score = score + 1                  'And add one to score
END IF

IF p2y > 5 AND bally < p2y THEN p2y = p2y - 3        'Computer paddle AI
IF p2y <127> p2y THEN p2y = p2y + 3      '

PUT (18, p1y), p1             'Draw objects
PUT (295, p2y), p2            'at their
PUT (ballx, bally), ball      'final positions

PCOPY 1, 0                 'Show everything
LOOP                       'End of main loop

Tell me what you think!!

Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe » Mon Aug 27, 2007 1:00 pm

I get an "illegal function call" on this line:

Code: Select all

PUT (ballx, bally), ball      'final positions 
Here's what I pulled from the instant watch feature:

ballx: 306
bally: 186

It looks like some of the sprite was drawn beyond the edges of the screen.

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Mon Aug 27, 2007 1:51 pm

? It worked for me...

What QB Version are you using? I have 4.5. Maybe thats the problem?

Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe » Mon Aug 27, 2007 2:03 pm

4.5

Windows XP Professional
Pentium M, 1.7ghz

It runs for the first second, but when the ball goes off the screen it crashes the program.

Also, what are the controls? All I can find in the code is a reference to one keypress. How can you play the game with only one key?
Last edited by Joe on Mon Aug 27, 2007 2:12 pm, edited 1 time in total.

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Mon Aug 27, 2007 2:09 pm

Oh, well, then I really don't know. Can I upload the EXE file?

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Mon Aug 27, 2007 2:20 pm

I don't know why that happens, as it works fine for me. And the controls are the up and down arrow keys.

And how are you running the code? Did you paste it directly in the DOS window? Because that doesn't always work right. Try pasting it into Notepad and saving it as a .bas file.

Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe » Mon Aug 27, 2007 2:26 pm

You must have pasted the wrong version of the code or something. Look up at your first post with the code. There is no check for the down arrow.

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Mon Aug 27, 2007 2:27 pm

Hey, wait a second, I just checked the code with my copy, and it has a HUGE chunk missing...

It turns out that this forum cuts off the code... Here, let me upload it to a site and link to it here...

Here, go download it and see if it works -

http://www.mediafire.com/?anaj1meo95y

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Mon Aug 27, 2007 2:29 pm

How is it?

Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe » Mon Aug 27, 2007 2:31 pm

Much better. :)

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Mon Aug 27, 2007 2:36 pm

Cool :D

So is it a good game? It's my first one, and I would really appreciate some pointers.

Oh, and how do you use .bmp files in your game? Is it possible?

Joe
Coder
Posts: 20
Joined: Wed Aug 15, 2007 3:11 pm
Contact:

Post by Joe » Mon Aug 27, 2007 2:42 pm

It took me a few tries, but I managed to beat it. Not bad. Faster response and movement for the paddle would be nice, but then that would take away the challenge factor. ;)

You can load bmp files in QB. All you need to know is how to load a file and understand the bmp file structure. But if you don't have the time for that then there are libraries you can use that will do it for you. DirectQB and FutureLib are good ones. You can find them, along with others, here.

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Mon Aug 27, 2007 2:43 pm

Cool, Thanks!!!!!!

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Mon Aug 27, 2007 8:29 pm

Here, try this:

... 'normal code
PSET (X,Y)
PSET (U,V),0 'turns old image off
LET U=X
LET V=Y
...

That makes you pong ball move very quickly while using little memory.
MAKE SURE TO SET U AND V TO X AND Y AFTER PSET!
Good luck. That was my first program.
For any grievances posted above, I blame whoever is in charge . . .

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Tue Aug 28, 2007 1:28 pm

Where do I put the PSET?

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Tue Aug 28, 2007 4:27 pm

Uh.. where you normaly print the graphics. :D
It's a good idea to take advantage of modular programming. By that I mean you have work done in components of the program (subroutines) so editing and debugging is MUCH easier.
Code clarity may seem tedious and unnecasary, but trust me, it pays off. :wink:
If you don't believe me, go code a 1k program in TI-BASIC, leave it for at least a week, and go back. I had to delete my best because of obscurity (WHAT DOES (G=4)G MEAN!? :evil: ).

I won't tell you where to exactly put code, because you will (and should) develope your own style. And me telling you what to put where doesn't help you become familiar with your program.
For any grievances posted above, I blame whoever is in charge . . .

Frosty480
Coder
Posts: 17
Joined: Mon Aug 27, 2007 11:59 am
Contact:

Post by Frosty480 » Wed Aug 29, 2007 10:31 am

Weeeeeeeeeeeelllllllllllllllll......... The thing is, I NEVER use PSET in my program, I use PUT...

And I've tried SUBs, but I mainly use them for useful functions. Like faster CLSs and PSETS. I call them XPSET and XCLS :)

The theres also XLEFT, XRIGHT, XUP, XDOWN. But those are recent. I use them for the arrow keys.

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Wed Aug 29, 2007 4:33 pm

What ever works 8) . But keep in mind, Pong isn't exactly a CPU hungry game so optimization isn't important. It's good for practice though. I don't use PUT (or anything directly linked to memory adresses) because I don't know how, but I've managed to do without such pretty well. :D
For any grievances posted above, I blame whoever is in charge . . .

Post Reply