01-27-2024, 04:46 PM
I share all the code of my first simple game, naalaa pong.
The game starts with the title screen where you have to press return and wait for the countdown to finish, then play until one of you gets 10 points, and the letter "p" to pause the game.
At the top of each file the name of each file is written in a comment, everything is done with the resources that the naalaa language brings, there is nothing external.
The game starts with the title screen where you have to press return and wait for the countdown to finish, then play until one of you gets 10 points, and the letter "p" to pause the game.
At the top of each file the name of each file is written in a comment, everything is done with the resources that the naalaa language brings, there is nothing external.
Code:
'pong
include "player.n7"
include "player-cpu.n7"
include "ball.n7"
include "miscellaneous.n7"
set window "example pong",640,480,false
set redraw off
'instancias y variables---------------------------
Font = createfont("Consolas",64)
set font Font
visible state = 0
visible timer = 5
player = []
player_cpu = []
ball = []
while not keydown(KEY_ESCAPE,true)
set color 0,0,0
cls
'objetos-----------------------------
select state
case 0
'iniciar objetos y variables----------
timer = 5
set_score_player(0)
set_score_player_cpu(0)
player = Player()
player_cpu = Player_Cpu()
ball = Ball()
'escena introducion------------------
set color 255,0,204
set caret 320,100
center "NAALAA PONG"
set color 255,204,0
set caret 320,250
center "start"
'si pulsas return me mandas al estado 5
if keydown(KEY_RETURN,true)
state = 5
endif
case 5
'escena introducion parte 2---------------
set color 255,0,204
set caret 320,100
center "NAALAA PONG"
set color 255,204,0
set caret 320,250
center timer
'si llega a 0 me mandas al estado 10
timer = timer - 1
if timer < -1
state = 10
endif
wait(1000)
case 10
'juego------------------------------
player.update()
player.Draw()
player_cpu.update(ball)
player_cpu.Draw()
ball.update(player,player_cpu)
ball.Draw()
'puntuacion player-----------------------
set color 255,0,204
set caret 162,32
wln(get_score_player())
'puntuacion player cpu--------------------
set color 255,204,0
set caret 448,32
wln(get_score_player_cpu())
'lineas para el fondo------------------
set color 255,255,255
'linea en y
draw line 315,32,315,448
draw line 325,32,325,448
'linea en x arriba
draw line 32,22,608,22
draw line 32,32,608,32
'linea en x abajo
draw line 32,448,608,448
draw line 32,458,608,458
'mandar a la pantalla de game over-------------
if get_score_player() >= 10 or get_score_player_cpu() >= 10
state = 20
endif
case 20
'eliminar objetos y pantalla game over------------
player.live = false
player_cpu.live = false
ball.live = false
set color 255,255,255
set caret 320,200
center "GAME OVER"
'esperar 1 segundo y me mandas al estado 25
wait(1000)
timer = 5
state = 25
case 25
'pantalla game over parte 2-----------------------
set color 255,255,255
set caret 320,200
center "GAME OVER"
set color 255,204,0
set caret 320,300
center timer
'si llega a 0 me mandas al estado 0
timer = timer - 1
if timer < -1
state = 0
endif
wait(1000)
endsel
'pausar el juego-----------------------
if state = 10 and keydown(KEY_P,true)
set color 50,200,200
set caret 320,200
center "PAUSE"
while not keydown(KEY_P,true)
wait(10)
redraw
wend
endif
redraw
fwait 60
wend
Code:
'player
function Player()
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,204
draw rect this.x,this.y,this.Width,this.Height,true
endif
endfunc
player.move = function()
if keydown(KEY_UP,false) and this.y > 0
this.y = this.y - this.speed
elseif keydown(KEY_DOWN,false) and this.y < 480-this.Height
this.y = this.y + this.speed
endif
endfunc
return player
endfunc
Code:
'player-cpu
function Player_Cpu()
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(ball)
if this.live
this.move(ball)
endif
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
player_cpu.move = function(ball)
if ball.x > 300
if this.y > ball.y and this.y > 0
this.y = this.y - (this.speed+ball.speedX)
endif
if this.y < ball.y and this.y < 480-this.Height
this.y = this.y + (this.speed+ball.speedX)
endif
endif
endfunc
return player_cpu
endfunc
Code:
'ball
include "miscellaneous.n7"
visible sound_bounce = CreateSineSfx(0.1,125,100,0.9,11025)
visible sound_over = CreateSineSfx(0.2,1150,600,0.9,11025)
function Ball()
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.state = 0
ball.count = 0
ball.update = function(player,player_cpu)
if this.live = true
select this.state
case 0
this.x = 320 - this.Width / 2
this.y = 240 - this.Height / 2
this.speedX = 2
this.speedY = 2
this.count = this.count + 1
if this.count >= 120
this.count = 0
this.state = 1
endif
case 1
this.move()
this.bounce()
this.collision_player(player)
this.collision_player_cpu(player_cpu)
this.score()
endsel
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
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
play sound sound_over,0.5
this.state = 0
wait(1000)
endif
if this.y <= 0 or this.y >= 480
this.speedY = this.speedY * -1
play sound sound_bounce,1
endif
endfunc
ball.collision_player = function(player)
' Marcus.
' Check if they overlap.
if collision_rect(this.x,this.y,this.Width,this.Height,player.x,player.y,player.Width,player.Height)
play sound sound_bounce,1
' Calculate delta x and delta y, between the center of the ball and the center of the
' paddle. Divide the delta x value width the width of the paddle and delta y with the
' height of the paddle to normalize them (make comparison valid).
' Calcular delta x y delta y, entre el centro de la pelota y el centro de la
' remo. Divida el valor delta x ancho el ancho de la paleta y delta y con el
' altura de la paleta para normalizarlos (hacer válida la comparación).
dx = (this.x + this.Width/2 - (player.x + player.Width/2))/player.Width
dy = (this.y + this.Height/2 - (player.y + player.Height/2))/player.Height
' If dx is higher, it means that there's less overlapping along the x-axis. In that
' case bounce left or right.
' Si dx es mayor, significa que hay menos superposición a lo largo del eje x. En eso
' el caso rebota hacia la izquierda o hacia la derecha.
if |dx| >= |dy|
' dx < 0, ball should bounce to the left, and we also move the ball to the left so
' that there's no longer any collision.
' dx < 0, la pelota debe rebotar hacia la izquierda, y también la movemos hacia la izquierda para que
' que ya no hay ninguna colisión.
if dx < 0
this.speedX = -|this.speedX|
this.x = player.x - this.Width
' dx > 0, ball should bounce to the right, and move the ball to the right of the
' paddle.
' dx > 0, la pelota debe rebotar hacia la derecha y moverse hacia la derecha del
' remo.
else
this.speedX = |this.speedX|
this.x = player.x + player.Width
endif
' dy is higher, same principle as for dx :)
' dy es mayor, el mismo principio que para dx
else
if dy < 0
this.speedY = -|this.speedY|
this.y = player.y - this.Height
else
this.speedY = |this.speedY|
this.y = player.y + player.Height
endif
endif
endif
endfunc
ball.collision_player_cpu = function(player_cpu)
' Marcus.
' Check if they overlap.
if collision_rect(this.x,this.y,this.Width,this.Height,player_cpu.x,player_cpu.y,player_cpu.Width,player_cpu.Height)
'sonido
play sound sound_bounce,1
'aumentar velocidad
this.increase_speed()
dx = (this.x + this.Width/2 - (player_cpu.x + player_cpu.Width/2))/player_cpu.Width
dy = (this.y + this.Height/2 - (player_cpu.y + player_cpu.Height/2))/player_cpu.Height
if |dx| >= |dy|
if dx < 0
this.speedX = -|this.speedX|
this.x = player_cpu.x - this.Width
else
this.speedX = |this.speedX|
this.x = player_cpu.x + player_cpu.Width
endif
else
if dy < 0
this.speedY = -|this.speedY|
this.y = player_cpu.y - this.Height
else
this.speedY = |this.speedY|
this.y = player_cpu.y + player_cpu.Height
endif
endif
endif
endfunc
ball.score = function()
if this.x <= 0
add_score_player_cpu(1)
endif
if this.x >= 640
add_score_player(1)
endif
endfunc
ball.increase_speed = function()
'aumentar velocidad al colisionar
this.speedX = this.speedX + 1
if this.speedX >= 10
this.speedX = 10
endif
endfunc
return ball
endfunc
Code:
'miscellaneous
'variables y funciones para manejar los puntos
'para el player---------------------
visible score_player = 0
function set_score_player(number)
score_player = number
endfunc
function add_score_player(number)
score_player = score_player + number
endfunc
function get_score_player()
return score_player
endfunc
'para el player cpu-------------------
visible score_player_cpu = 0
function set_score_player_cpu(number)
score_player_cpu = number
endfunc
function add_score_player_cpu(number)
score_player_cpu = score_player_cpu + number
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
'funcion para el sonido------------------------------
function CreateSineSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
data = []
a = 0
da = 2*PI*startFreq/sampleRate
dda = (2*PI*endFreq/sampleRate - 2*PI*startFreq/sampleRate)/(duration*sampleRate)
vol = 1
fadeOut = fadeOut*duration*sampleRate
fadeOutDelta = 1/(duration*sampleRate - fadeOut)
for i = 0 to duration*sampleRate - 1
data[i] = sin(a)*vol
a = a + da
da = da + dda
if i > fadeOut vol = vol - fadeOutDelta
next
return createsound(data, data, sampleRate)
endfunc