Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Breakout
#1
Nothing fancy going on here. Arrow keys to move.
Code: (Select All)
$COLOR:32
CONST BALL_RADIUS = 5
CONST PADDLE_SPAN = 25
CONST PADDLE_HEIGHT = 10
CONST PADDLE_VELOCITY = 5
CONST PADDLE_GRADIENT = 0.3
CONST BLOCK_WIDTH = 42
CONST BLOCK_HEIGHT = 10
CONST BLOCK_MARGIN = 10
CONST NUM_BLOCKS = 36
CONST SPEED_PER_LEVEL = 1.1

CONST FALSE = 0, TRUE = NOT FALSE
CONST KEY_LEFT = 19200
CONST KEY_RIGHT = 19712

TYPE block_t
    x1 AS INTEGER
    x2 AS INTEGER
    y1 AS INTEGER
    y2 AS INTEGER
    alive AS INTEGER
    colour AS _UNSIGNED LONG
END TYPE
DIM blocks(1 TO NUM_BLOCKS) AS block_t

DIM SHARED colours(1 TO 12) AS _UNSIGNED LONG
colours(1) = Peru
colours(2) = RazzleDazzleRose
colours(3) = Wheat
colours(4) = WildBlueYonder
colours(5) = Lavender
colours(6) = MountainMeadow
colours(7) = BurntOrange
colours(8) = PeachPuff
colours(9) = LawnGreen
colours(10) = Chestnut
colours(11) = IndianRed
colours(12) = LightGoldenRodYellow

SCREEN _NEWIMAGE(640, 480, 32)
RANDOMIZE TIMER

start:
level = 0

next_level:
level = level + 1
ball_vy = -(3 + SPEED_PER_LEVEL * level)
win_condition = FALSE
init_blocks blocks()
ball_x = _WIDTH / 2
ball_vx = INT(RND * 5)
ball_y = _HEIGHT * 0.9
paddle_x = _WIDTH / 2
paddle_y = _HEIGHT - PADDLE_HEIGHT - 10
CLS
centreprint "Level" + STR$(level)
_DISPLAY
_DELAY 2

DO
    CLS , 0
    'Paddle movement
    IF _KEYDOWN(KEY_LEFT) THEN paddle_x = paddle_x - PADDLE_VELOCITY
    IF paddle_x < PADDLE_SPAN THEN paddle_x = PADDLE_SPAN
    IF _KEYDOWN(KEY_RIGHT) THEN paddle_x = paddle_x + PADDLE_VELOCITY
    IF paddle_x > _WIDTH - PADDLE_SPAN THEN paddle_x = _WIDTH - PADDLE_SPAN
    'Ball movement
    ball_x = ball_x + ball_vx
    ball_y = ball_y + ball_vy
    'Draw paddle
    LINE (paddle_x - PADDLE_SPAN, paddle_y)-(paddle_x + PADDLE_SPAN, paddle_y + PADDLE_HEIGHT), White, BF
    'Draw ball
    CIRCLE (ball_x, ball_y), BALL_RADIUS, White
    'Draw blocks & block collision
    win_condition = TRUE
    FOR b = LBOUND(blocks) TO UBOUND(blocks)
        IF NOT blocks(b).alive THEN _CONTINUE
        top_bound = ball_y + BALL_RADIUS >= blocks(b).y1
        bottom_bound = ball_y - BALL_RADIUS <= blocks(b).y2
        left_bound = ball_x + BALL_RADIUS >= blocks(b).x1
        right_bound = ball_x - BALL_RADIUS <= blocks(b).x2
        IF top_bound AND left_bound AND right_bound AND bottom_bound THEN
            ball_vy = -ball_vy
            blocks(b).alive = FALSE
        ELSE
            LINE (blocks(b).x1, blocks(b).y1)-(blocks(b).x2, blocks(b).y2), blocks(b).colour, BF
            win_condition = FALSE
        END IF
    NEXT b
    'Win?
    IF win_condition THEN GOTO next_level
    'Boundary collision
    IF ball_x - BALL_RADIUS + ball_vx <= 0 OR ball_x + BALL_RADIUS + ball_vx >= _WIDTH THEN ball_vx = -ball_vx
    IF ball_y - BALL_RADIUS + ball_vy <= 0 THEN ball_vy = -ball_vy
    'Paddle collision
    IF ABS(ball_x - paddle_x) <= PADDLE_SPAN + BALL_RADIUS AND ball_y + BALL_RADIUS >= paddle_y THEN
        ball_vy = -ball_vy
        ball_vx = (ball_x - paddle_x) * PADDLE_GRADIENT
    END IF
    'Missed ball
    IF ball_y - BALL_RADIUS >= _HEIGHT THEN
        centreprint "Game Over"
        _DELAY 2
        GOTO start
    END IF
    _DISPLAY
    _LIMIT 60
LOOP
PRINT "Game over"

SUB init_blocks (blocks() AS block_t)
    x = BLOCK_MARGIN
    y = BLOCK_MARGIN
    FOR i = LBOUND(blocks) TO UBOUND(blocks)
        blocks(i).alive = TRUE
        IF x + BLOCK_WIDTH > _WIDTH THEN
            x = BLOCK_MARGIN
            y = y + BLOCK_HEIGHT + BLOCK_MARGIN
        END IF
        blocks(i).x1 = x
        blocks(i).x2 = x + BLOCK_WIDTH
        blocks(i).y1 = y
        blocks(i).y2 = y + BLOCK_HEIGHT
        blocks(i).colour = colours(INT(RND * UBOUND(colours)) + 1)
        x = x + BLOCK_WIDTH + BLOCK_MARGIN
    NEXT i
END SUB

SUB centreprint (s$)
    _PRINTSTRING ((_WIDTH - _PRINTWIDTH(s$)) / 2, (_HEIGHT - _FONTHEIGHT) / 2), s$
    _DISPLAY
END SUB
Reply
#2
Fun Smile

Add sound effects, particle effects, and explosions! Everybody likes explosions!
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#3
(06-13-2024, 12:28 PM)luke Wrote: Nothing fancy going on here. Arrow keys to move.

Nothing fancy, just a 134 line breakout clone. LOL. Your humility is awesome.

This is fantastic work. I'm going to dissect the crap out of this and try and get some of your nothing fancy to rub off on me. Smile

Thanks for sharing!

Any other goodies like this you've got tucked away, please do share!

Also to @TerryRitchie point yes! Add sound effects! Add music! Big Grin I can help you with these things if you like. This would serve as a great example too on the Wiki I think @RhoSigma @SMcNeill
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#4
@luke

How do you make the ball faster when it's on the edges of the paddle? I've looked and I can't sus that out. Could you please provide an explanation on that?

Thanks
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#5
@grymmjack look for ball vx and vy changes, v for velocity.

_vx uses a PADDLE_GRADIENT multiplier

line 100: ball_vx = (ball_x - paddle_x) * PADDLE_GRADIENT
b = b + ...
Reply
#6
(06-13-2024, 05:09 PM)TerryRitchie Wrote: Fun Smile

Add sound effects, particle effects, and explosions! Everybody likes explosions!

How about crunching and exploding spiders?
Creep Out
https://qb64phoenix.com/forum/showthread...2#pid21042
b = b + ...
Reply
#7
(07-21-2024, 07:26 PM)bplus Wrote: @grymmjack look for ball vx and vy changes, v for velocity.

_vx uses a PADDLE_GRADIENT multiplier

line 100: ball_vx = (ball_x - paddle_x) * PADDLE_GRADIENT

Ah, OK thank you.
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#8
Nice little game! Really tight coding here.

- Dav

Find my programs here in Dav's QB64 Corner
Reply
#9
Nice beginner game. We can teach you how to make the next one without GOTO. Big Grin 

+1 to get you out of unlucky (13).

Pete
Fake News + Phony Politicians = Real Problems

Reply




Users browsing this thread: 6 Guest(s)