01-24-2024, 06:59 PM
(This post was last modified: 01-25-2024, 08:59 PM by aliensoldier.)
I'm trying to create the pong game and I'm having problems when the ball collides with the top and bottom of the moving player, the ball doesn't bounce, it just goes into the player's graph.
This happens when the player is moving, standing still it does not happen, the problem is in the ball.collision_player method of the ball.
I show all the code I have for now:
This happens when the player is moving, standing still it does not happen, the problem is in the ball.collision_player method of the ball.
I show all the code I have for now:
Code:
'pong
include "player.n7"
include "player-cpu.n7"
include "ball.n7"
set window "example pong",640,480,false
set redraw off
'objeto-----------------------------------
player = Player()
player_cpu = Player_Cpu()
ball = Ball()
while not keydown(KEY_ESCAPE,true)
set color 0,0,0
cls
'objetos-----------------------------
player.update()
player.Draw()
player_cpu.update()
player_cpu.Draw()
ball.update()
ball.Draw()
'lineas para el fondo------------------
'draw rect 320,32,2,60,true
'draw rect 320,120,2,60,true
'draw rect 320,210,2,60,true
'draw rect 320,300,2,60,true
'draw rect 320,390,2,60,true
'draw line 320,32,320,460
'draw line 330,32,330,460
redraw
fwait 60
wend
Code:
'player
visible player = []
function Player()
player.Width = 20
player.Height = 100
player.x = 32 - player.Width/2
player.y = 240 - player.Height/2
player.speed = 5
player.live = true
player.update = function()
if this.live = true
this.move()
endif
endfunc
player.Draw = function()
if this.live = true
set color 255,0,205
draw rect this.x,this.y,this.Width,this.Height,true
endif
endfunc
player.move = function()
if keydown(KEY_UP,false) and this.y > 16
this.y = this.y - this.speed
elseif keydown(KEY_DOWN,false) and this.y < 370
this.y = this.y + this.speed
endif
endfunc
return player
endfunc
function get_player()
return player
endfunc
Code:
'player-cpu
include "ball.n7"
visible ball = get_ball()
visible player_cpu = []
function Player_Cpu()
player_cpu.Width = 20
player_cpu.Height = 100
player_cpu.x = 608 - player_cpu.Width/2
player_cpu.y = 240 - player_cpu.Height/2
player_cpu.speed = 5
player_cpu.live = true
player_cpu.update = function()
endfunc
player_cpu.Draw = function()
if this.live
set color 255,204,0
draw rect this.x,this.y,this.Width,this.Height,true
endif
endfunc
return player_cpu
endfunc
function get_player_cpu()
return player_cpu
endfunc
Code:
'ball
include "player.n7"
include "miscellaneous.n7"
include "player-cpu.n7"
visible player = get_player()
visible player_cpu = get_player_cpu()
visible ball = []
function Ball()
ball.Width = 16
ball.Height = 16
ball.x = 320 - ball.Width / 2
ball.y = 240 - ball.Height / 2
ball.speedX = 2
ball.speedY = 2
ball.live = true
ball.update = function()
if this.live = true
this.move()
this.bounce()
this.collision_player()
endif
endfunc
ball.Draw = function()
if this.live = true
set color 200,200,200
draw rect this.x,this.y,this.Width,this.Height,true
'draw ellipse this.x,this.y,9,9,true
endif
endfunc
ball.move = function()
this.x = this.x + this.speedX
this.y = this.y + this.speedY
endfunc
ball.bounce = function()
if this.x <= 0 or this.x >= 640
this.speedX = this.speedX * -1
endif
if this.y <= 0 or this.y >= 480
this.speedY = this.speedY * -1
endif
endfunc
ball.collision_player = function()
'colision con la x
if collision_rect(this.x+this.speedX,this.y,this.Width,this.Height,player.x,player.y,player.Width,player.Height)
this.speedX = this.speedX * -1
endif
'colision con la y
if collision_rect(this.x,this.y+this.speedY,this.Width,this.Height,player.x,player.y,player.Width,player.Height)
this.speedY = this.speedY * -1
endif
endfunc
return ball
endfunc
function get_ball()
return ball
endfunc
Code:
'Miscellaneous
'variables y funciones para manejar los puntos
visible score_player = 0
visible score_player_cpu = 0
function get_score_player()
return score_player
endfunc
function get_score_player_cpu()
return score_player_cpu
endfunc
'funcion de colision--------------------------------
function collision_rect(x1,y1,w1,h1,x2,y2,w2,h2)
return x1 + w1 > x2 and x1 < x2 + w2 and
y1 + h1 > y2 and y1 < y2 + h2
endfunc