Log In  

I have some problems with my first brakeout test game. The ball is sometimes to fast or its transparent. i cannot find the problem.

this is my code:

ball_x=60
ball_y=113
ball_x_speed=1
ball_y_speed=-4
bat_x=48
bat_width=32

function _update()
move_bat()
move_ball()
check_border()
check_collision()
end

function _draw()
cls(0)
map(0,0,0,0,16,16)
spr(1,ball_x,ball_y)
draw_bat()
end

function move_bat()
if btn(0) and bat_x>10 then
bat_x=bat_x-3
elseif btn(1) and
bat_x<118-bat_width then
bat_x=bat_x+3

end

function move_ball()
ball_x=ball_x+ball_x_speed
ball_y=ball_y+ball_y_speed
end
end

function check_border()
if ball_x+7+ball_x_speed>120
or ball_x+ball_x_speed<9 then
ball_x_speed=-ball_x_speed
end

if ball_y+ball_y_speed<4 then
    ball_y_speed=-ball_y_speed
end

-- losing the ball

if ball_y>128 then
lose_life()
end

end

function draw_bat()
spr(48,bat_x,120)
spr(49,bat_x+8,120)
spr(49,bat_x+16,120)
spr(50,bat_x+24,120)
end

function check_collision()
if ball_y>110 and ball_y<120
and ball_x>=bat_x-6
and ball_x<bat_x+bat_width
and ball_y_speed>0 then
ball_y_speed=-ball_y_speed
ball_x_speed=ball_x_speed+
4/(ball_x+4-(bat_x+bat_width/2))
sfx(0)
end
end

function lose_life()
ball_x=bat_x+bat_width/2
ball_y=113
ball_x_speed=1
ball_y_speed=-4
sfx(1)
end

It would be great if someone could help me.
greetings from germany
slate

P#55788 2018-08-27 18:09 ( Edited 2018-08-28 00:36)

I'll likely be skinned alive for this because I've been told (amply) that I don't code conventionally, but here is a simple breakout with moving paddle and ball - and not much else.

x=0 y=33 ax=5 ay=4 px=60
repeat
  cls(1)
  circfill(x,y,3,10)
  x+=ax y+=ay
  if (x<0 or x>127) ax=-ax
  if (y<0 or y>127) ay=-ay
  if (btn(0)) px-=4
  if (btn(1)) px+=4
  rectfill(px-8,120,px+8,122,6)
  flip()
until forever

If you like, however, there is a complete video tutorial, very thorough, on how to write (the accepted way) your own Breakout game completely from scratch in Pico-8 found HERE:

https://www.youtube.com/watch?v=YQzwVDMIfyU&list=PLYND9uft5u_1YCkmXiMrPU7tiBG3hIKAZ&index=3&t=0s

Best of success to you !

P#55789 2018-08-27 18:38 ( Edited 2018-08-27 22:42)

For your speed issue, this line in check_collision is your problem:

ball_x_speed=ball_x_speed+4/(ball_x+4-(bat_x+bat_width/2))

Think about what happens when the distance between the ball and the bat is close to 0 or equal to 0.

(or don't think and use print() & assert() instead!)

edit: I didn't notice any transparent balls when I played your game (with my own art). It might be related to the speed, or it might be a rare bug I just didn't see. Fix the speed first and then check if it still happens.

P#55791 2018-08-27 20:36 ( Edited 2018-08-28 00:47)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 13:07:48 | 0.007s | Q:11