forum.boolean.name

forum.boolean.name (http://forum.boolean.name/index.php)
-   MidletPascal (http://forum.boolean.name/forumdisplay.php?f=46)
-   -   Keypressing (http://forum.boolean.name/showthread.php?t=13719)

NastyKhan 14.11.2010 03:01

Keypressing
 
Hello,

I'd like to make simple game, when an Object (starship, car, rocket etc.) is controlled by LEFT and RIGHT buttons. When you hold LEFT it strafe left, when you hold RIGHT, it strafe right, when you release buttons - it stops moving. Like any common shooter.


<------ /[]\ ------>


I tried something like this:


repeat;
drawimage(ship,x,0);
repaint;
delay(10);
key:=getkeypressed;
if (keyToAction(key)=GA_LEFT) then x:=x-10;
if (keyToAction(key)=GA_RIGHT) then x:=x+10;

until false;

It WORKS when I'm pressing ONE button only. But doesn't when I press both at same time. I'll try to explain this:

1. When I press (and hold) LEFT: CPU thinks i'm holding LEFT, and is doing his job.

[left]
<----/[]\

2. Then, when I press (and hold) RIGHT, while still holding LEFT pressed: CPU now thinks i'm holding RIGHT.

[left][right]
/[]\---->

3a. When I release RIGHT button, while still holding LEFT pressed: CPU thinks RIGHT has been released, so in his opinion all buttons has been released and so he does nothing.

[left]
/[]\

3b. Also when I release LEFT button, while still holding RIGHT pressed: CPU thinks that all buttons has been released and so he does nothing.

[right]
/[]\


I hope i made myself clear :P


My questions:
1. How to fix it?
2. Does anybody could give me working "spacecraft movement" procedure, please?

Thanks

ViNT 14.11.2010 03:19

Ответ: Keypressing
 
This action can't be handled by standard MP functions, because they can't determine current button state, they only handle press/release events.

You should try to use get_key_states function from Lib_game, this possible can handle multiple key pressing.

NastyKhan 14.11.2010 03:35

Ответ: Keypressing
 
Hmm.. i was also thinking about it. However I don't know how to use lib_game, and other 'non-standard things'.. any clues with that?

edit:

I downloaded and installed 'game' library into my "..\MidletPascal 3.1\Libs\" folder. Then i added "uses game;" line to my game. Finally i changed the code:


repeat;
drawimage(ship,x,0);
repaint;
delay(10);
key := game.get_key_states;
if (game.get_bit(key, 4)<>0) then x:=x-10;
if (game.get_bit(key, 32)<>0) then x:=x+10;

until false;

Compliation and buliding worked fine. The '*.jar' works. However nothing happens. I think the games freezes from some reason.. But dont know why.

edit2:
I realised than i'm missing "game.init", and "game.show" thingies. However when i put them, i get white blank screen. What are they for anyway?

Please help..

NastyKhan 14.11.2010 05:02

Ответ: Keypressing
 
I searched the forum, and i found this:


Program test;

uses
game;

const
UP_PRESSED = 2;
DOWN_PRESSED = 64;
LEFT_PRESSED = 4;
RIGHT_PRESSED = 32;
FIRE_PRESSED = 256;
GAME_A_PRESSED = 512;
GAME_B_PRESSED = 1024;
GAME_C_PRESSED = 2048;
GAME_D_PRESSED = 4096;

var
img: image;
key: integer;
x,y: integer;

procedure cls;
begin
setcolor(255,255,255);
fillrect(0,0,getwidth,getheight);
end;

procedure init;
begin
img:=loadimage('/icon.png');
x:=20;
y:=20;
game.init;
game.show;
end;

Begin
init;
repeat
key:=game.get_key_states;
if game.get_bit(key, LEFT_PRESSED)<>0 then
x:=x-5;
if game.get_bit(key, RIGHT_PRESSED)<>0 then
x:=x+5;
if game.get_bit(key, UP_PRESSED)<>0 then
y:=y-5;
if game.get_bit(key, DOWN_PRESSED)<>0 then
y:=y+5;
cls;
drawimage(img,x,y);
game.refresh;
delay(50);
until game.get_bit(key, FIRE_PRESSED)<>0;
End.



That's exactly what i need. I compiled it, but i when i run *.jar, i get blank white screen again!

Please help..

Trazzy 14.11.2010 07:29

Ответ: Keypressing
 
u can try to change game.init; -> game.init(1); :)

ViNT 14.11.2010 08:30

Ответ: Keypressing
 
Try to compile this in MP 2.0.2 version.

NastyKhan 14.11.2010 16:53

Ответ: Keypressing
 
Using MP 2.0 and changing game.init; to game.init(1); didn't work.

In fact, when i keep game.init; it doesn't compile at all.


Still need help : (


edit:
Maybe i have the library wrongly installed. All i have to do is to copy it into "..\MidletPascal 3.1\Libs\" folder?

Also what [lib_name].init and [lib_name].show commands do?

ViNT 14.11.2010 21:29

Ответ: Keypressing
 
Use MP 2.0.2 version, not MP 2.0.
I compile example from post #4 and it perfectly works.

Trazzy 15.11.2010 00:28

Ответ: Keypressing
 
Вложений: 2
Use Midlet Pascal 2.02
Copy Lib_game.class into "..\MIDletPascal\Libs\"
It works!.

NastyKhan 17.11.2010 20:02

Ответ: Keypressing
 
Thanks :) It works now.


Часовой пояс GMT +4, время: 11:36.

vBulletin® Version 3.6.5.
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Перевод: zCarot