Welcome, Guest |
You have to register before you can post on our site.
|
Latest Threads |
MBA Assignment Help in Du...
Forum: General Discussion
Last Post: hafsahomar
Yesterday, 07:05 AM
» Replies: 0
» Views: 1
|
лучшие песни медляки слуш...
Forum: Petr
Last Post: WillieTop
06-08-2025, 02:21 AM
» Replies: 0
» Views: 20
|
пинк слушать онлайн беспл...
Forum: SMcNeill
Last Post: WillieTop
06-08-2025, 02:20 AM
» Replies: 0
» Views: 18
|
скачать музыку российскую...
Forum: madscijr
Last Post: WillieTop
06-08-2025, 02:18 AM
» Replies: 0
» Views: 19
|
нежная музыка mp3 скачать
Forum: Keybone
Last Post: WillieTop
06-08-2025, 02:17 AM
» Replies: 0
» Views: 17
|
лучшая песня слушать онла...
Forum: bplus
Last Post: WillieTop
06-08-2025, 02:16 AM
» Replies: 0
» Views: 18
|
пикник слушать онлайн луч...
Forum: Spriggsy
Last Post: WillieTop
06-08-2025, 02:15 AM
» Replies: 0
» Views: 20
|
какая сейчас популярная м...
Forum: RhoSigma
Last Post: WillieTop
06-08-2025, 02:14 AM
» Replies: 0
» Views: 17
|
хит лета 2019 музыка на т...
Forum: Christmas Code
Last Post: WillieTop
06-08-2025, 02:12 AM
» Replies: 0
» Views: 22
|
бесплатная музыка mp3 рег...
Forum: Works in Progress
Last Post: WillieTop
06-08-2025, 02:11 AM
» Replies: 0
» Views: 16
|
|
|
QBJS - Web Chat |
Posted by: dbox - 12-17-2022, 06:38 PM - Forum: QBJS, BAM, and Other BASICs
- Replies (7)
|
 |
You guys inspired me. I saw all of the recent chat server posts and wondered how hard it would be to create a web chat client in QBJS. For anyone interested you can try out the end result here:
QB Web Chat
The server is written in QB64. I used luke's simple HTTP server as a starting point and modified it to be an HTTP chat server. Here is the server code in case you want to play around with it:
Code: (Select All) ' QB Chat Server
' Author: dbox*
' *This originally started as luke's simple HTTP server.
' It has been modified to serve as an HTTP chat server.
' Here is the original source attribution:
' -------------------------------------------------------------------------------------
' HTTP 1.1 Compliant Web Server
' Author: luke
' Source: https://www.qb64.org/forum/index.php?topic=2052.0
' This program is made available for you to use, modify and distribute it as you wish,
' all under the condition you do not claim original authorship.
' -------------------------------------------------------------------------------------
$Console:Only
Option _Explicit
DefLng A-Z
Const MAX_CONNECTIONS = 8
Dim PORT As Integer: PORT = 8080
If _CommandCount > 0 Then
PORT = Val(Command$(1))
End If
Const FALSE = 0
Const TRUE = -1
Dim Shared CRLF As String
CRLF = Chr$(13) + Chr$(10)
Const HTTP_10 = 1
Const HTTP_11 = 11
Const HTTP_GET = 1
Const HTTP_HEAD = 2
Const HTTP_POST = 3
Type connection_t
handle As Long
read_buf As String
http_version As Integer
method As Integer
request_uri As String
content_length As Integer
End Type
Type http_error_t
code As Integer
message As String
connection As Integer
End Type
Type file_error_t
failed As Integer
code As Integer
End Type
Dim i
Dim num_active_connections
Dim server_handle
Dim Shared Connections(1 To MAX_CONNECTIONS) As connection_t
Dim Shared Http_error_info As http_error_t
Dim Shared File_error_info As file_error_t
'---------------------------------------------------------------------
' Chat server-specific initialization
'---------------------------------------------------------------------
Type Message
sender As String
message As String
time As String
End Type
Const MAX_MESSAGE = 100
Dim Shared messages(MAX_MESSAGE) As Message
Dim Shared cmsg As _Unsigned Long
cmsg = 0
Open "chatlog.txt" For Append As #1
'---------------------------------------------------------------------
On Error GoTo error_handler
server_handle = _OpenHost("TCP/IP:" + LTrim$(Str$(PORT)))
Print "Listening on port:" + Str$(PORT)
Do
If num_active_connections < MAX_CONNECTIONS Then
Dim new_connection
new_connection = _OpenConnection(server_handle)
If new_connection Then
num_active_connections = num_active_connections + 1
For i = 1 To MAX_CONNECTIONS
If Connections(i).handle = 0 Then
Dim empty_connection As connection_t
Connections(i) = empty_connection
Connections(i).handle = new_connection
num_active_connections = num_active_connections - 1
Exit For
End If
Next i
End If
End If
For i = 1 To MAX_CONNECTIONS
If Connections(i).handle Then
Dim buf$
Get #Connections(i).handle, , buf$
If buf$ <> "" Then
' This is a bit of a hack workaround.
' We have no gaurantee that the sent message will not be fragmented.
' So there's a chance the full message will not be sent all at once.
' Unfortunately, there's no way I know of to tell from the connection
' if there is more to read.
' Hence, we sleep for .05 seconds and try to read from the connection again.
' We'll repeat this as long as we keep reading content.
' Will this ensure we read the entire body content?
' No, but it seems to work for most cases.
While buf$ <> ""
Connections(i).read_buf = Connections(i).read_buf + buf$
_Delay .05
Get #Connections(i).handle, , buf$
Wend
process_request i
http_error_complete:
End If
End If
Next i
_Limit 240
Loop
error_handler:
If Err = 100 Then 'HTTP error
'Print "HTTP error"; Http_error_info.code; Http_error_info.message; " for connection"; Http_error_info.connection
Resume http_error_complete
End If
'Print "error"; Err; "on line"; _ErrorLine
End
file_error_handleyour:
File_error_info.failed = TRUE
File_error_info.code = Err
Resume Next
Sub http_send_status (c, code, message As String)
Dim s$
s$ = "HTTP/1.1" + Str$(code) + " " + message + CRLF
Put #Connections(c).handle, , s$
End Sub
Sub http_send_header (c, header As String, value As String)
Dim s$
s$ = header + ": " + value + CRLF
Put #Connections(c).handle, , s$
End Sub
Sub http_end_headers (c)
Put #Connections(c).handle, , CRLF
End Sub
Sub http_send_body (c, body As String)
Put #Connections(c).handle, , body
End Sub
Sub http_do_get (c)
http_do_post c
End Sub
Sub http_do_head (c)
Dim s$
s$ = "HTTP/1.1 200 OK" + CRLF + CRLF
Put #Connections(c).handle, , s$
End Sub
Sub http_do_post (c)
Dim As String action, body
action = Connections(c).request_uri
body = Connections(c).read_buf
Dim response As String
Select Case action
Case "/connect": response = HandleConnect(body)
Case "/disconnect": response = HandleDisconnect(body)
Case "/get": response = HandleGet(body)
Case "/send": response = HandleSend(body)
End Select
http_send_status c, 200, "OK"
http_send_header c, "Content-Type", "text/plain"
http_send_header c, "Content-Length", LTrim$(Str$(Len(response)))
http_send_header c, "Access-Control-Allow-Origin", "*"
http_send_header c, "Connection", "close"
http_end_headers c
http_send_body c, response
close_connection c
End Sub
Function HandleConnect$ (body As String)
AddMessage "system", body + " has entered the chat"
HandleConnect$ = Str$(cmsg - 1)
End Function
Function HandleDisconnect$ (body As String)
AddMessage "system", body + " has left the chat"
HandleDisconnect$ = Str$(cmsg - 1)
End Function
Function HandleGet$ (body As String)
Dim fmsg As Integer
Dim response As String
fmsg = Val(_Trim$(body))
response = "["
Dim As Integer i, idx
For i = fmsg + 1 To cmsg
idx = (i - 1) Mod MAX_MESSAGE + 1
response = response + CRLF + "{ " + _
Q$("from") + ":" + Q$(messages(idx).sender) + ", " + _
Q$("msg") + ":" + Q$(messages(idx).message) + ", " + _
Q$("time") + ":" + Q$(messages(idx).time) + " }"
If i < cmsg Then response = response + ","
Next i
response = response + CRLF + "]"
HandleGet$ = response
End Function
Function HandleSend$ (body As String)
Print "send: ["; body; "]"
Dim sender As String
sender = "Unknown"
Dim idx As Integer
idx = InStr(body, ":")
If idx > 0 Then
sender = Mid$(body, 1, idx - 1)
body = Mid$(body, idx + 1)
End If
AddMessage sender, _Trim$(body)
HandleSend$ = Str$(cmsg)
End Function
Sub AddMessage (sender As String, message As String)
Dim idx As Integer
cmsg = cmsg + 1
idx = (cmsg - 1) Mod MAX_MESSAGE + 1
messages(idx).sender = JSONString(sender)
messages(idx).message = JSONString(message)
messages(idx).time = Date$ + " " + Time$
LogMessage messages(idx)
End Sub
Function JSONString$ (s As String)
s = Replace(_Trim$(s), Chr$(13) + Chr$(10), "\n")
s = Replace(s, Chr$(10), "\n")
s = Replace(s, Chr$(34), "\" + Chr$(34))
JSONString$ = s
End Function
Sub LogMessage (msg As Message)
Print #1, msg.time; " - "; msg.sender
Print #1, msg.message
End Sub
Function Replace$ (s As String, searchString As String, newString As String)
Dim ns As String
Dim i As Integer
Dim slen As Integer
slen = Len(searchString)
For i = 1 To Len(s) '- slen + 1
If Mid$(s, i, slen) = searchString Then
ns = ns + newString
i = i + slen - 1
Else
ns = ns + Mid$(s, i, 1)
End If
Next i
Replace = ns
End Function
Function Q$ (value As String)
Q$ = Chr$(34) + value + Chr$(34)
End Function
Sub close_connection (c)
Close #Connections(c).handle
Connections(c).handle = 0
End Sub
Sub process_request (c)
Dim eol
Dim l As String
Do
eol = InStr(Connections(c).read_buf, CRLF)
If eol = 0 Then Exit Sub
l = Left$(Connections(c).read_buf, eol - 1)
Connections(c).read_buf = Mid$(Connections(c).read_buf, eol + 2)
If Connections(c).http_version = 0 Then 'First line not yet read
process_start_line c, l
Else
If l = "" Then
'headers complete; act upon request now
Select Case Connections(c).method
Case HTTP_GET
http_do_get c
Case HTTP_POST
http_do_post c
Case HTTP_HEAD
http_do_head c
End Select
Exit Sub
Else
process_header c, l
End If
End If
Loop
End Sub
Sub process_start_line (c, l As String)
'7230 3.1.1
'METHOD uri HTTP/x.y
Dim sp1, sp2
sp1 = InStr(l, " ")
If sp1 = 0 Then http_error 400, "Bad Request", c
'7231 4.3
Select Case Left$(l, sp1 - 1)
Case "GET"
Connections(c).method = HTTP_GET
Case "HEAD"
Connections(c).method = HTTP_HEAD
Case "POST"
Connections(c).method = HTTP_POST
Case Else
http_error 501, "Not Implemented", c
End Select
sp2 = InStr(sp1 + 1, l, " ")
If sp2 = 0 Or sp2 - sp1 = 1 Then http_error 400, "Bad Request", c
Connections(c).request_uri = Mid$(l, sp1 + 1, sp2 - sp1 - 1)
'7230 2.6
If Mid$(l, sp2 + 1, 5) <> "HTTP/" Then
http_error 400, "Bad Request", c
End If
Select Case Mid$(l, sp2 + 6)
Case "1.0"
Connections(c).http_version = HTTP_10
Case "1.1"
Connections(c).http_version = HTTP_11
Case Else
http_error 505, "HTTP Version Not Supported", c
End Select
End Sub
Sub process_header (c, l As String)
' ignoring headers for now
End Sub
Sub http_error (code, message As String, connection)
http_send_status connection, code, message
http_send_header connection, "Content-Length", "0"
http_send_header connection, "Connection", "close"
http_end_headers connection
close_connection connection
Http_error_info.code = code
Http_error_info.message = message
Http_error_info.connection = connection
Error 100
End Sub
If you want to connect to your own server just change the address at the top of the chat window to http://localhost:8080 (or whatever port you decide to use).
It was definitely an interesting exercise and gave me a number of ideas about new web functionality that I might want to add to QBJS's standard libraries.
Here is the client code in case you want to hot rod that side:
Code: (Select All) Import Dom From "lib/web/dom.bas"
Dim Shared As Object txtAddress, txtUsername, btnConnect, textbox, msgPanel
Dim Shared lastMsg As Integer
Dim Shared username As String
Dim Shared connected As Integer
Dim Shared refreshing As Integer
Dim Shared sending As Integer
Dim Shared sndMsg As Integer
_Title "QB Chat"
sndMsg = _SndOpen("https://raw.githubusercontent.com/boxgaming/qbjs/main/samples/apps/new-message.ogg")
InitUI
SetTimeout sub_Refresh, 1000
' HTTP Event Handlers
' ---------------------------------------------------------------
Sub OnConnect (response)
If Not connected Then
lastMsg = Val(response)
connected = -1
txtAddress.disabled = true
txtUsername.disabled = true
btnConnect.innerText = "Disconnect"
Else
connected = 0
txtAddress.disabled = false
txtUsername.disabled = false
btnConnect.innerText = "Connect"
End If
End Sub
Sub OnSend (response)
sending = 0
End Sub
Sub Refresh
If connected And Not refreshing Then
refreshing = -1
HttpSend txtAddress.value + "/get", "POST", lastMsg, sub_OnRefresh
End If
SetTimeout sub_Refresh, 1000
End Sub
Sub OnRefresh (response)
Dim playSound As Integer
Dim res As Object
res = JSON.parse(response)
Dim i as Integer
For i = 0 To res.length-1
AddMessage res[i]
If res[i].from <> username Then
playSound = -1
End If
Next i
lastMsg = lastMsg + res.length
msgPanel.scrollTop = msgPanel.scrollHeight;
If playSound Then _SndPlay sndMsg
refreshing = 0
End Sub
Sub OnRefresError
refreshing = 0
End Sub
' UI Event Handlers
' ----------------------------------------------------------
Sub OnClickConnect
If txtUsername.value = "" Then
Dom.Alert "Enter a username"
Exit Sub
End If
If Not connected Then
username = txtUsername.value
Print "Setting username: [" + username + "]"
HttpSend txtAddress.value + "/connect", "POST", username, sub_OnConnect
Else
HttpSend txtAddress.value + "/disconnect", "POST", username, sub_OnConnect
End If
End Sub
Function OnKeyPress (event)
If event.keyCode = 13 Then
'Print textbox.value
Dim msg as String
msg = _Trim$(textbox.value)
If msg <> "" Then SendMessage msg
OnKeyPress = false
End If
End Function
Sub SendMessage (msg As String)
Dim timeout As Integer
While sending And timeout < 1000
_Delay .01
timeout = timeout + 1
Wend
sending = -1
Dim body As String
body = username + ":" + _Trim$(textbox.value)
'Print "sending: [ " + body + " ]"
HttpSend txtAddress.value + "/send", "POST", body, sub_OnSend
textbox.value = ""
End Sub
Sub OnResize
'msgPanel.height = Dom.Container().height - 100
msgPanel.style.height = (_ResizeHeight - 200) + "px"
End Sub
' ----------------------------------------------------
Sub InitUI
Dim As Object container, panel, btnSend, apanel
Dom.GetImage(0).style.display = "none"
container = Dom.Container
container.style.letterSpacing = "normal"
container.style.backgroundColor = "#333"
Dom.Event window, "resize", sub_OnResize
' Create the login panel
apanel = Dom.Create("div")
apanel.style.display = "grid"
apanel.style.margin = "auto 15%"
apanel.style.textAlign = "left"
apanel.style.fontWeight = "bold"
Dom.Create "div", apanel, "Host"
Dom.Create "div", apanel, "Username"
Dom.Create "div", apanel
apanel.style.gridTemplateColumns = "auto auto auto"
txtAddress = Dom.Create("input", apanel, "https://chat.boxgaming.co")
txtUsername = Dom.Create("input", apanel)
btnConnect = Dom.Create("button", apanel, "Connect")
Dom.Event btnConnect, "click", sub_OnClickConnect
' Create the message window
msgPanel = Dom.Create("div")
msgPanel.style.backgroundColor = "#444"
msgPanel.style.overflowY = "auto"
msgPanel.style.height = "300px"
msgPanel.style.margin = "auto 15%"
msgPanel.style.textAlign = "left"
msgPanel.style.padding = "10px"
' Create send message control
Dim sendLabel As Object
panel = Dom.Create("div")
panel.style.margin = "auto 15%"
sendLabel = Dom.Create("div", panel, "Send Message")
sendLabel.style.textAlign = "left"
sendLabel.style.fontWeight = "bold"
sendLabel.style.backgroundColor = "#666"
sendLabel.style.padding = "4px"
sendLabel.style.color = "#ccc"
textbox = Dom.Create("textarea", panel)
textbox.style.width = "100%"
textbox.style.height = "100px"
Dom.Event textbox, "keydown", func_OnKeyPress
OnResize
End Sub
Sub AddMessage (msg As Object)
Dim As Object mc, s, t, m
mc = Dom.Create("div", msgPanel)
mc.style.marginBottom = "10px"
s = Dom.Create("div", mc)
If msg.from = "system" Then
s.style.color = "#00f4af"
Else
s.style.color = "#00aff4"
End If
s.style.fontWeight = "bold"
s.innerHTML = msg.from
t = Dom.Create("span", s)
t.style.color = "#999"
t.style.fontWeight = "normal"
t.style.marginLeft = "8px"
t.style.fontSize = ".8em"
t.innerHTML = msg.time
m = Dom.Create("div", mc)
m.style.color = "#efefef"
m.style.whiteSpace = "pre"
m.innerHTML = msg.msg
End Sub
Sub HttpSend(url, method, message, callbackFn, errorCallbackFn)
$If Javascript Then
const client = new XMLHttpRequest();
client.open(method, url);
client.setRequestHeader("Content-Length", message.length);
client.send(message);
client.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (callbackFn) {
callbackFn(this.responseText);
}
}
else { // assume any other status is an error
if (errorCallbackFn) {
errorCallbackFn(this.responseText);
}
}
}
};
$End If
End Sub
Sub SetTimeout(callbackFn, millis)
$If Javascript Then
setTimeout(callbackFn, millis);
$End If
End Sub
|
|
|
DAY 037: FREEFILE |
Posted by: Pete - 12-17-2022, 12:30 PM - Forum: Keyword of the Day!
- Replies (8)
|
 |
We interrupt your regularly scheduled program for this brief commercial message!
Tired of spending your hard earned money on an overly-priced file? Well now you can avoid those high fees with this tried and true QB Keyword of the Day, FREEFILE!
SYNTAX filenum& = FREEFILE
Usage: Retrieves the next available open file handle.
So what's the benefit?
Well, for most small apps, we just code...
Code: (Select All) OPEN "myfile.dat" FOR INPUT AS #1
'De Foo Foo Foo De blah, blah, blah...
CLOSE #1
However, let's say we make a big app, that uses 30 files, and some of them, depending on operations being called, stay opened!
Hmm, now keeping track of all those file numbers would require considerable thought. In fact, the only way to not to get into trouble, by using a file number which is still opened, would be to assign a unique number to each file operation, or... do it the easy way by letting the system figure it out for us. Well, that's exactly what FREEFILE does!
But first, please note: Even though we can assign a variable to FREEFILE before we OPEN the file, "You actually have to Open the file to trigger a value." Thanks, Dimster. So
please make sure you use good coding practice and associate each FREEFILE statement with each corresponding OPEN statement.
Code: (Select All) DEFLNG f
ON Rob GOSUB ThePolice
ThePolice:
filenum1 = FREEFILE
OPEN "myfile-abc.dat" FOR INPUT AS filenum1
'De Foo Foo Foo De blah, blah, blah...
filenum2 = FREEFILE
OPEN "myfile-def.dat" FOR INPUT AS filenum2
'Is all I will output to you...
IF mydata$ = "EOF" THEN CLOSE #filenum1
filenum3 = FREEFILE
OPEN "myfile-ghi.dat" FOR INPUT AS filenum3
'De Foo Foo Foo De blah, blah, blah...
filenum4 = FREEFILE
OPEN "myfile-jkl.dat" FOR INPUT AS filenum4
' And now this FREEFILE demo's through.
CLOSE #filenum1, #filenum2, #filenum3, #filenum4
RETURN
So in the code example above, FREEFILE will assign the file handles either as 1, 2, 3, and 4 or, if mydata$ = "EOF" and the first file gets closed... 1, 2, 1, 3. Remember, FREEFILE is tracking your file use, and depending on conditional statements, like in our above example, your file numbering results will vary. Also, always remember to use good coding practices. Don't have a list of pre-assigned FREEFILE assignments, at the top of your program, without each corresponding file actually being opened. You will get duplicate file handles.
Oh, and if you forget to add the # symbol, don't worry; it's optional. I just like using it as a search marker. What;s that? You don't need no stinkin' search marker. Alrighty then, save yourself some typing by omitting it. Like I always say to my wife, "It takes a big man to omit when he's wrong!" Okay Steve, enough with the "big man" jokes, already...
And now back to your regularly scheduled program, DEEP Thoughts, by jack handy...
Do woke coders OPEN files as non-BINARY?
Pete
|
|
|
Snowman screen saver playing 17 MIDI Christmas songs |
Posted by: Dav - 12-17-2022, 12:23 PM - Forum: Christmas Code
- Replies (11)
|
 |
I updated my old Snowman screen saver to test the experimental (and $UNSTABLE) MIDI playback feature of QB64PE. This is a simple looping animation that plays 17 different Christmas songs continuously. It's just something nice to have on the screen whenever you need a snowy atmosphere.
It sure is nice to have MIDI playback in QB64PE, the only negative is that it takes a rather long time to open a MIDI file. Loading these 17 MIDI files takes a couple minutes on my PC. I found that MIDI feature is most stable when opening MIDI files at the beginning and leaving them open throughout the program. Opening/Closing them over and over in the program caused hangups, so I ended up just loading them all at startup and doing a _SNDSTOP when changing to a start new song.
I'll post the code here for the curious, but you will need to download the archive to run it. You will need QB64PE v3.2 or higher to use the $MIDISOUNDFONT metacommand. I haven't tested this in Linux yet, but I guess it will work if the Linux version also supports the new MIDI metacommand.
- Dav
snowman-v103.zip (Size: 710.24 KB / Downloads: 66)
Code: (Select All) '==========
'SNOWMAN.BAS v1.03
'===========
'Play Snowman animation while playing 17 Christmas songs.
'Coded by Dav for QB64PE, DEC/2022
'ALL MEDIA SOURCES FOUND IN PUBLIC DOMAIN
'
'=======================================================================
'NOTE: THIS PROGRAM REQUIRES QB64-PE v3.2 OR HIGHER TO RUN & USE MIDI
'=======================================================================
'
'CONTROLS:
' WHILE PLAYING, USE LEFT & RIGHT ARROWS TO SKIP SONGS.
' ANY OTHER KEY QUITS THE SAVER
'
'The screen saver will start playing a random song. You an skip up/down
'to different songs using the left/right arrow keys. Pressing any other
'key will quit the program.
'
'NOTE: It takes a minute or two to load all the MIDI files on startup.
'Please be patient while they load, and enjoythe Christmas screen saver.
'
'=======================================================================
$UNSTABLE:MIDI
PRINT "=============================="
PRINT "SNOWMAN SCREENSAVER FOR QB64PE"
PRINT "(please wait while files load)"
PRINT "=============================="
PRINT
PRINT "Init MIDI soundfont...";
$MIDISOUNDFONT:DEFAULT
PRINT "OK!"
'=== Check the animation file
PRINT "Checking animation file...";
file$ = "snowman.dat"
OPEN file$ FOR BINARY AS #1
IF LOF(1) = 0 THEN
PRINT "SNOWMAN.DAT NOT FOUND!"
CLOSE 1: KILL file$
END
END IF
IF LOF(1) <> 800528 THEN
PRINT "INVALID SNOWMAN.DAT!"
CLOSE 1
END
END IF
PRINT "OK!"
'=== Build the MIDI song list..
trackmax = 17 '17 midi files
DIM song$(1 TO trackmax), songhand&(1 TO trackmax)
'name each one
song$(1) = "frosty.mid"
song$(2) = "hark.mid"
song$(3) = "away.mid"
song$(4) = "angels.mid"
song$(5) = "carol.mid"
song$(6) = "joy.mid"
song$(7) = "deck.mid"
song$(8) = "wethree.mid"
song$(9) = "holynite.mid"
song$(10) = "godrest.mid"
song$(11) = "itcame.mid"
song$(12) = "ohcome.mid"
song$(13) = "noel.mid"
song$(14) = "silent.mid"
song$(15) = "town.mid"
song$(16) = "jingle.mid"
song$(17) = "wewish.mid"
FOR s = 1 TO trackmax
PRINT "Loading MIDI file"; s; "of"; trackmax; ": "; song$(s); "...";
songhand&(s) = _SNDOPEN(song$(s), "stream")
IF songhand&(s) = 0 THEN
PRINT "Error opening file: "; song$(s)
END
END IF
PRINT "OK!"
NEXT
'=== start playing a random track
RANDOMIZE TIMER
track = INT(RND * trackmax) + 1
_SNDPLAY songhand&(track)
SCREEN 13: _FULLSCREEN _SQUAREPIXELS , _SMOOTH
DO
SEEK 1, 129
FOR f% = 1 TO 15
FF& = SEEK(1): FRM$ = INPUT$(16, 1)
FrameSize& = CVL(MID$(FRM$, 1, 4))
chunks% = CVI(MID$(FRM$, 7, 2))
d$ = INKEY$
IF d$ <> "" THEN
SELECT CASE d$
CASE CHR$(0) + CHR$(77)
_SNDSTOP songhand&(track)
track = track + 1
IF track > trackmax THEN track = 1
_SNDPLAY songhand&(track)
CASE CHR$(0) + CHR$(75)
_SNDSTOP songhand&(track)
track = track - 1
IF track < 1 THEN track = trackmax
_SNDPLAY songhand&(track)
CASE ELSE: EXIT DO
END SELECT
END IF
FOR C% = 1 TO chunks%
Fpos& = SEEK(1)
CD$ = INPUT$(6, 1)
ChunkSize& = CVL(MID$(CD$, 1, 4))
ChunkType% = CVI(MID$(CD$, 5, 2))
Fpos& = Fpos& + ChunkSize&
SELECT CASE ChunkType%
CASE 11
Clr% = 0
paks% = ASC(INPUT$(2, 1))
FOR d% = 1 TO paks%
skip% = ASC(INPUT$(1, 1))
change% = ASC(INPUT$(1, 1))
IF change% = 0 THEN change% = 256
Clr% = Clr% + skip%
FOR s% = 1 TO change%
OUT &H3C8, Clr%
OUT &H3C9, ASC(INPUT$(1, 1))
OUT &H3C9, ASC(INPUT$(1, 1))
OUT &H3C9, ASC(INPUT$(1, 1))
Clr% = Clr% + 1
NEXT
NEXT
CASE 12
skip% = CVI(INPUT$(2, 1))
change% = CVI(INPUT$(2, 1))
FOR y% = skip% TO change% + (skip% - 1)
ppos& = SEEK(1)
C$ = INPUT$(500, 1): m% = 1
paks% = ASC(MID$(C$, m%, 1)): m% = m% + 1: x% = 0
FOR d% = 1 TO paks%
s% = ASC(MID$(C$, m%, 1))
p% = ASC(MID$(C$, m% + 1, 1))
m% = m% + 2: x% = x% + s%
IF p% > 127 THEN
p% = (256 - p%)
LINE (x%, y%)-STEP(p% - 1, 0), ASC(MID$(C$, m%, 1))
x% = x% + p%: m% = m% + 1
ELSE
Row$ = MID$(C$, m%, p%)
m% = m% + p%
FOR g% = 0 TO p% - 1
PSET (x% + g%, y%), ASC(MID$(Row$, g% + 1, 1))
NEXT
x% = x% + p%
END IF
NEXT
SEEK #1, ppos& + m% - 1: C$ = ""
NEXT
CASE 16
x% = 0
FOR y% = 0 TO 199
Row$ = INPUT$(320, 1): p% = 320
FOR g% = 0 TO p% - 1
PSET (x% + g%, y%), ASC(MID$(Row$, g% + 1, 1))
NEXT
NEXT
CASE ELSE: CLOSE 1: EXIT DO
END SELECT
SEEK #1, Fpos&
NEXT
SEEK 1, FF& + FrameSize&
_DISPLAY
_LIMIT 12
NEXT
IF NOT _SNDPLAYING(songhand&(track)) THEN
_SNDSTOP songhand&(track)
track = track + 1
IF track > trackmax THEN track = 1
_SNDPLAY songhand&(track)
END IF
LOOP
_SNDSTOP songhand&(track)
_SNDCLOSE songhand&(track)
CLOSE 1
SYSTEM
|
|
|
wrangling chatgpt to get it to write a more complex program - Spacewar! |
Posted by: madscijr - 12-16-2022, 09:18 PM - Forum: General Discussion
- Replies (8)
|
 |
So the sky's the limit, right? Why not shoot for the moon, or at least Space!
Here's what happend when I asked it to write Spacewar!
Code: (Select All) Can you write a 2 player Spacewar! game in QB64 where the players are controlled with the keyboard, (player 1 controls are: up arrow = thrust, down arrow = warp, left arrow = rotate left, right arrow = rotate right, ENTER = fire; player 2 controls are: A = rotate left, D = rotate right, S = warp, W = thrust, E = fire), and includes 2 additional computer controlled players, and 2 suns?
Sure! Here is a simple Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' Declare constants for the screen width and height
CONST SCREEN_WIDTH = 640
CONST SCREEN_HEIGHT = 480
' Declare constants for the player colors
CONST PLAYER_1_COLOR = 15
CONST PLAYER_2_COLOR = 11
CONST CPU_PLAYER_COLOR = 7
' Declare constants for the sun colors
CONST SUN_1_COLOR = 12
CONST SUN_2_COLOR = 14
' Declare constants for the player controls
CONST PLAYER_1_UP = 72
CONST PLAYER_1_DOWN = 80
CONST PLAYER_1_LEFT = 75
CONST PLAYER_1_RIGHT = 77
CONST PLAYER_1_FIRE = 13
CONST PLAYER_2_UP = 87
CONST PLAYER_2_DOWN = 83
CONST PLAYER_2_LEFT = 65
CONST PLAYER_2_RIGHT = 68
CONST PLAYER_2_FIRE = 69
' Declare constants for the game states
CONST GAME_STATE_INITIALIZING = 0
CONST GAME_STATE_PLAYING = 1
CONST GAME_STATE_PAUSED = 2
CONST GAME_STATE_OVER = 3
' Declare variables for the game state and the player scores
DIM GameState AS INTEGER
DIM Player1Score AS INTEGER
DIM Player2Score AS INTEGER
' Declare a type for the players
TYPE Player
X AS SINGLE
Y AS SINGLE
Angle AS SINGLE
Speed AS SINGLE
RotationSpeed AS SINGLE
Color AS INTEGER
IsComputerControlled AS BOOLEAN
END TYPE
' Declare variables for the players
DIM Player1 AS Player
DIM Player2 AS Player
DIM CPUPlayer1 AS Player
DIM CPUPlayer2 AS Player
' Declare a type for the suns
TYPE Sun
X AS SINGLE
Y AS SINGLE
Mass AS SINGLE
Color AS INTEGER
END TYPE
' Declare variables for the suns
DIM Sun1 AS Sun
DIM Sun2 AS Sun
' Initialize the game
SUB InitializeGame
' Set the game state to initializing
GameState = GAME_STATE_INITIALIZING
' Initialize the player scores
Player1Score = 0
Player2Score = 0
' Initialize player 1
Player1.X = SCREEN_WIDTH / 4
Player1.Y = SCREEN_HEIGHT / 2
Player1.Angle = 0
Player1.Speed = 0
Player1.RotationSpeed = 0
Player1.Color = PLAYER_1_COLOR
Player1.IsComputerControlled = False
' Initialize player 2
Player2.X = SCREEN_WIDTH * 3 / 4
Player2.Y = SCREEN_HEIGHT / 2
Player2.Angle = 180
Player2.Speed = 0
Player2.R
Wait! You're not done! What if we ask it to finish? We'll even say "please"!
Code: (Select All) Would you please show the rest of the program?
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' Update the player's position and angle based on their speed and rotation speed
SUB UpdatePlayer(Player AS Player)
' Update the player's angle
Player.Angle = Player.Angle + Player.RotationSpeed
' Keep the angle between 0 and 360 degrees
IF Player.Angle < 0 THEN
Player.Angle = Player.Angle + 360
ELSEIF Player.Angle > 360 THEN
Player.Angle = Player.Angle - 360
END IF
' Calculate the player's new position based on their speed and angle
Player.X = Player.X + Player.Speed * COS(Player.Angle)
Player.Y = Player.Y + Player.Speed * SIN(Player.Angle)
' Keep the player within the screen bounds
IF Player.X < 0 THEN
Player.X = 0
ELSEIF Player.X > SCREEN_WIDTH THEN
Player.X = SCREEN_WIDTH
END IF
IF Player.Y < 0 THEN
Player.Y = 0
ELSEIF Player.Y > SCREEN_HEIGHT THEN
Player.Y = SCREEN_HEIGHT
END IF
END SUB
' Update the player's speed based on their thrust
SUB UpdatePlayerThrust(Player AS Player, IsThrusting AS BOOLEAN)
' If the player is thrusting, increase their speed
IF IsThrusting THEN
Player.Speed = Player.Speed + 0.1
' If the player is not thrusting, decrease their speed
ELSE
Player.Speed = Player.Speed - 0.1
END IF
' Limit the player's speed
IF Player.Speed < 0 THEN
Player.Speed = 0
ELSEIF Player.Speed > 5 THEN
Player.Speed = 5
END IF
END SUB
' Update the player's rotation speed based on their rotation
SUB UpdatePlayerRotation(Player AS Player, IsRotatingLeft AS BOOLEAN, IsRotatingRight AS BOOLEAN)
' If the player is rotating left, increase their rotation speed
IF Is
OK, it remembered what we asked it, and sort of continued, but not where it left off!
Shall we persevere?
Code: (Select All) Would you please show the rest of the program?
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' Update the computer-controlled players
SUB UpdateCPUPlayers
' Update player 1
UpdateCPUPlayer(CPUPlayer1)
' Update player 2
UpdateCPUPlayer(CPUPlayer2)
END SUB
' Update a computer-controlled player
SUB UpdateCPUPlayer(Player AS Player)
' Calculate the distance to player 1
DX = Player1.X - Player.X
DY = Player1.Y - Player.Y
DistanceToPlayer1 = SQR(DX * DX + DY * DY)
' Calculate the distance to player 2
DX = Player2.X - Player.X
DY = Player2.Y - Player.Y
DistanceToPlayer2 = SQR(DX * DX + DY * DY)
' Calculate the distance to the nearest sun
DX = Sun1.X - Player.X
DY = Sun1.Y - Player.Y
DistanceToSun1 = SQR(DX * DX + DY * DY)
DX = Sun2.X - Player.X
DY = Sun2.Y - Player.Y
DistanceToSun2 = SQR(DX * DX + DY * DY)
If DistanceToSun1 < DistanceToSun2 Then
DistanceToNearestSun = DistanceToSun1
Else
DistanceToNearestSun = DistanceToSun2
End If
' If the player is far from the other players and the suns, move randomly
If DistanceToPlayer1 > 100 And DistanceToPlayer2 > 100 And DistanceToNearestSun > 100 Then
' Choose a random angle and set the player's rotation speed
TargetAngle = RND * 360
If TargetAngle > Player.Angle Then
Player.RotationSpeed = 0.5
Else
Player.RotationSpeed = -0.5
End If
' If the player is close to a player, move towards them
ElseIf DistanceToPlayer1 < DistanceToPlayer2 Then
' Calculate the angle to player 1 and set the player's rotation speed
TargetAngle = ATN2(DY, DX) * 180 / PI
If TargetAngle > Player.Angle Then
Player.RotationSpeed = 0.5
Else
Player.RotationSpeed = -0.5
End If
Else
' Calculate the angle to player 2 and set the player's rotation speed
TargetAngle = ATN2(DY, DX) * 180 / PI
If TargetAngle > Player.Angle Then
Player.RotationSpeed = 0.5
Else
Don't leave me hanging, chatgpt!
Code: (Select All) Would you please show the rest of the program?
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' Update the player's speed based on the gravitational pull of the suns
SUB UpdatePlayerGravity(Player AS Player)
' Calculate the distance and angle to sun 1
DX = Sun1.X - Player.X
DY = Sun1.Y - Player.Y
DistanceToSun1 = SQR(DX * DX + DY * DY)
AngleToSun1 = ATN2(DY, DX) * 180 / PI
' Calculate the distance and angle to sun 2
DX = Sun2.X - Player.X
DY = Sun2.Y - Player.Y
DistanceToSun2 = SQR(DX * DX + DY * DY)
AngleToSun2 = ATN2(DY, DX) * 180 / PI
' Calculate the gravitational force from sun 1
GravitationalForce1 = Sun1.Mass / DistanceToSun1
' Calculate the gravitational force from sun 2
GravitationalForce2 = Sun2.Mass / DistanceToSun2
' Calculate the total gravitational force
TotalGravitationalForce = GravitationalForce1 + GravitationalForce2
' Calculate the gravitational acceleration
GravitationalAcceleration = TotalGravitationalForce / Player.Mass
' Calculate the gravitational speed
GravitationalSpeed = GravitationalAcceleration * 0.1
' Calculate the new speed based on the gravitational speed
NewSpeed = Player.Speed + GravitationalSpeed
' Limit the player's speed
If NewSpeed < 0 Then
NewSpeed = 0
ElseIf NewSpeed > 5 Then
NewSpeed = 5
End If
' Set the player's speed
Player.Speed = NewSpeed
END SUB
' Draw a player
SUB DrawPlayer(Player AS Player)
' Calculate the points of the triangle representing the player's ship
X1 = Player.X + 5 * COS
Aye aye aye! It doesn't seem to always know how to (or want to) continue from where it left off, does it?
What if we're more specific?
Code: (Select All) would you please show the full SUB UpdatePlayerRotation ?
Certainly! Here is the full UpdatePlayerRotation subroutine in the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' Update the player's rotation speed based on their rotation
SUB UpdatePlayerRotation(Player AS Player, IsRotatingLeft AS BOOLEAN, IsRotatingRight AS BOOLEAN)
' If the player is rotating left, increase their rotation speed
IF IsRotatingLeft THEN
Player.RotationSpeed = Player.RotationSpeed + 0.5
' If the player is rotating right, decrease their rotation speed
ELSEIF IsRotatingRight THEN
Player.RotationSpeed = Player.RotationSpeed - 0.5
' If the player is not rotating, decrease their rotation speed
ELSE
Player.RotationSpeed = Player.RotationSpeed - 0.1
END IF
' Limit the player's rotation speed
IF Player.RotationSpeed < -2 THEN
Player.RotationSpeed = -2
ELSEIF Player.RotationSpeed > 2 THEN
Player.RotationSpeed = 2
END IF
END SUB
This subroutine updates the player's rotation speed based on whether they are rotating left, rotating right, or not rotating at all. If the player is rotating left, their rotation speed is increased. If the player is rotating right, their rotation speed is decreased. If the player is not rotating, their rotation speed is decreased. The rotation speed is limited to a maximum of 2 and a minimum of -2.
I hope this helps! Let me know if you have any questions or if you need further assistance.
Maybe being specific is the key? Let's try to get it to spit out the rest of "teh codez"!
Code: (Select All) Thank You. Would you please show the full SUB UpdateCPUPlayer ?
Certainly! Here is the full UpdateCPUPlayer subroutine in the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' Update a computer-controlled player
SUB UpdateCPUPlayer(Player AS Player)
' Calculate the distance to player 1
DX = Player1.X - Player.X
DY = Player1.Y - Player.Y
DistanceToPlayer1 = SQR(DX * DX + DY * DY)
' Calculate the distance to player 2
DX = Player2.X - Player.X
DY = Player2.Y - Player.Y
DistanceToPlayer2 = SQR(DX * DX + DY * DY)
' Calculate the distance to the nearest sun
DX = Sun1.X - Player.X
DY = Sun1.Y - Player.Y
DistanceToSun1 = SQR(DX * DX + DY * DY)
DX = Sun2.X - Player.X
DY = Sun2.Y - Player.Y
DistanceToSun2 = SQR(DX * DX + DY * DY)
If DistanceToSun1 < DistanceToSun2 Then
DistanceToNearestSun = DistanceToSun1
Else
DistanceToNearestSun = DistanceToSun2
End If
' If the
Argh. Why does it output in such small chunks?? How useless is that?!
But we must see this thing through!
Code: (Select All) would you please show the rest of the routine?
Certainly! Here is the rest of the UpdateCPUPlayer subroutine in the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' If the player is far from the other players and the suns, move randomly
If DistanceToPlayer1 > 100 And DistanceToPlayer2 > 100 And DistanceToNearestSun > 100 Then
' Choose a random angle and set the player's rotation speed
TargetAngle = RND * 360
If TargetAngle > Player.Angle Then
Player.RotationSpeed = 0.5
Else
Player.RotationSpeed = -0.5
End If
' If the player is close to a player, move towards them
ElseIf DistanceToPlayer1 < DistanceToPlayer2 Then
' Calculate the angle to player 1 and set the player's rotation speed
TargetAngle = ATN2(DY, DX) * 180 / PI
If TargetAngle > Player.Angle Then
Player.RotationSpeed = 0.5
Else
Player.RotationSpeed = -0.5
End If
Else
' Calculate the angle to player 2 and set the player's rotation speed
TargetAngle = ATN2(DY, DX) * 180 / PI
If TargetAngle > Player.Angle Then
Player.RotationSpeed = 0.5
Else
Player.RotationSpeed = -0.5
End If
End If
' Set the player's thrust based on their distance to the nearest sun
If DistanceToNearestSun < 50 Then
Player.IsThrusting = False
Else
Player.IsThrusting = True
End If
END SUB
This subroutine updates the position and angle of a computer-controlled player based on their distance to the other players and the suns. If the player is far from the other players and the suns, they choose a random angle and rotate towards it. If the player is close to a player, they move towards them. If the player is close to a sun, they stop thrusting.
I hope this helps! Let me know if you have any questions or if you need further assistance.
OK then! We'll keep soldiering on, then...
Maybe the "p" word isn't enough, let's also be sure to say "thank you"!
Code: (Select All) Thank you. Would you please show the full SUB DrawPlayer?
Certainly! Here is the full DrawPlayer subroutine in the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' Draw a player
SUB DrawPlayer(Player AS Player)
' Calculate the points of the triangle representing the player's ship
X1 = Player.X + 5 * COS(Player.Angle)
Y1 = Player.Y + 5 * SIN(Player.Angle)
X2 = Player.X + 5 * COS(Player.Angle + 120)
Y2 = Player.Y + 5 * SIN(Player.Angle + 120)
X3 = Player.X + 5 * COS(Player.Angle + 240)
Y3 = Player.Y + 5 * SIN(Player.Angle + 240)
' Draw the triangle representing the player's ship
LINE (X1, Y1)-(X2, Y2), Player.Color
LINE (X2, Y2)-(X3, Y3), Player.Color
LINE (X3, Y3)-(X1, Y1), Player.Color
' If the player is thrusting, draw a flame behind their ship
If Player.IsThrusting Then
LINE (X2, Y2)-(X2 + 2 * COS(Player.Angle + 180), Y2 + 2 * SIN(Player.Angle + 180)), Player.Color
LINE (X3, Y3)-(X3 + 2 * COS(Player.Angle + 180), Y3 + 2 * SIN(Player.Angle + 180)), Player.Color
End If
END SUB
This subroutine draws a triangle representing the player's ship and, if the player is thrusting, a flame behind their ship. The points of the triangle are calculated using the player's angle and the coordinates of the player's ship. The triangle is then drawn using the player's color. If the player is thrusting, two lines are drawn behind the triangle to represent the flame.
I hope this helps! Let me know if you have any questions or if you need further assistance.
And on...
Code: (Select All) Thank you. Would you please show the rest of the program?
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' Initialize the players
Player1.X = 100
Player1.Y = 100
Player1.Angle = 0
Player1.RotationSpeed = 0
Player1.Speed = 0
Player1.IsThrusting = False
Player1.Mass = 10
Player1.Color = RGB(255, 0, 0)
Player2.X = 200
Player2.Y = 100
Player2.Angle = 0
Player2.RotationSpeed = 0
Player2.Speed = 0
Player2.IsThrusting = False
Player2.Mass = 10
Player2.Color = RGB(0, 255, 0)
CPUPlayer1.X = 100
CPUPlayer1.Y = 200
CPUPlayer1.Angle = 0
CPUPlayer1.RotationSpeed = 0
CPUPlayer1.Speed = 0
CPUPlayer1.IsThrusting = False
CPUPlayer1.Mass = 10
CPUPlayer1.Color = RGB(0, 0, 255)
CPUPlayer2.X = 200
CPUPlayer2.Y = 200
CPUPlayer2.Angle = 0
CPUPlayer2.RotationSpeed = 0
CPUPlayer2.Speed = 0
CPUPlayer2.IsThrusting = False
CPUPlayer2.Mass = 10
CPUPlayer2.Color = RGB(255, 255, 0)
' Initialize the suns
Sun1.X = 150
Sun1.Y = 150
Sun1.Mass = 1000
Sun2.X = 250
Sun2.Y = 150
Sun2.Mass = 1000
' Set the screen update rate
SCREEN UpdateRate(30)
' Set the window title
WINDOW Title$ = "Spacewar!"
' Set the window size
WINDOW Width = 320, Height = 240
DO
' Clear the screen
CLS
' Update the players
UpdatePlayerRotation(Player1, GetKeyState(VK_LEFT) AND 1, GetKeyState(VK_RIGHT) AND 1)
UpdatePlayerRotation(Player2, GetKeyState(VK_A) AND 1, GetKeyState(VK_D) AND 1)
UpdatePlayerThrust(Player1, GetKeyState(VK_UP) AND 1)
UpdatePlayerThrust(Player2, GetKeyState(VK_W) AND 1)
UpdatePlayerPosition(Player1)
UpdatePlayerPosition(Player2)
UpdateCPUPlayers
UpdatePlayerGravity(Player1)
UpdatePlayerGravity(Player2)
UpdatePlayerGravity(CPUPlayer1)
UpdatePlayerGravity(CPUPlayer2)
' Draw the players
DrawPlayer(Player1)
DrawPlayer(Player2)
DrawPlayer(CPUPlayer1)
DrawPlayer(CPUPlayer2)
' Draw the suns
CIRCLE (Sun1.X, Sun1.Y), 10, RGB(255, 255, 0)
CIRCLE (Sun2.X, Sun2.Y), 10, RGB(255, 255, 0)
' Check for player collisions
If PlayerCollision(Player1, Player2) Then
' End the game
OK, now it's initializing values, this looks like the main (global) code.
Can we get the rest out of this thing?
What if we're specific about telling it to NOT STOP until it's DONE?
Code: (Select All) Thank you. Would you please show the rest of the program? Don't stop until you have shown the whole thing.
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
' End the game
EXIT DO
End If
If PlayerCollision(Player1, CPUPlayer1) Then
' End the game
EXIT DO
End If
If PlayerCollision(Player1, CPUPlayer2) Then
' End the game
EXIT DO
End If
If PlayerCollision(Player2, CPUPlayer1) Then
' End the game
EXIT DO
End If
If PlayerCollision(Player2, CPUPlayer2) Then
' End the game
EXIT DO
End If
' Check for player and sun collisions
If PlayerSunCollision(Player1, Sun1) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(Player1, Sun2) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(Player2, Sun1) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(Player2, Sun2) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(CPUPlayer1, Sun1) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(CPUPlayer1, Sun2) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(CPUPlayer2, Sun1) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(CPUPlayer2, Sun2) Then
' End the game
EXIT DO
End If
' Check for player shots
If GetKeyState(VK_RETURN) AND 1 Then
Player1.Shots.AddNew
Player1.Shots(Player1.Shots.Count).X = Player1.X + 5 * COS(Player1.Angle)
Player1.Shots(Player1.Shots.Count).Y = Player1.Y + 5 * SIN(Player1.Angle)
Player1.Shots(Player1.Shots.Count).Angle = Player1.Angle
End If
If GetKeyState(VK_E) AND 1 Then
Player2.Shots.AddNew
Player2.Shots(Player2.Shots.Count).X = Player2.X + 5 * COS(Player2.Angle)
Player2.Shots(Player2.Shots.Count).Y =
A lot good that did!
Well, we've made it this far, let's see just how much more it will take to get the whole program!
Code: (Select All) Thank you. Would you please show the rest of the program?
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
Player2.Shots(Player2.Shots.Count).Angle = Player2.Angle
End If
' Update and draw the player shots
For i = 0 To Player1.Shots.Count - 1
UpdateShot(Player1.Shots(i))
DrawShot(Player1.Shots(i))
Next
For i = 0 To Player2.Shots.Count - 1
UpdateShot(Player2.Shots(i))
DrawShot(Player2.Shots(i))
Next
' Check for shot and player collisions
For i = 0 To Player1.Shots.Count - 1
If ShotPlayerCollision(Player1.Shots(i), Player2) Then
' End the game
EXIT DO
End If
If ShotPlayerCollision(Player1.Shots(i), CPUPlayer1) Then
' End the game
EXIT DO
End If
If ShotPlayerCollision(Player1.Shots(i), CPUPlayer2) Then
' End the game
EXIT DO
End If
Next
For i = 0 To Player2.Shots.Count - 1
If ShotPlayerCollision(Player2.Shots(i), Player1) Then
' End the game
EXIT DO
End If
If ShotPlayerCollision(Player2.Shots(i), CPUPlayer1) Then
' End the game
EXIT DO
End If
If ShotPlayerCollision(Player2.Shots(i), CPUPlayer2) Then
' End the game
EXIT DO
End If
Next
' Check for shot and sun collisions
For i = 0 To Player1.Shots.Count - 1
If
If... if what you nincompoop?!?!?!
Let's not lose our temper, now.
We must stay calm, cool, and collected!
After all, there has to be at least one adult in the room!
Code: (Select All) Thank you. Would you please show the rest of the program? Don't stop until you have shown the whole thing.
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
ShotSunCollision(Player1.Shots(i), Sun1) Then
Player1.Shots.Remove(i)
End If
If ShotSunCollision(Player1.Shots(i), Sun2) Then
Player1.Shots.Remove(i)
End If
Next
For i = 0 To Player2.Shots.Count - 1
If ShotSunCollision(Player2.Shots(i), Sun1) Then
Player2.Shots.Remove(i)
End If
If ShotSunCollision(Player2.Shots(i), Sun2) Then
Player2.Shots.Remove(i)
End If
Next
' Update the screen
SCREEN Update
LOOP
This is the main game loop that updates and draws the players, suns, and shots, and checks for collisions between them. The game ends when any two players collide, a player collides with a sun, or a shot hits a player or sun.
I hope this helps! Let me know if you have any questions or if you need further assistance.
Is it done? I've just about reached the end of my rope.
Can we just have it email the thing to us?
(After all, they have you give an email address when you sign up for a free account.)
Only one way to find out...
Code: (Select All) can you email me the entire program listing at my account's email address?
I'm sorry, but I am not able to send emails as I am just a text-based AI program. I do not have the ability to access or send emails. However, I can provide you with the full listing of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns.
Here is the full listing of the program:
' Struct to represent a player
TYPE Player
X AS SINGLE
Y AS SINGLE
Angle AS SINGLE
RotationSpeed AS SINGLE
Speed AS SINGLE
IsThrusting AS BOOLEAN
Mass AS SINGLE
Color AS LONG
Shots AS Collection
END TYPE
' Struct to represent a shot
TYPE Shot
X AS SINGLE
Y AS SINGLE
Angle AS SINGLE
Speed AS SINGLE
Color AS LONG
END TYPE
' Struct to represent a sun
TYPE Sun
X AS SINGLE
Y AS SINGLE
Mass AS SINGLE
END TYPE
' Declare the players and suns
DIM Player1 AS Player
DIM Player2 AS Player
DIM CPUPlayer1 AS Player
DIM CPUPlayer2 AS Player
DIM Sun1 AS Sun
DIM Sun2 AS Sun
' Function to calculate the distance between two players
FUNCTION DistanceToPlayer(Player1 AS Player, Player2 AS Player) AS SINGLE
DistanceToPlayer = SQR((Player1.X
Guess not... But wait, is it starting over?
We've seen the Player and Sun UDTs already, but we haven't seen this Shot UDT before!
That's new - what gives?? Can we coax some more out of it?
Code: (Select All) go on
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
- Player2.X) ^ 2 + (Player1.Y - Player2.Y) ^ 2)
END FUNCTION
' Function to check for a collision between two players
FUNCTION PlayerCollision(Player1 AS Player, Player2 AS Player) AS BOOLEAN
If DistanceToPlayer(Player1, Player2) < 10 Then
PlayerCollision = True
Else
PlayerCollision = False
End If
END FUNCTION
' Function to check for a collision between a player and a sun
FUNCTION PlayerSunCollision(Player AS Player, Sun AS Sun) AS BOOLEAN
If DistanceToPlayer(Player, Sun) < 10 Then
PlayerSunCollision = True
Else
PlayerSunCollision = False
End If
END FUNCTION
' Function to check for a collision between a shot and a player
FUNCTION ShotPlayerCollision(Shot AS Shot, Player AS Player) AS BOOLEAN
If DistanceToPlayer(Shot, Player) < 5 Then
ShotPlayerCollision = True
Else
ShotPlayerCollision = False
End If
END FUNCTION
' Function to check for a collision between a shot and a sun
FUNCTION ShotSunCollision(Shot AS Shot, Sun AS Sun) AS BOOLEAN
If DistanceToPlayer(Shot, Sun) < 5 Then
ShotSunCollision = True
Else
ShotSunCollision = False
End If
END FUNCTION
' Update a player's rotation
SUB UpdatePlayerRotation(Player AS Player, LeftKey AS BOOLEAN, RightKey AS BOOLEAN)
If LeftKey Then
Player.RotationSpeed = Player.RotationSpeed - 0.1
ElseIf RightKey Then
Player.RotationSpeed = Player.RotationSpeed + 0.1
Else
Player.RotationSpeed = Player.RotationSpeed * 0.9
End If
Player.Angle = Player.Angle + Player
Mmmmmkay, so is there an end to this?
We'll continue, as long as our sanity holds out!
Code: (Select All) do continue
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
'.RotationSpeed
END SUB
' Update a player's thrust
SUB UpdatePlayerThrust(Player AS Player, UpKey AS BOOLEAN)
If UpKey Then
Player.IsThrusting = True
Player.Speed = Player.Speed + 0.1
Else
Player.IsThrusting = False
End If
END SUB
' Update a player's position based on their speed and angle
SUB UpdatePlayerPosition(Player AS Player)
Player.X = Player.X + Player.Speed * COS(Player.Angle)
Player.Y = Player.Y + Player.Speed * SIN(Player.Angle)
END SUB
' Update the computer-controlled players
SUB UpdateCPUPlayers
CPUPlayer1.Angle = ATN2(Player1.Y - CPUPlayer1.Y, Player1.X - CPUPlayer1.X)
CPUPlayer1.Speed = CPUPlayer1.Speed + 0.1
CPUPlayer1.X = CPUPlayer1.X + CPUPlayer1.Speed * COS(CPUPlayer1.Angle)
CPUPlayer1.Y = CPUPlayer1.Y + CPUPlayer1.Speed * SIN(CPUPlayer1.Angle)
CPUPlayer2.Angle = ATN2(Player2.Y - CPUPlayer2.Y, Player2.X - CPUPlayer2.X)
CPUPlayer2.Speed = CPUPlayer2.Speed + 0.1
CPUPlayer2.X = CPUPlayer2.X + CPUPlayer2.Speed * COS(CPUPlayer2.Angle)
CPUPlayer2.Y = CPUPlayer2.Y + CPUPlayer2.Speed * SIN(CPUPlayer2.Angle)
END SUB
' Update a player's position based on the gravity of the suns
SUB UpdatePlayerGravity(Player AS Player)
Dim GravityForce As Single
GravityForce = (Sun1.Mass
And again...
Code: (Select All) more, please (continue listing the rest of the program)
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
* Player.Mass) / DistanceToPlayer(Player, Sun1) ^ 2
Player.X = Player.X + GravityForce * (Sun1.X - Player.X) / DistanceToPlayer(Player, Sun1)
Player.Y = Player.Y + GravityForce * (Sun1.Y - Player.Y) / DistanceToPlayer(Player, Sun1)
GravityForce = (Sun2.Mass * Player.Mass) / DistanceToPlayer(Player, Sun2) ^ 2
Player.X = Player.X + GravityForce * (Sun2.X - Player.X) / DistanceToPlayer(Player, Sun2)
Player.Y = Player.Y + GravityForce * (Sun2.Y - Player.Y) / DistanceToPlayer(Player, Sun2)
END SUB
' Update a shot's position based on its speed and angle
SUB UpdateShot(Shot AS Shot)
Shot.X = Shot.X + Shot.Speed * COS(Shot.Angle)
Shot.Y = Shot.Y + Shot.Speed * SIN(Shot.Angle)
END SUB
' Draw a player
SUB DrawPlayer(Player AS Player)
SCREEN(Player.X, Player.Y) = Player.Color
SCREEN(Player.X + 5 * COS(Player.Angle), Player.Y + 5 * SIN(Player.Angle)) = Player.Color
SCREEN(Player.X + 5 * COS(Player.Angle + 1.5), Player.Y + 5 * SIN(Player.Angle + 1.5)) = Player.Color
SCREEN(Player.X + 5 * COS(Player.Angle - 1.5), Player.Y + 5 * SIN(Player.Angle - 1.5)) = Player.Color
If Player.IsThrusting Then
SCREEN(Player.X + 5 * COS(Player.Angle + 1.5), Player.Y + 5 * SIN(Player.Angle + 1.5)) = RGB(255, 0, 0)
SCREEN(Player.X + 5 * COS(Player.Angle - 1.5), Player.Y + 5 * SIN(Player.Angle - 1.5)) = RGB(255, 0, 0)
End If
END SUB
' Draw a shot
SUB DrawShot(Shot AS Shot)
SCREEN(Shot.X, Shot.Y) = Shot.Color
END SUB
' Initialize the game
SCREEN Initialize
SCREEN SetRes 800, 600
SCREEN SetTitle "Spacewar!"
' Set the player properties
Player1.X = 400
Player1.Y = 300
Player1.Angle = 0
Player1.RotationSpeed = 0
Player1.Speed = 0
Player1.IsThrusting = False
Player1.Mass = 1
Player1.Color = RGB(255, 255, 0)
Set Player1.Shots = New Collection
Player2.X = 400
...aaaaand again...!
Code: (Select All) continue listing the rest of the program please
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
Player2.Y = 300
Player2.Angle = 0
Player2.RotationSpeed = 0
Player2.Speed = 0
Player2.IsThrusting = False
Player2.Mass = 1
Player2.Color = RGB(0, 255, 255)
Set Player2.Shots = New Collection
CPUPlayer1.X = 100
CPUPlayer1.Y = 100
CPUPlayer1.Angle = 0
CPUPlayer1.RotationSpeed = 0
CPUPlayer1.Speed = 0
CPUPlayer1.IsThrusting = False
CPUPlayer1.Mass = 1
CPUPlayer1.Color = RGB(255, 0, 255)
Set CPUPlayer1.Shots = New Collection
CPUPlayer2.X = 700
CPUPlayer2.Y = 500
CPUPlayer2.Angle = 0
CPUPlayer2.RotationSpeed = 0
CPUPlayer2.Speed = 0
CPUPlayer2.IsThrusting = False
CPUPlayer2.Mass = 1
CPUPlayer2.Color = RGB(0, 255, 0)
Set CPUPlayer2.Shots = New Collection
' Set the sun properties
Sun1.X = 100
Sun1.Y = 500
Sun1.Mass = 100
Sun2.X = 700
Sun2.Y = 100
Sun2.Mass = 100
' Main game loop
DO
' Clear the screen
SCREEN Cls
' Draw the suns
SCREEN(Sun1.X, Sun1.Y) = RGB(255, 255, 0)
SCREEN(Sun2.X, Sun2.Y) = RGB(255, 255, 0)
' Check for player and sun collisions
If PlayerSunCollision(Player1, Sun1) Or PlayerSunCollision(Player1, Sun2) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(Player2, Sun1) Or PlayerSunCollision(Player2, Sun2) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(CPUPlayer1, Sun1) Or PlayerSunCollision(CPUPlayer1, Sun2) Then
' End the game
EXIT DO
End If
If PlayerSunCollision(CPUPlayer2, Sun1) Or PlayerSunCollision(CPUPlayer2, Sun2) Then
' End the game
EXIT DO
End If
' Check for player collisions
If PlayerCollision(Player
Don't stop now! We're so close! (yeah, right)
Code: (Select All) keep listing the rest of the program
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
, Player2) Or PlayerCollision(Player1, CPUPlayer1) Or PlayerCollision(Player1, CPUPlayer2) Then
' End the game
EXIT DO
End If
If PlayerCollision(Player2, CPUPlayer1) Or PlayerCollision(Player2, CPUPlayer2) Then
' End the game
EXIT DO
End If
' Check for shot and player collisions
For i = 0 To Player1.Shots.Count - 1
If ShotPlayerCollision(Player1.Shots(i), Player2) Then
Player1.Shots.Remove(i)
End If
If ShotPlayerCollision(Player1.Shots(i), CPUPlayer1) Then
Player1.Shots.Remove(i)
End If
If ShotPlayerCollision(Player1.Shots(i), CPUPlayer2) Then
Player1.Shots.Remove(i)
End If
Next
For i = 0 To Player2.Shots.Count - 1
If ShotPlayerCollision(Player2.Shots(i), Player1) Then
Player2.Shots.Remove(i)
End If
If ShotPlayerCollision(Player2.Shots(i), CPUPlayer1) Then
Player2.Shots.Remove(i)
End If
If ShotPlayerCollision(Player2.Shots(i), CPUPlayer2) Then
Player2.Shots.Remove(i)
End If
Next
For i = 0 To CPUPlayer1.Shots.Count - 1
If ShotPlayerCollision(CPUPlayer1.Shots(i), Player1) Then
CPUPlayer1.
I can't wait to piece this all together, and see what this hunk of burnin' love hath wrought!
Code: (Select All) please keep going and list the rest
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
Shots.Remove(i)
End If
If ShotPlayerCollision(CPUPlayer1.Shots(i), Player2) Then
CPUPlayer1.Shots.Remove(i)
End If
If ShotPlayerCollision(CPUPlayer1.Shots(i), CPUPlayer2) Then
CPUPlayer1.Shots.Remove(i)
End If
Next
For i = 0 To CPUPlayer2.Shots.Count - 1
If ShotPlayerCollision(CPUPlayer2.Shots(i), Player1) Then
CPUPlayer2.Shots.Remove(i)
End If
If ShotPlayerCollision(CPUPlayer2.Shots(i), Player2) Then
CPUPlayer2.Shots.Remove(i)
End If
If ShotPlayerCollision(CPUPlayer2.Shots(i), CPUPlayer1) Then
CPUPlayer2.Shots.Remove(i)
End If
Next
' Check for shot and sun collisions
For i = 0 To Player1.Shots.Count - 1
If ShotSunCollision(Player1.Shots(i), Sun1) Or ShotSunCollision(Player1.Shots(i), Sun2) Then
Player1.Shots.Remove(i)
End If
Next
For i = 0 To Player2.Shots.Count - 1
If ShotSunCollision(Player2.Shots(i), Sun1) Or ShotSunCollision(Player2.Shots(i), Sun2) Then
Player2.Shots.Remove(i)
End If
Next
For i = 0 To CPUPlayer1.Shots.Count - 1
If ShotSunCollision(CPUPlayer1.Shots(i), Sun1) Or ShotSunCollision(CPUPlayer1.Shots(i), Sun2) Then
CPUPlayer1.Shots.Remove(i)
End If
Next
For i = 0 To CPUPlayer2.Shots.Count - 1
If ShotSunCollision(CPUPlayer2.Shots(i), Sun1) Or ShotSunCollision(CPUPlayer2.Shots(i), Sun2) Then
CPUPlayer2.Shots.Remove(i)
End If
Next
' Update the player rotations
UpdatePlayerRotation Player1, KEY(37), KEY(39)
UpdatePlayerRotation Player2, KEY(65), KEY(68)
' Update the player thrusts
UpdatePlayerThrust Player1, KEY(38)
Update
I must be a glutton for punishment!
(and you too, gentle reader, if you're still following along!)
Code: (Select All) please list the rest
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
PlayerThrust Player2, KEY(87)
' Update the players' positions
UpdatePlayerPosition Player1
UpdatePlayerPosition Player2
UpdateCPUPlayers
' Update the players' positions based on gravity
UpdatePlayerGravity Player1
UpdatePlayerGravity Player2
UpdatePlayerGravity CPUPlayer1
UpdatePlayerGravity CPUPlayer2
' Update the shots' positions
For i = 0 To Player1.Shots.Count - 1
UpdateShot Player1.Shots(i)
Next
For i = 0 To Player2.Shots.Count - 1
UpdateShot Player2.Shots(i)
Next
For i = 0 To CPUPlayer1.Shots.Count - 1
UpdateShot CPUPlayer1.Shots(i)
Next
For i = 0 To CPUPlayer2.Shots.Count - 1
UpdateShot CPUPlayer2.Shots(i)
Next
' Draw the players
DrawPlayer Player1
DrawPlayer Player2
DrawPlayer CPUPlayer1
DrawPlayer CPUPlayer2
' Draw the shots
For i = 0 To Player1.Shots.Count - 1
DrawShot Player1.Shots(i)
Next
For i = 0 To Player2.Shots.Count - 1
DrawShot Player2.Shots(i)
Next
For i = 0 To CPUPlayer1.Shots.Count - 1
DrawShot CPUPlayer1.Shots(i)
Next
For i = 0 To CPUPlayer2.Shots.Count - 1
DrawShot CPUPlayer2.Shots(i)
Next
' Check for player firing
If KEY(13) Then
Dim Shot As Shot
Shot.X = Player1.X + 5 * COS(Player1.Angle)
Shot.Y = Player1.Y + 5 * SIN(Player1.Angle
Will it ever end???
Code: (Select All) please list the rest
Certainly! Here is the rest of the Spacewar! game in QB64 that allows two players to control their ships with the keyboard, and includes two additional computer-controlled players and two suns:
Shot.Angle = Player1.Angle
Shot.Speed = 5
Shot.Color = RGB(255, 255, 255)
Player1.Shots.Add Shot
End If
If KEY(69) Then
Dim Shot As Shot
Shot.X = Player2.X + 5 * COS(Player2.Angle)
Shot.Y = Player2.Y + 5 * SIN(Player2.Angle)
Shot.Angle = Player2.Angle
Shot.Speed = 5
Shot.Color = RGB(255, 255, 255)
Player2.Shots.Add Shot
End If
' Update the screen
SCREEN Update
LOOP
' End the game
SCREEN Close
I hope this helps! Let me know if you have any questions or need further assistance.
ChatGPT Dec 15 Version. Free Research Preview. Our goal is to make AI systems more natural and safe to interact with. Your feedback will help us improve.
You hope this helps!
Oh it helped, all right!
Helped me straight to the looney bin!
Up next, let's try to piece this bad boy together!
|
|
|
Useful Constants |
Posted by: SMcNeill - 12-16-2022, 08:23 PM - Forum: SMcNeill
- Replies (1)
|
 |
Code: (Select All) 'Common CONST which I tend to find very useful to plug into programs
Const True = -1%%
Const False = 0%%
$If WIN Then
Const Slash = "\"
$Else
Const Slash = "/"
$End If
Dim Shared As String MyAppPath, MyAppName, MyAppFullPath, Quote ' These are shared strings, but they need to be gotten at the start of a program.
MyAppPath = _CWD$ ' I'll grab them here and just pretend that they're CONST.
MyAppName = Mid$(Command$(0), _InStrRev(Command$(0), Slash$) + 1) 'Shhh... don't tell anyone. It'll be our little secret!
MyAppFullPath = MyAppPath + Slash + MyAppName
Quote = Chr$(34)
'Variable Type Limits
Const Bit_Min = -1`, Bit_Max = 0`
Const UBit_Min = 0~`, UBit_Max = 1~`
Const Byte_Min = -128%%, Byte_Max = 127%%, Int8_Min = -128%%, Int8_Max = 127%%
Const UByte_Min = 0~%%, UByte_Max = 255~%%, UInt8_Min = 0~%%, UInt_Max = 255~%%
Const Integer_Min = -32768%, Integer_Max = 32767%, Int16_Min = -32768%, Int16_Max = 32767%
Const UInteger_Min = 0~%, UInteger_Max = 65535~%, UInt16_Min = 0~%, UInt16_Max = 65535~%
Const Long_Min = -2147483648&, Long_Max = 2147483647&, Int32_Min = -2147483648&, Int32_Max = 2147483647&
Const ULong_Min = 0~&, ULong_Max = 4294967295~&, UInt32_Min = 0~&, UInt32_Max = 4294967295~&
Const Integer64_Min = -9223372036854775808&&, Integer64_Max = 9223372036854775807&&, Int64_Min = -9223372036854775808&&, Int64_Max = 9223372036854775807&&
Const UInteger64_Min = 0~&&, UInteger64_Max = 18446744073709551615~&&, UInt64_Min = 0~&&, UInt64_Max = 18446744073709551615~&&
Const Single_Min! = -2.802597E-45, Single_Max! = 3.402823E+38
Const Double_Min# = -4.490656458412465E-324, Double_Max = 1.797693134862310E+308
Const Float_Min## = -1.18E-4932, Float_Max## = 1.18E+4932
$If 32BIT Then
Const Offset_Min = -2147483648&, Offset_Max = 2147483647&
Const UOffset_Min = 0~&, UOffset_Max = 4294967295~&
$Else
Const Offset_Min = -9223372036854775808&&, Offset_Max = 9223372036854775807&&
Const UOffset_Min = 0~&&, UOffset_Max = 18446744073709551615~&&
$End If
'Keyhit Values
Const Key_Backspace = 8
Const Key_Tab = 9
Const Key_Enter = 13
Const Key_Shift = 16
Const Key_Control = 17
Const Key_Alt = 18
Const Key_CapsLock = 20
Const Key_ESC = 27
Const Key_Space = 32
Const Key_LeftShift = 100304
Const Key_RightShift = 100303
Const Key_LeftCtrl = 100306
Const Key_RightCtrl = 100305
Const Key_LeftAlt = 100308
Const Key_RightAlt = 100307
Const Key_PageUp = 18688
Const Key_PageDown = 20736
Const Key_LeftArrow = 19200
Const Key_RightArrow = 19712
Const Key_UpArrow = 18432
Const Key_DownArrow = 20480
Const Key_F1 = 15104
Const Key_F2 = 15360
Const Key_F3 = 15616
Const Key_F4 = 15872
Const Key_F5 = 16128
Const Key_F6 = 16384
Const Key_F7 = 16640
Const Key_F8 = 16896
Const Key_F9 = 17152
Const Key_F10 = 17408
Const Key_F11 = 34048
Const Key_F12 = 34304
Const Key_Home = 18176
Const Key_Delete = 21248
Const Key_Insert = 20992
Const Key_Win = 100311
Const Key_Menu = 100319
Const Key_PrintScreen = 900044
Const Key_ScrollLock = 100302
Const Key_Accent = 96
Const Key_Tilde = 126
Const Key_1 = 49
Const Key_2 = 50
Const Key_3 = 51
Const Key_4 = 52
Const Key_5 = 53
Const Key_6 = 54
Const Key_7 = 55
Const Key_8 = 56
Const Key_9 = 57
Const Key_0 = 48
Const Key_Minus = 45, Key_Dash = 45, Key_Subtract = 45
Const Key_Equal = 61
Const Key_Exclaim = 33
Const Key_At = 64
Const Key_Pound = 35
Const Key_Dollar = 36
Const Key_Percent = 37
Const Key_Power = 94, Key_Caret = 94
Const Key_And = 38
Const Key_Star = 42, Key_Multiply = 42
Const Key_LeftParen = 40
Const Key_RightParen = 41
Const Key_Underscore = 95
Const Key_Plus = 43, Key_Add = 43
Const Key_LowerCase = 32 ' Add to key values to get lowercase characters
Const Key_A = 65, Key_A_Low = 97 'for example, 65 is A. 65 + 32 = a (97)
Const Key_B = 66, Key_B_Low = 98 'So it's basically IF Key_B AND Key_LowerCase THEN 'it's a "b"
Const Key_C = 67, Key_C_Low = 99
Const Key_D = 68, Key_D_Low = 10
Const Key_E = 69, Key_E_Low = 101
Const Key_F = 70, Key_F_Low = 102
Const Key_G = 71, Key_G_Low = 103
Const Key_H = 72, Key_H_Low = 104
Const Key_I = 73, Key_I_Low = 105
Const Key_J = 74, Key_J_Low = 106
Const Key_K = 75, Key_K_Low = 107
Const Key_L = 76, Key_L_Low = 108
Const Key_M = 77, Key_M_Low = 109
Const Key_N = 78, Key_N_Low = 110
Const Key_O = 79, Key_O_Low = 111
Const Key_P = 80, Key_P_Low = 112
Const Key_Q = 81, Key_Q_Low = 113
Const Key_R = 82, Key_R_Low = 114
Const Key_S = 83, Key_S_Low = 115
Const Key_T = 84, Key_T_Low = 116
Const Key_U = 85, Key_U_Low = 117
Const Key_V = 86, Key_V_Low = 118
Const Key_W = 87, Key_W_Low = 119
Const Key_X = 88, Key_X_Low = 110
Const Key_Y = 89, Key_Y_Low = 111
Const Key_Z = 90, Key_Z_Low = 112
Const Key_LeftBrace = 91, Key_LeftBracket = 91
Const Key_RightBrace = 93, Key_RightBracket = 93
Const Key_BackSlash = 92
Const Key_LeftCurly = 123
Const Key_RightCurly = 125
Const Key_Pipe = 124
Const Key_Semicolon = 59
Const Key_Colon = 58
Const Key_Apostrophy = 39
Const Key_Quote = 34
Const Key_Comma = 44
Const Key_Period = 46, Key_Dot = 46
Const Key_ForwardSlash = 47, Key_Slash = 47
Const Key_LessThan = 60
Const Key_GreaterThan = 62
Const Key__QuestionMark = 63, Key_Question = 63
'Note that the following keys are listed as 900000+. These are KeyHit codes from my KeyHit library and not _KEYHIT values.
'For _KEYHIT values, drop the 900 in front, and QB64-PE will report the other values on a KEYUP event only.
'So you can read Mute, but it'll *ONLY* read as -173, and that's *only* when you release the key.
'None of these read at all for us on keydown events.
Const Key_Mute = 900173
Const Key_VolUp = 900174
Const Key_VolDown = 900175
Const Key_Stop = 900178
Const Key_Rewind = 900177
Const Key_Forward = 900176
Const Key_Play = 900179
'And for the following, these are also 900000+ KeyHit library codes. These are mouse events that QB64-PE simply fails to recognize
'at all. These neither register with _KEYHIT as a keydown, nor a keyup event. As far as _KEYHIT is concerned, these keys
'simply don't exist!
Const Key_LeftMouse = 900001
Const Key_RightMouse = 900002
Const Key_MiddleMouse = 900004
Const Key_Mouse4 = 900005
Const Key_Mouse5 = 900006
'Inkey$ Codes
Dim Shared As String Inkey_Backspace: Inkey_Backspace = Chr$(8)
Dim Shared As String Inkey_Tab: Inkey_Tab = Chr$(9)
Dim Shared As String Inkey_Enter: Inkey_Enter = Chr$(13)
Dim Shared As String Inkey_ESC: Inkey_ESC = Chr$(27)
Const Inkey_Space = " "
Dim Shared As String Inkey_PageUp: Inkey_PageUp = MKI$(18688)
Dim Shared As String Inkey_PageDown: Inkey_PageDown = MKI$(20736)
Dim Shared As String Inkey_LeftArrow: Inkey_LeftArrow = MKI$(19200)
Dim Shared As String Inkey_RightArrow: Inkey_RightArrow = MKI$(19712)
Dim Shared As String Inkey_UpArrow: Inkey_UpArrow = MKI$(18432)
Dim Shared As String Inkey_DownArrow: Inkey_DownArrow = MKI$(20480)
Dim Shared As String Inkey_F1: Inkey_F1 = MKI$(15104)
Dim Shared As String Inkey_F2: Inkey_F2 = MKI$(15360)
Dim Shared As String Inkey_F3: Inkey_F3 = MKI$(15616)
Dim Shared As String Inkey_F4: Inkey_F4 = MKI$(15872)
Dim Shared As String Inkey_F5: Inkey_F5 = MKI$(16128)
Dim Shared As String Inkey_F6: Inkey_F6 = MKI$(16384)
Dim Shared As String Inkey_F7: Inkey_F7 = MKI$(16640)
Dim Shared As String Inkey_F8: Inkey_F8 = MKI$(16896)
Dim Shared As String Inkey_F9: Inkey_F9 = MKI$(17152)
Dim Shared As String Inkey_F10: Inkey_F10 = MKI$(17408)
Dim Shared As String Inkey_F11: Inkey_F11 = MKI$(34048)
Dim Shared As String Inkey_F12: Inkey_F12 = MKI$(34304)
Dim Shared As String Inkey_Home: Inkey_Home = MKI$(18176)
Dim Shared As String Inkey_Delete: Inkey_Delete = MKI$(21248)
Dim Shared As String Inkey_Insert: Inkey_Insert = MKI$(20992)
Const Inkey_Accent = "`"
Const Inkey_Tilde = "~"
Const Inkey_1 = "1"
Const Inkey_2 = "2"
Const Inkey_3 = "3"
Const Inkey_4 = "4"
Const Inkey_5 = "5"
Const Inkey_6 = "6"
Const Inkey_7 = "7"
Const Inkey_8 = "8"
Const Inkey_9 = "9"
Const Inkey_0 = "0"
Const Inkey_Minus = "-", Inkey_Dash = "-", Inkey_Subtract = "-"
Const Inkey_Equal = "="
Const Inkey_Exclaim = "!"
Const Inkey_At = "@"
Const Inkey_Pound = "#"
Const Inkey_Dollar = "$"
Const Inkey_Percent = "%"
Const Inkey_Power = "^", Inkey_Caret = "^"
Const Inkey_And = "&"
Const Inkey_Star = "*", Inkey_Multiply = "*"
Const Inkey_LeftParen = "("
Const Inkey_RightParen = ")"
Const Inkey_Underscore = "_"
Const Inkey_Plus = "+", Inkey_Add = "+"
Const Inkey_A = "A", Inkey_A_Low = "a" 'for example, 65 is A. 65 + 32 = a (97)
Const Inkey_B = "B", Inkey_B_Low = "b" 'So it's basically IF Inkey_B AND Inkey_LowerCase THEN 'it's a "b"
Const Inkey_C = "C", Inkey_C_Low = "c"
Const Inkey_D = "D", Inkey_D_Low = "d"
Const Inkey_E = "E", Inkey_E_Low = "e"
Const Inkey_F = "F", Inkey_F_Low = "f"
Const Inkey_G = "G", Inkey_G_Low = "g"
Const Inkey_H = "H", Inkey_H_Low = "h"
Const Inkey_I = "I", Inkey_I_Low = "i"
Const Inkey_J = "J", Inkey_J_Low = "j"
Const Inkey_K = "K", Inkey_K_Low = "k"
Const Inkey_L = "L", Inkey_L_Low = "l"
Const Inkey_M = "M", Inkey_M_Low = "m"
Const Inkey_N = "N", Inkey_N_Low = "n"
Const Inkey_O = "O", Inkey_O_Low = "o"
Const Inkey_P = "P", Inkey_P_Low = "p"
Const Inkey_Q = "Q", Inkey_Q_Low = "q"
Const Inkey_R = "R", Inkey_R_Low = "r"
Const Inkey_S = "S", Inkey_S_Low = "s"
Const Inkey_T = "T", Inkey_T_Low = "t"
Const Inkey_U = "U", Inkey_U_Low = "u"
Const Inkey_V = "V", Inkey_V_Low = "v"
Const Inkey_W = "W", Inkey_W_Low = "w"
Const Inkey_X = "X", Inkey_X_Low = "x"
Const Inkey_Y = "Y", Inkey_Y_Low = "y"
Const Inkey_Z = "Z", Inkey_Z_Low = "z"
Const Inkey_LeftBrace = "[", Inkey_LeftBracket = "["
Const Inkey_RightBrace = "]", Inkey_RightBracket = "]"
Const Inkey_BackSlash = "\"
Const Inkey_LeftCurly = "{"
Const Inkey_RightCurly = "}"
Const Inkey_Pipe = "|"
Const Inkey_Semicolon = ";"
Const Inkey_Colon = ":"
Const Inkey_Apostrophy = "'"
Dim Shared As String Inkey_Quote: Inkey_Quote = Chr$(34)
Const Inkey_Comma = ","
Const Inkey_Period = ".", Inkey_Dot = "."
Const Inkey_ForwardSlash = "/", Inkey_Slash = "/"
Const Inkey_LessThan = "<"
Const Inkey_GreaterThan = ">"
Const Inkey_QuestionMark = "?", Inkey_Question = "?"
A list of the various CONST that I find myself endlessly using in my programs. Gathered in one spot like this, they make for an easy $INCLUDE at the top of my code, and then I can make use of the names and values without having to always look up a keycode chart somewhere for my needs.
For example, what's the minimum value you can use for a LONG? Long_Min!
What's the input value of the left arrow key with Inkey$? Inkey_LeftArrow!
What's the keydown value of the left shift key? Key_LeftShift!
Names of things that are much easier to remember than their actual values -- that's all this is. Free to use, abuse, and alter as wanted/needed.
Enjoy, guys.
|
|
|
DAY 036: _STRCMP |
Posted by: Pete - 12-16-2022, 07:09 PM - Forum: Keyword of the Day!
- Replies (3)
|
 |
Ever want to compare strings, without being called a racist? Well now you can with _STRCMP...
SYNTAX compare% = _STRCMP(string1$, string2$)
What does it do? _STRCMP reads each string from left to right, and compares each ASCII character value. At the first instance where one value is not equal to the other, the function exits and returns:
-1 If the string character being compared in the first string is smaller than the string character being compared in the second string.
0 If the strings are equal.
1 If the string character being compared in the first string is larger than the string character being compared in the second string.
Also, remember that: Upper case letters are valued less than lower case letters in the ASCII evaluation. (Wiki).
So here is a quick function using our Keyword of the Day to see how it "functions."
Code: (Select All) DO
LINE INPUT "String 1: "; a$
LINE INPUT "String 2: "; b$: PRINT
IF a$ = "" AND b$ = "" THEN END
PRINT scompare(a$, b$)
PRINT
LOOP
FUNCTION scompare$ (string1$, string2$)
a% = _STRCMP(string1$, string2$)
SELECT CASE a%
CASE -1
scompare$ = "The first string is smaller than the second string."
CASE 0
scompare$ = "The first string is equal to the second string."
CASE 1
scompare$ = "The first string is larger than the second string."
END SELECT
END FUNCTION
Well hold on a minute there, Sparky...
What if I input a$ = 99.9 and b$ = 1024?
Well, _STRCMP would correctly return "The first string is larger than the second string." because with left to right evaluation, "9" of a$ is larger than "1" of b$.
To get a method to tell you if the value of a string representing a numeric value like we use in String Math routines, requires a bit of work...
Code: (Select All) DO
LINE INPUT "Number 1: "; s1$
LINE INPUT "Number 2: "; s2$
IF s1$ = "" OR s2$ = "" THEN EXIT DO
gl% = 0: sm_greater_lesser s1$, s2$, gl%
SELECT CASE gl%
CASE -1: PRINT s1$; " < "; s2$
CASE 0: PRINT s1$; " = "; s2$
CASE 1: PRINT s1$; " > "; s2$
END SELECT
PRINT
LOOP
SUB sm_greater_lesser (stringmatha$, stringmathb$, gl%)
compa$ = stringmatha$: compb$ = stringmathb$ ' So original variables do not get changed.
DO
WHILE -1 ' Falx loop.
IF gl% = 2 THEN EXIT WHILE ' For bypassing sign and decimal adjustments when only positive non-decimal numbers are being evaluated.
' Remove trailing zeros after a decimal point.
IF INSTR(compa$, ".") THEN
DO UNTIL RIGHT$(compa$, 1) <> "0" AND RIGHT$(compa$, 1) <> "." AND RIGHT$(compa$, 1) <> "-"
compa$ = MID$(compa$, 1, LEN(compa$) - 1)
LOOP
END IF
IF INSTR(compb$, ".") THEN
DO UNTIL RIGHT$(compb$, 1) <> "0" AND RIGHT$(compb$, 1) <> "." AND RIGHT$(compb$, 1) <> "-"
compb$ = MID$(compb$, 1, LEN(compb$) - 1)
LOOP
END IF
IF MID$(compa$, 1, 2) = "-0" OR compa$ = "" OR compa$ = "-" THEN compa$ = "0"
IF MID$(compb$, 1, 2) = "-0" OR compb$ = "" OR compb$ = "-" THEN compb$ = "0"
' A - and +
j% = 0: k% = 0
IF LEFT$(compa$, 1) = "-" THEN j% = -1
IF LEFT$(compb$, 1) = "-" THEN k% = -1
IF k% = 0 AND j% THEN gl% = -1: EXIT DO
IF j% = 0 AND k% THEN gl% = 1: EXIT DO
j&& = INSTR(compa$, ".")
k&& = INSTR(compb$, ".")
' A starting decimal and non-decimal.
IF j&& = 0 AND k&& = 1 THEN
IF compa$ = "0" THEN gl% = -1 ELSE gl% = 1
EXIT DO
END IF
IF k&& = 0 AND j&& = 1 THEN
IF compb$ = "0" THEN gl% = 1 ELSE gl% = -1
EXIT DO
END IF
' remove decimals and align.
j2&& = 0: k2&& = 0
IF j&& <> 0 OR k&& <> 0 THEN
IF j&& THEN compa$ = MID$(compa$, 1, INSTR(compa$, ".") - 1) + MID$(compa$, INSTR(compa$, ".") + 1): j2&& = LEN(compa$) - j&& + 1
IF k&& THEN compb$ = MID$(compb$, 1, INSTR(compb$, ".") - 1) + MID$(compb$, INSTR(compb$, ".") + 1): k2&& = LEN(compb$) - k&& + 1
compa$ = compa$ + STRING$(k2&& - j2&&, "0")
compb$ = compb$ + STRING$(j2&& - k2&&, "0")
END IF
EXIT WHILE
WEND
' Remove leading zeros if any.
DO UNTIL LEFT$(compa$, 1) <> "0"
compa$ = MID$(compa$, 2)
LOOP
IF compa$ = "" THEN compa$ = "0"
DO UNTIL LEFT$(compb$, 1) <> "0"
compb$ = MID$(compb$, 2)
LOOP
IF compb$ = "" THEN compb$ = "0"
' Both positive or both negative whole numbers.
SELECT CASE LEN(compa$)
CASE IS < LEN(compb$)
gl% = -1
CASE IS = LEN(compb$)
IF compa$ = compb$ THEN
gl% = 0
ELSEIF compa$ > compb$ THEN gl% = 1
ELSEIF compa$ < compb$ THEN gl% = -1
END IF
CASE IS > LEN(compb$)
gl% = 1
END SELECT
EXIT DO
LOOP
END SUB
So sure, doing a simple numeric comparison like...
Code: (Select All) PRINT 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997 = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997.1
Output 0 for False, which is correct.
...works, but below, using a strings to represent these values, the VAL() function converts our string numbers to S.N. with some loss of accuracy along the way.
Code: (Select All) PRINT VAL("99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997") > VAL("99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997.1")
Output -1 for True, which is incorrect; as VAL() of both is: 1D+224
So since String Math is designed for enormous numbers, a string comparison function for val() had to be created. If you have an alternative, please post, but otherwise please just keep in mind the difference between _STRCMP, which compares the ASCII character values from left to right, and the function I posted, which compares the numeric value of the strings.
Pete
|
|
|
program generated by chatgpt |
Posted by: James D Jarvis - 12-16-2022, 07:06 PM - Forum: General Discussion
- Replies (17)
|
 |
this program was generated by chat gpt. I did have to lightly edit it because it tried to use the symbol "&" in place of the symbol ";" in print commands, otherwise it all came from this prompt:
create qb64pe source code that allows the user to select from a displayed list of 10 entries.
Code: (Select All) ' Define an array to hold the list of entries
Dim entries(10) As String
' Populate the array with sample data
entries(1) = "Apple"
entries(2) = "Banana"
entries(3) = "Cherry"
entries(4) = "Date"
entries(5) = "Eggplant"
entries(6) = "Fig"
entries(7) = "Grape"
entries(8) = "Honeydew"
entries(9) = "Kiwi"
entries(10) = "Lemon"
' Display the list of entries
Print "Please select an entry from the list:"
For i = 1 To 10
Print i; ") "; entries(i)
Next i
' Get the user's selection
Do
Input "Enter your selection: ", selection
If selection < 1 Or selection > 10 Then
Print "Invalid selection. Please try again."
Else
Exit Do
End If
Loop
' Display the selected entry
Print "You selected: "; entries(selection)
|
|
|
_OS$ Very interesting, but... |
Posted by: Pete - 12-16-2022, 04:57 PM - Forum: Help Me!
- Replies (4)
|
 |
Rowin and Martin's Laugh IN aside,
I don't quite get this about _OS$.
The wiki states _OS$ ireturns the operating system that compiled the program. So If I compile a program in Windows, and a Linux user gets the .exe file, and runs it., all the _OS$ conditional statements can do is to indicate to the Linux user the exe was compiled on a Windows OS.
It seems to me we would be better off having it or something else identify the OS the exe is actually running on, but maybe I'm missing some value here. I haven't used it before.
Pete
|
|
|
Sending Color to Specific Location |
Posted by: Dimster - 12-16-2022, 04:03 PM - Forum: Works in Progress
- Replies (7)
|
 |
I was just wondering if there is way to change the color of a value which is displaying on a screen. For example, something like this
Screen _NewImage(1900, 900, 32)
$Color:32
A$ = "5"
Color White
_PrintString (100, 100), A$
Color Green
_printstring (100,100), color green
So the value 5 is displaying on the screen in white however, later on in the program I want that value to pop out if certain conditions apply. Present coding requires a complete repeat of the value plus the new color but if I just want to change the color only at that same location doesn't appear color only can be changed? or might there be a way???
|
|
|
|