Rojart,
I´ve used your suggestion and it works perfectly now... thank you!
But my games still run very slow... I don´t know if it´s me doing something wrong or maybe something else... My games are all panel based...
Here is the code for my pong clone... it´s very easy so I don´t mind if somebody reading it use it... it should be very fast because it is a simple game...
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////
BMAP* campo_bmp = "campo.png";
BMAP* puck_bmp = "puck.bmp";
BMAP* raquete_bmp = "raquete.bmp";
BMAP* vitoria_bmp = "vitoria.bmp";
#define PRAGMA_BIND "campo.png";
#define PRAGMA_BIND "puck.bmp";
#define PRAGMA_BIND "raquete.bmp";
#define PRAGMA_BIND "vitoria.bmp";
PANEL* vitoria_pan =
{
pos_x = 260;
pos_y = 100;
layer = 5;
bmap = vitoria_bmp;
}
PANEL* puck_pan =
{
pos_x = 385;
pos_y = 285;
layer = 2;
bmap = puck_bmp;
flags = OVERLAY | VISIBLE;
}
PANEL* r1_pan =
{
pos_x = 60;
pos_y = 270;
layer = 1;
bmap = raquete_bmp;
flags = OVERLAY | VISIBLE;
}
PANEL* r2_pan =
{
pos_x = 715;
pos_y = 270;
layer = 1;
bmap = raquete_bmp;
flags = OVERLAY | VISIBLE;
}
PANEL* campo_pan =
{
pos_x = 0;
pos_y = 0;
layer = 0;
bmap = campo_bmp;
flags = VISIBLE;
}
STRING* jogoL = "SUPERPONG";
STRING* golsjogador1L = "JOGADOR 1 ";
STRING* golsjogador2L = "JOGADOR 2 ";
STRING* jogo = "PLACAR";
STRING* golsjogador1 = " ";
STRING* golsjogador2 = " ";
STRING* vitoria1 = "PARABENS!";
STRING* vitoria2 = "VOCE VENCEU,";
TEXT* vitoria1_txt =
{
pos_x = 270;
pos_y = 370;
layer = 6;
string (vitoria1, vitoria2, golsjogador1L);
}
TEXT* vitoria2_txt =
{
pos_x = 270;
pos_y = 370;
layer = 6;
string (vitoria1, vitoria2, golsjogador2L);
}
TEXT* placarlabel_txt =
{
pos_x = 240;
pos_y = 570;
layer = 3;
string (jogoL, golsjogador1L, golsjogador2L);
flags = VISIBLE;
}
TEXT* placar_txt =
{
pos_x = 340;
pos_y = 570;
layer = 3;
string (jogo, golsjogador1, golsjogador2);
flags = VISIBLE;
}
/////////////////////
//VARIAVEIS GLOBAIS//
/////////////////////
var direcao_puck = 6; //puck orientation
var velocidade_puck = 1; //puck speed
var gols_jogador1 = 0; //player 1 points
var gols_jogador2 = 0; //player 2 points
var posicao_relativa; //relative position
function atualizaPosicaoRelativa() //update relative position
{
if (puck_pan.pos_x < r1_pan.pos_x) posicao_relativa = 1;
else if (puck_pan.pos_x > r2_pan.pos_x) posicao_relativa = 2;
else posicao_relativa = 0;
}
function verificaLimites() //check boundaries
{
//raquete 1
if (r1_pan.pos_x <= 1) r1_pan.pos_x = 1;
if (r1_pan.pos_x >= 374) r1_pan.pos_x = 374;
if (r1_pan.pos_y <= 1) r1_pan.pos_y = 1;
if (r1_pan.pos_y >= 539) r1_pan.pos_y = 539;
//raquete 2
if (r2_pan.pos_x >= 774) r2_pan.pos_x = 774;
if (r2_pan.pos_x <= 400) r2_pan.pos_x = 400;
if (r2_pan.pos_y <= 1) r2_pan.pos_y = 1;
if (r2_pan.pos_y >= 539) r2_pan.pos_y = 539;
}
function mudarDirecao(var direx) //change direction
{
direcao_puck = direx;
}
function movePuck() //move puck
{
if (direcao_puck == 1) puck_pan.pos_x -= velocidade_puck; //esquerda - left
if (direcao_puck == 2) puck_pan.pos_x += velocidade_puck; //direita - right
if (direcao_puck == 3) puck_pan.pos_y -= velocidade_puck; //norte - north
if (direcao_puck == 4) puck_pan.pos_y += velocidade_puck; //sul - south
if (direcao_puck == 5) //nordeste - northeast
{
puck_pan.pos_x += (velocidade_puck/2);
puck_pan.pos_y -= (velocidade_puck/2);
}
if (direcao_puck == 6) //sudeste - southeast
{
puck_pan.pos_x += (velocidade_puck/2);
puck_pan.pos_y += (velocidade_puck/2);
}
if (direcao_puck == 7) //sudoeste - southwest
{
puck_pan.pos_x -= (velocidade_puck/2);
puck_pan.pos_y += (velocidade_puck/2);
}
if (direcao_puck == 8) //noroeste - northwest
{
puck_pan.pos_x -= (velocidade_puck/2);
puck_pan.pos_y -= (velocidade_puck/2);
}
if (direcao_puck == 0) { } //sem movimento - no movement
}
function controlaPontos() //update points
{
if (puck_pan.pos_x == 0 && (puck_pan.pos_y >= 200 && puck_pan.pos_y < 370))
{
gols_jogador2 += 1;
media_play("alarm1.wav", NULL, 100);
str_for_num(golsjogador2, gols_jogador2);
}
if (puck_pan.pos_x == 770 && (puck_pan.pos_y >= 200 && puck_pan.pos_y < 370))
{
gols_jogador1 += 1;
media_play("alarm1.wav", NULL, 100);
str_for_num(golsjogador1, gols_jogador1);
}
}
function colisaoPuck() //puck colision detection
{
var temp_dir;
if (puck_pan.pos_x <= r1_pan.pos_x + 20 && puck_pan.pos_x + 30 >= r1_pan.pos_x &&
(puck_pan.pos_y >= r1_pan.pos_y - 20 && puck_pan.pos_y <= r1_pan.pos_y + 80))
{
if (posicao_relativa == 0)
{
if (direcao_puck == 1) mudarDirecao(2);
if (direcao_puck == 7) mudarDirecao(6);
if (direcao_puck == 8) mudarDirecao(5);
media_play("ploop.wav", NULL, 100);
}
if (posicao_relativa == 1)
{
if (direcao_puck == 2) mudarDirecao(1);
if (direcao_puck == 6) mudarDirecao(7);
if (direcao_puck == 5) mudarDirecao(8);
media_play("ploop.wav", NULL, 100);
}
}
if (puck_pan.pos_x + 30 >= r2_pan.pos_x && puck_pan.pos_x <= r2_pan.pos_x + 20 &&
(puck_pan.pos_y >= r2_pan.pos_y - 20 && puck_pan.pos_y <= r2_pan.pos_y + 80))
{
if (posicao_relativa == 0)
{
if (direcao_puck == 2) mudarDirecao(1);
if (direcao_puck == 6) mudarDirecao(7);
if (direcao_puck == 5) mudarDirecao(8);
media_play("ploop.wav", NULL, 100);
}
if (posicao_relativa == 2)
{
if (direcao_puck == 1) mudarDirecao(2);
if (direcao_puck == 7) mudarDirecao(6);
if (direcao_puck == 8) mudarDirecao(5);
media_play("ploop.wav", NULL, 100);
}
}
if (puck_pan.pos_x <= 0)
{
if (direcao_puck == 1) mudarDirecao(2);
if (direcao_puck == 7) mudarDirecao(6);
if (direcao_puck == 8) mudarDirecao(5);
media_play("ploop.wav", NULL, 100);
}
if (puck_pan.pos_y <= 0)
{
if (direcao_puck == 3) mudarDirecao(4);
if (direcao_puck == 5) mudarDirecao(6);
if (direcao_puck == 8) mudarDirecao(7);
media_play("ploop.wav", NULL, 100);
}
if (puck_pan.pos_y >= 570)
{
if (direcao_puck == 4) mudarDirecao(3);
if (direcao_puck == 6) mudarDirecao(5);
if (direcao_puck == 7) mudarDirecao(8);
media_play("ploop.wav", NULL, 100);
}
if (puck_pan.pos_x >= 770)
{
if (direcao_puck == 2) mudarDirecao(1);
if (direcao_puck == 6) mudarDirecao(7);
if (direcao_puck == 5) mudarDirecao(8);
media_play("ploop.wav", NULL, 100);
}
}
function moveRaquete() //move players
{
if (key_w) r1_pan.pos_y -= 0.3;
if (key_s) r1_pan.pos_y += 0.3;
if (key_a) r1_pan.pos_x -= 0.3;
if (key_d) r1_pan.pos_x += 0.3;
if (key_cuu) r2_pan.pos_y -= 0.3;
if (key_cud) r2_pan.pos_y += 0.3;
if (key_cul) r2_pan.pos_x -= 0.3;
if (key_cur) r2_pan.pos_x += 0.3;
}
function verificaFinal() //check for a winner
{
if (gols_jogador1 == 5)
{
direcao_puck = 0;
media_play("crowdcheer.wav", NULL, 100);
vitoria_pan.flags = OVERLAY | VISIBLE;
vitoria1_txt.flags = OVERLAY | VISIBLE;
}
if (gols_jogador2 == 5)
{
direcao_puck = 0;
media_play("crowdcheer.wav", NULL, 100);
vitoria_pan.flags = OVERLAY | VISIBLE;
vitoria2_txt.flags = OVERLAY | VISIBLE;
}
}
function main()
{
video_mode = 7;
random_seed(1);
while(1)
{
verificaFinal();
atualizaPosicaoRelativa();
colisaoPuck();
controlaPontos();
movePuck();
moveRaquete();
verificaLimites();
wait(1);
}
}