Home  |  Contents 

Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: INKEY$ and multiple keypresses Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
karjo238
Newbie
Newbie


Joined: 12 October 2018
Location: New Zealand
Online Status: Offline
Posts: 2
Posted: 12 October 2018 at 1:45pm | IP Logged Quote karjo238

Hey there! My name is Joseph Karl, and after finding out about Geoff Grahams Colour Maximite, I'm reliving a wave of nostalgia, programming in BASIC the way I used to many, many years ago

I have cut my teeth by making Super Star Trek work, which wasn't actually much work once you added spaces where necessary, and a real blast to play again.

From there, I decided to boldly go into something of my own (sort of), which is a version of the Atari 2600 game Missile Command.

I have done some work so far, and have had a great time making what I have done function, but I've come to an impasse.

So, I'm in the business of finding help. I stumbled upon this forum, and thought, "Hey, this looks like the place!"

My problem is that the game uses the arrow keys and the spacebar to move the cursor and fire the gun, but if I'm holding down more than one key, things don't function the way I expected. If I hold down the spacebar and then try and move the cursor, I can't. if I hold down the cursor keys and then press the spacebar, things work how I'd expect.

I'm a newbie to this version of BASIC, and I'm probably doing something silly, but right now, I don't know what that is.

This is the code. It needs cleaning up, I know, but it does mostly function.

If anybody has any ideas, this somewhat clumsy coder would be glad to hear of it.

Many thanks,

Joseph :)


Sub init
  x1 = 0
  y1 = 0
  x2 = 0
  y2 = 0
  sx = 240
  sy = 400
  outerRad = 30
  innerRad = 20
  adjac = 0
  oppos = 0
  hypo = 0
  aimX = 200
  aimY = 100
  Dim shell1(5,5)

  LoadBMP"backg.bmp"

  Sprite Load "cross.spr"
  Sprite on 1,aimX,aimY

  mathstuff

  getpoints(angle, innerRad, outerRad)
  Line(x1,y1) - (x2,y2),7
  main
End Sub

Sub fireshell
  q = 0
  doneflag = false
  Pixel(x2,y2) = 0
  Do
    If shell1(q,0) = 0 Then
      doneflag = true
      shell1(q,0) = x2
      shell1(q,1) = y2
      shell1(q,2) = aimX + 7
      shell1(q,3) = aimY + 7
      shell1(q,4) = x2
    Else
      q = q + 1
    EndIf
  Loop Until doneflag = true
  drawpoint
End Sub

Sub drawpoint
  For q = 0 To 4
    If shell1(q,0) <> 0 Then
      x = shell1(q,4)
      Rem dx = endX - x2
      dx = shell1(q,2) - shell1(q,0)
      Rem dy = endY - y2
      dy = shell1(q,3) - shell1(q,1)

      y = shell1(q,1) + dy * (x - shell1(q,0)) / dx
      If y >= shell1(q,3) Then
        Pixel(x,y) = 7
        If shell1(q,2) < shell1(q,0) Then
          shell1(q,4) = shell1(q,4) - 1
        Else
          shell1(q,4) = shell1(q,4) + 1
        EndIf

        Pixel(x,y) = 0
      Else
        For j = 0 To 4:shell1(q,j) = 0:Next j
      EndIf
    EndIf
  Next q

End Sub

Sub getPoints (a,r,r1)
  x1 = sx + (r * Cos(a * Pi / 180))
  y1 = sy + (r * Sin(a * Pi / 180))
  x2 = sx + (r1 * Cos(a * Pi / 180))
  y2 = sy + (r1 * Sin(a * Pi / 180))
End Sub

Sub mathstuff
  oppos = sy - aimY
  adjac = Abs(sx - aimX)
  If adjac = 0 Then
    angle = 270
  Else
    hypo = Sqr((oppos * oppos) + (adjac * adjac))
    If aimX - sX > 0 Then
      angle = 360 - arcsin(oppos / hypo) * (180 / Pi)
    Else
      angle = 180 + arcsin(oppos / hypo) * (180 / Pi)
    EndIf
  EndIf
End Sub

Function arcsin(x)
  arcsin = Atn(x/Sqr(-x*x+1))
End Function

Sub clearStuff
  Sprite off 1
  Line(x1,y1) - (x2,y2),0
End Sub

Sub drawStuff
  mathstuff
  getpoints(angle, innerRad, outerRad)
  Sprite on 1, aimX, aimY
  Line(x1,y1) - (x2,y2),7
End Sub

Sub main

  Do While 1 = 1

    a = KeyDown

    If a = 128 Then
      clearStuff
      aimY = aimY - 1
      drawStuff
      drawpoint
    EndIf

    If a = 129 Then
      clearStuff
      aimY = aimY + 1
      drawStuff
      drawpoint
    EndIf

    If a = 130 Then
      clearStuff
      aimX = aimX - 1
      drawStuff
      drawpoint
    EndIf

    If a = 131 Then
      clearStuff
      aimX = aimX + 1
      drawStuff
      drawpoint
    EndIf

    If a = 32 Then
      fireshell
    EndIf

    For t = 1 To 500:Next t

  Loop


End Sub

init




Back to Top View karjo238's Profile Search for other posts by karjo238
 
TassyJim
Guru
Guru
Avatar

Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2673
Posted: 12 October 2018 at 4:47pm | IP Logged Quote TassyJim

Welcome to the forum.

I don't have a Maximite setup for testing.
KeyDown is not a keyword in the current versions of MMBasic so it is difficult to test on a micromite (the Maximite successor).
It also would depend on the keyboard routines so could not be easily tested using a terminal emulator.

You could try
a = asc(inkey$)

and see if that is usable instead.

Jim


__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
CaptainBoing
Guru
Guru
Avatar

Joined: 07 September 2016
Location: United Kingdom
Online Status: Offline
Posts: 626
Posted: 12 October 2018 at 7:25pm | IP Logged Quote CaptainBoing

Hey Joseph - you OK?

A few questions:
I haven't played with the MaxiMite, are you using a PS2 keyboard plugged directly in?
Is KeyDown a Maximite (MMBasic) statement or a Sub you have written?

With scanning a keyboard, if you have access to the hardware then you will be able to check for multiple (and multiple) keys pressed. If you are using a console connection then no such luck. If you hold a key down until it repeats then simultaneously hold another key down, the first input is abandoned in favour of the most recent. You can see this if you open an editor and hold a key down, then hold two keys down etc... thus you'll only ever see a single key with INKEY$

With the hardware in a keyboard, the state of the key up/down is sent by the keyboard controller (which may well be the system cpu) and the driver for it (which is a subsystem in the firmware) maintains a table of all the key number values. This gets updated as key states come in from the keyboard electronics and any tests for key states from (say your program) only ever has to go to the table (you don't actually do this, the system call does). You don't get this with a console connection because you are working through the driver on your PC etc which converts key presses to ASCII values - it is easier for most things to get the actual character of the key you pressed. The driver for the keyboard hides all this tedious state checking and stuff from you so your program can generally get on with the things you want to do... it can get in the way if you don't want that though. I think back to the days of my Amstrad 6128, you had INKEY$ which would fetch the character of he current key in the buffer (or "") and you also had INKEY(n) - note no $ - where n was the key number (in hardware) and the value returned would tell you if that key was up or down. All keys had a hardware number and could be checked - even keys that didn't produce any character - like CTRL and SHIFT. http://www.cpcwiki.eu/index.php/Locomotive_BASIC#INKEY_.28i.29. Some basics e.g. QBasic had an ON KEY(n) GOSUB event that worked by the key number. https://www.qbasic.net/en/reference/qb11/Statement/ON-KEY.htm

So in short - if you have access to the key states coming from the keyboard electronics, you should be able to detect the state of individual keys. If you don't you might be stuck.





Edited by CaptainBoing on 12 October 2018 at 8:08pm
Back to Top View CaptainBoing's Profile Search for other posts by CaptainBoing
 
twofingers
Guru
Guru


Joined: 02 June 2014
Location: Germany
Online Status: Offline
Posts: 594
Posted: 12 October 2018 at 8:13pm | IP Logged Quote twofingers

Hi Joseph,

some idea (I did not test that):
I would try to replace the SPACE with CTRL keypresses.
(And maybe the CURSOR keys with w-a-s-z or similar if needed)

Please take a look at the MMBasic manual for Maximites Appendix F
Special Keyboard Keys.
Quote:
If the control key is simultaneously pressed then 20 (hex) is added to the code (this is the equivalent of setting
bit 5). If the shift key is simultaneously pressed then 40 (hex) is added to the code (this is the equivalent of
setting bit 6). If both are pressed 60 (hex) is added. For example Control-PageDown will generate A9 (hex).
The shift modifier only works with the function keys F1 to F12; it is ignored for the other keys.


Normaly MMBasic interprets the keypresses as a serial stream. What you need - I think - is somethin more paralell working.

Kind regards
Michael

EDIT: I had to edit for english keyboard layout.


Edited by twofingers on 12 October 2018 at 8:25pm
Back to Top View twofingers's Profile Search for other posts by twofingers
 
CircuitGizmos
Guru
Guru
Avatar

Joined: 08 September 2011
Location: United States
Online Status: Offline
Posts: 1046
Posted: 13 October 2018 at 2:20am | IP Logged Quote CircuitGizmos

Why not wire in your own buttons? You could use arcade buttons on the Maximite port and test for any combination of left, right, up, down, and fire states.

__________________
Micromites and Maximites! - Beginning Maximite
Back to Top View CircuitGizmos's Profile Search for other posts by CircuitGizmos Visit CircuitGizmos's Homepage
 
TassyJim
Guru
Guru
Avatar

Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2673
Posted: 13 October 2018 at 6:45am | IP Logged Quote TassyJim

CaptainBoing wrote:

Is KeyDown a Maximite (MMBasic) statement or a Sub you have written?

KEYDOWN is a MMBasic function introduced in the Maximite to make game-playing easier. For rapid fire games, it is better than INKEY.

The micromites were not considered a game platform so KEYDOWN was dropped, along with a lot of the graphic functions.

Detecting multiple consecutive key-presses is going to be difficult. Using CTRL instead of SPACEBAR would work, but not when the CTRL/SPACE is the only key pressed.

Having a game-pad with a few buttons wired up would be the most likely way to success.

I am sure that someone would have done that in the past.

Jim

__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
karjo238
Newbie
Newbie


Joined: 12 October 2018
Location: New Zealand
Online Status: Offline
Posts: 2
Posted: 14 October 2018 at 11:14am | IP Logged Quote karjo238

TassyJim wrote:


Having a game-pad with a few buttons wired up would be the most likely way to success.

I am sure that someone would have done that in the past.

Jim


Hey guys. Sorry for the delay in replying. Life(TM) gets in the way occasionally.

The gamepad idea, now that you mention it, sounds like a great one. It would help lend an air of authenticity to what I'm trying to do here, so I'll have a hunt around and see what's available here in NZ, and see how difficult it is to hook up to the Maximite.

Many thanks for your help thus far (I'm sure I'll have more questions in the future)

Joseph
Back to Top View karjo238's Profile Search for other posts by karjo238
 
WhiteWizzard
Guru
Guru


Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2681
Posted: 14 October 2018 at 5:17pm | IP Logged Quote WhiteWizzard

Hi Joseph,

Welcome to the forum - and great that someone else is needing this feature.

Even though it may not be what you are asking about; the way I overcame the issue (for 'GamePlay') was to use a Nintendo NunChuk. This is an I2C device so is easy to interface to via software, and gives a very nice direction/fire/jump type controller at a low cost.

You can get 'Break-Out-Boards' that have the 6-pin connector shaped into a PCB with 0.1" headers. Google will help you locate availability for you; OR alternatively, you could cut off the end connector, and hard-wire the four relevant wires (two for power, and two for I2C).

Rgds,
WW



__________________
For everything MicroMite visit MicroMite.org

Direct Email: WhiteWizzard@MicroMite.org
Back to Top View WhiteWizzard's Profile Search for other posts by WhiteWizzard Visit WhiteWizzard's Homepage
 
twofingers
Guru
Guru


Joined: 02 June 2014
Location: Germany
Online Status: Offline
Posts: 594
Posted: 14 October 2018 at 9:16pm | IP Logged Quote twofingers

There is a Pacman clone (MAXMAN) in the MMBasic Library that maybe can be helpful.

A NunChuk (clone from china at ebay) costs about 3.60 US$.
Back to Top View twofingers's Profile Search for other posts by twofingers
 


If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Search the forums using Google.
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1104 seconds.
Privacy Policy     Process times : 0, 0, 0, 0.11