Qbasicnews.com
June 18, 2018, 12:51:31 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Simple 1 or 2 player game  (Read 6540 times)
Atom Ant
Member
*
Posts: 78



« on: April 28, 2007, 06:28:34 PM »

Just a little game I whipped up over the past few days.  Tell me what you think. Smiley

EDIT:  By the way, you can easily change the way the game plays by editting the values of the variables near the top of the code.

EDIT2:  I just realized I hadn't posted the controls.  :|
Player 1:
Left: Left
Right: Right

Player 2:
Left: A
Right: D

Code:

OPTION EXPLICIT

RANDOMIZE TIMER

DIM paddleWidth AS INTEGER
DIM paddleHeight AS INTEGER
DIM p1PaddleX AS INTEGER
DIM p1PaddleY AS INTEGER
DIM p1PaddleState AS INTEGER
DIM p2PaddleX AS INTEGER
DIM p2PaddleY AS INTEGER
DIM p2PaddleState AS INTEGER
DIM blockerWidth AS INTEGER
DIM blockerHeight AS INTEGER
DIM blockerX AS INTEGER
DIM blockerY AS INTEGER
DIM ballSize AS INTEGER
DIM ballX AS DOUBLE
DIM ballY AS DOUBLE
DIM ballMovementX AS DOUBLE
DIM ballMovementMaxX AS INTEGER
DIM ballMovementY AS DOUBLE
DIM ballSpeedInc AS DOUBLE
DIM ballCurve AS DOUBLE
DIM ballCurveState AS INTEGER
DIM paddleMovement AS INTEGER
DIM blockerMovement AS INTEGER
DIM p1Color AS INTEGER
DIM p2Color AS INTEGER
DIM blockerColor AS INTEGER
DIM ballColor AS INTEGER
DIM clr AS INTEGER
DIM gameState AS INTEGER
DIM p1Points AS INTEGER
DIM p2Points AS INTEGER
DIM pointsToWin AS INTEGER
DIM timeCheck AS DOUBLE
DIM roundWait AS INTEGER
DIM endGameWait AS INTEGER
DIM playerNum AS INTEGER
DIM mouseX AS INTEGER
DIM mouseButton AS INTEGER
DIM unused AS INTEGER

LET paddleWidth = 100
LET paddleHeight = 10
LET blockerWidth = 160
LET blockerHeight = paddleHeight
LET ballSize = 5
LET ballMovementMaxX = 10
LET ballSpeedInc = .5
LET ballCurve = .05
LET blockerMovement = 2
LET paddleMovement = 5
LET p1Color = 0
LET p2Color = 0
LET p1Points = 0
LET p2Points = 0
LET pointsToWin = 3
LET roundWait = 3
LET endGameWait = 3

SCREEN 12, 16, 2, 0
SCREENSET 0, 1

LINE (320, 0)-(320, 479)
LOCATE 15, 16
PRINT "1 Player"
LOCATE 15, 56
PRINT "2 Player"

SCREENCOPY

LET playerNum = 0
DO WHILE playerNum = 0
    GETMOUSE(mouseX, unused,, mouseButton)
    IF mouseButton <> 0 AND mouseButton <> -1 THEN
        IF mouseX < 320 THEN
                LET playerNum = 1
            ELSEIF mouseX > 320 THEN
                LET playerNum = 2
        END IF
    END IF
   
    SCREENSYNC
LOOP

SETMOUSE (,,0)

CLS
FOR clr = 1 TO 15
    COLOR clr
    PRINT clr
NEXT clr

SCREENCOPY
SCREENSET 0, 0

DO WHILE p1Color = 0
    INPUT "Player 1 color number: ", p1Color
    SCREENSYNC
LOOP

DO WHILE p2Color = 0
    INPUT "Player 2 color number: ", p2Color
    SCREENSYNC
LOOP

SCREENSET 0, 1

DO WHILE p1Points < pointsToWin AND p2Points < pointsToWin
    LET p1PaddleX = 320
    LET p1PaddleY = 479 - (paddleHeight / 2)
    LET p1PaddleState = 0
    LET p2PaddleX = 320
    LET p2PaddleY = paddleHeight / 2
    LET p2PaddleState = 0
    LET blockerX = blockerWidth / 2
    LET blockerY = 240
    LET ballX = 320
    LET ballY = 240
    LET ballMovementX = RND * 2 - 1
    LET ballMovementY = 0
    DO WHILE ballMovementY = 0
        LET ballMovementY = INT(RND * 3 - 1)
    LOOP
    LET ballCurveState = 0
    LET ballColor = 7
    LET blockerColor = 8
    LET gameState = 0
   
    DO WHILE gamestate = 0
        IF (MULTIKEY(&h4b) AND MULTIKEY(&h4D)) OR (NOT MULTIKEY(&h4b) AND NOT MULTIKEY(&h4D)) THEN
                LET p1PaddleState = 0
            ELSEIF MULTIKEY(&h4b) THEN
                LET p1PaddleX = p1PaddleX - paddleMovement
                LET p1PaddleState = -1
            ELSEIF MULTIKEY(&h4D) THEN
                LET p1PaddleX = p1PaddleX + paddleMovement
                LET p1PaddleState = 1
        END IF
       
        IF playerNum = 2 THEN
                IF (MULTIKEY(&h1E) AND MULTIKEY(&h20)) OR (NOT MULTIKEY(&h1E) AND NOT MULTIKEY(&h20)) THEN
                        LET p2PaddleState = 0
                    ELSEIF MULTIKEY(&h1E) THEN
                        LET p2PaddleX = p2PaddleX - paddleMovement
                        LET p2PaddleState = -1
                    ELSEIF MULTIKEY(&h20) THEN
                        LET p2PaddleX = p2PaddleX + paddleMovement
                        LET p2PaddleState = 1
                END IF
            ELSEIF playerNum = 1 THEN
                IF p2PaddleX - (paddleWidth / 4) < ballX AND p2PaddleX + (paddleWidth / 4) > ballX THEN
                        IF ballY <= paddleHeight + ballSize + ABS(ballMovementY) THEN
                                IF INT(RND * 2) = 1 AND ballMovementY < 0 THEN
                                        IF p2PaddleX > ballX THEN
                                                LET p2PaddleX = p2PaddleX - paddleMovement
                                                LET p2PaddleState = -1
                                            ELSE
                                                LET p2PaddleX = p2PaddleX + paddleMovement
                                                LET p2PaddleState = 1
                                        END IF
                                    ELSE
                                        LET p2PaddleState = 0
                                END IF
                            ELSE
                                LET p2PaddleState = 0
                        END IF
                    ELSEIF p2PaddleX - (paddleWidth / 4) > ballX THEN
                        LET p2PaddleX = p2PaddleX - paddleMovement
                        LET p2PaddleState = -1
                    ELSEIF p2PaddleX + (paddleWIdth / 4) < ballX THEN
                        LET p2PaddleX = p2PaddleX + paddleMovement
                        LET p2PaddleState = 1
                END IF
        END IF
       
        IF p1PaddleX < paddleWidth / 2 THEN
                LET p1PaddleX = paddleWidth / 2
            ELSEIF p1PaddleX > 639 - (paddleWidth / 2) THEN
                LET p1PaddleX = 639 - (paddleWidth / 2)
        END IF
       
        IF p2PaddleX < paddleWidth / 2 THEN
                LET p2PaddleX = paddleWidth / 2
            ELSEIF p2PaddleX > 639 - (paddleWidth / 2) THEN
                LET p2PaddleX = 639 - (paddleWidth / 2)
        END IF
       
        LET blockerX = blockerX + blockerMovement
        IF blockerX < blockerWidth / 2 OR blockerX > 639 - (blockerWidth / 2) THEN
            LET blockerMovement = -blockerMovement
        END IF
   
        IF (ballX > (p1PaddleX - (paddleWidth / 2)) - ballSize AND ballX < (p1PaddleX + (paddleWidth / 2)) + ballSize) AND ballY > (p1PaddleY - (paddleHeight / 2)) - ballSize THEN
                LET ballMovementY = -ballMovementY
                IF ABS(ballMovementY) < paddleHeight THEN
                    IF ballMovementY > 0 THEN
                            LET ballMovementY = ballMovementY + ballSpeedInc
                        ELSE
                            LET ballMovementY = ballMovementY - ballSpeedInc
                    END IF
                END IF
                IF p1PaddleState = 1 THEN
                        LET ballCurveState = 1
                    ELSEIF p1PaddleState = -1 THEN
                        LET ballCurveState = -1
                    ELSE
                        LET ballCurveState = 0
                END IF
                LET ballMovementX = ballMovementX + ((ballX - p1PaddleX) / 15)
                IF ballMovementX < -ballMovementMaxX THEN LET ballMovementX = -ballMovementMaxX
                IF ballMovementX > ballMovementMaxX THEN LET ballMovementX = ballMovementMaxX
                LET ballY = 479 - paddleHeight - ballSize
                LET ballColor = p1Color
            ELSEIF (ballX > (p2PaddleX - (paddleWidth / 2)) - ballSize AND ballX < (p2PaddleX + (paddleWidth / 2)) + ballSize) AND ballY < (p2PaddleY + (paddleHeight / 2)) + ballSize THEN
                LET ballMovementY = -ballMovementY
                IF ABS(ballMovementY) < paddleHeight THEN
                    IF ballMovementY > 0 THEN
                            LET ballMovementY = ballMovementY + ballSpeedInc
                        ELSE
                            LET ballMovementY = ballMovementY - ballSpeedInc
                    END IF
                END IF
                IF p2PaddleState = 1 THEN
                        LET ballCurveState = 1
                    ELSEIF p2PaddleState = -1 THEN
                        LET ballCurveState = -1
                    ELSE
                        LET ballCurveState = 0
                END IF
                LET ballMovementX = ballMovementX + ((ballX - p2PaddleX) / 15)
                IF ballMovementX < -ballMovementMaxX THEN LET ballMovementX = -ballMovementMaxX
                IF ballMovementX > ballMovementMaxX THEN LET ballMovementX = ballMovementMaxX
                LET ballY = paddleHeight + ballSize
                LET ballColor = p2Color
        END IF
       
        IF (ballX > (blockerX - (blockerWidth / 2)) - ballSize AND ballX < (blockerX + (blockerWidth / 2)) + ballSize) AND (ballY > (blockerY - (blockerHeight / 2)) - ballSize AND ballY < (blockerY + (blockerHeight / 2)) + ballSize) THEN
            LET ballMovementY = -ballMovementY
            LET blockerColor = ballColor
            IF ballMovementY <= 0 THEN
                    LET ballY = blockerY - (blockerHeight / 2) - ballSize
                ELSE
                    LET ballY = blockerY + (blockerHeight / 2) + ballSize
            END IF
        END IF
       
        LET ballMovementX = ballMovementX + (ballCurve * ballCurveState)
       
        IF ballX > 639 - ballSize THEN
                LET ballX = 639 - ballSize
                LET ballMovementX = -ballMovementX
            ELSEIF ballX < ballSize THEN
                LET ballX = ballSize
                LET ballMovementX = -ballMovementX
        END IF
       
        IF ballY > 479 - ballSize THEN
                LET gameState = 1
            ELSEIF ballY < ballSize THEN
                LET gameState = -1
        END IF
   
        LET ballX = ballX + ballMovementX
        LET ballY = ballY + ballMovementY
       
        CIRCLE (315, 343), 10, p1Color,,,, F
        CIRCLE (315, 343), 12, p1Color
        CIRCLE (315, 343), 15, p1Color
        CIRCLE (315, 343), 20, p1Color
        CIRCLE (315, 343), 30, p1Color
        CIRCLE (315, 119), 10, p2Color,,,, F
        CIRCLE (315, 119), 12, p2Color
        CIRCLE (315, 119), 15, p2Color
        CIRCLE (315, 119), 20, p2Color
        CIRCLE (315, 119), 30, p2Color
        COLOR 0, p2Color
        LOCATE 8, 40
        PRINT STR(p2Points)
        COLOR 0, p1Color
        LOCATE 22, 40
        PRINT STR(p1Points)
        COLOR 0, 0
        CIRCLE (ballX, ballY), ballSize, ballColor,,,, F
        LINE (blockerX - blockerWidth / 2, blockerY - blockerHeight / 2)-(blockerX + blockerWidth / 2, blockerY + blockerHeight / 2), blockerColor, BF
        LINE (p1PaddleX - paddleWidth / 2, p1PaddleY - paddleHeight / 2)-(p1PaddleX + paddleWidth / 2, p1PaddleY + paddleHeight / 2), p1Color, BF
        LINE (p2PaddleX - paddleWidth / 2, p2PaddleY - paddleHeight / 2)-(p2PaddleX + paddleWidth / 2, p2PaddleY + paddleHeight / 2), p2Color, BF
       
        SCREENSYNC
        SCREENCOPY
        CLS
    LOOP
   
    IF gameState = -1 THEN
            LET p1Points = p1Points + 1
        ELSEIF gameState = 1 THEN
            LET p2Points = p2Points + 1
    END IF
   
    LET timeCheck = TIMER
    DO WHILE timeCheck > TIMER - roundWait
        SCREENSYNC
    LOOP
LOOP

LOCATE 15, 33
IF p1Points > p2Points THEN
        COLOR p1Color
        PRINT "Player 1 wins!"
    ELSE
        COLOR p2Color
        PRINT "Player 2 wins!"
END IF

SCREENCOPY

LET timeCheck = TIMER
DO WHILE timeCheck > TIMER - endGameWait
    SCREENSYNC
LOOP
Logged

nd remember kids, only you can provoke forest fires!
Skyler
Ancient Guru
****
Posts: 564



« Reply #1 on: April 28, 2007, 07:00:43 PM »

Hey, my ball just dodged!  Cheesy
Great new twist on an old game. Speaking of twists, is the ball attracted to the score, the middle paddle, or what? It seems to enjoy careening off in a seemingly random direction.
Logged

In the beginning, there is darkness the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Atom Ant
Member
*
Posts: 78



« Reply #2 on: April 28, 2007, 07:05:35 PM »

Well, it's not really attracted to anything, but the effect you're referring to is the spin put on the ball when you hit it while your paddle is moving.  Thanks for the feedback!
Logged

nd remember kids, only you can provoke forest fires!
Skyler
Ancient Guru
****
Posts: 564



« Reply #3 on: April 28, 2007, 07:55:16 PM »

No problem, I reply to almost any thread here, especially new ones. Maybe you should add a black hole or something that randomly appears and tries to suck the ball in...
Logged

In the beginning, there is darkness the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Atom Ant
Member
*
Posts: 78



« Reply #4 on: April 28, 2007, 08:10:19 PM »

Not a bad idea, I just might do that.
Logged

nd remember kids, only you can provoke forest fires!
Dio
I hold this place together
*****
Posts: 874



« Reply #5 on: April 30, 2007, 04:38:19 PM »

fun.
Logged

quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!