02-02-2025, 04:53 PM
(This post was last modified: 02-02-2025, 07:30 PM by TempodiBasic.
Edit Reason: answer to the question made
)
@Deciheximal
fine code, I can affirm that chatGPT is better coder than me.
QB64pe IDE found 2 unused variables, wposX and wposY.
Interesting the Helper suborutines set and the MoveCheck example function.
ChatGPT prefers the Pitagora's theorem for collision detecting.
Just a human dubt... is it possible packing all the action done into the many FOR loops into just one FOR loop?
yes it seems to be possible
here my confusional mod that packes all FOR loop of asteroids to one loop
it is just a curiosity and not a better code. But it still works.
The game sometimes starts with a GAME OVER. It is probable that sometimes the starship and an asteroid are born in the same place.
fine code, I can affirm that chatGPT is better coder than me.
QB64pe IDE found 2 unused variables, wposX and wposY.
Interesting the Helper suborutines set and the MoveCheck example function.
ChatGPT prefers the Pitagora's theorem for collision detecting.
Just a human dubt... is it possible packing all the action done into the many FOR loops into just one FOR loop?
yes it seems to be possible
here my confusional mod that packes all FOR loop of asteroids to one loop
Code: (Select All)
'--------------------------------------------
' Asteroids Clone in QB64 Phoenix Edition
'--------------------------------------------
' === CONSTANTS AND GLOBAL DECLARATIONS ===
Const SCREENSIZEX = 352 ' 352 pixels wide
Const SCREENSIZEY = 256 ' 256 pixels tall
Const MAX_SPEED = 5 ' Maximum speed for the spaceship
Common Shared viewSizeX, viewSizeY
' --- Game Settings ---
Const MAX_ASTEROIDS = 5 ' Number of asteroids
' --- Asteroid Variables (arrays are global and sized on declaration) ---
Dim Shared astX(1 To MAX_ASTEROIDS), astY(1 To MAX_ASTEROIDS)
Dim Shared astDX(1 To MAX_ASTEROIDS), astDY(1 To MAX_ASTEROIDS)
' --- Spaceship Variables ---
Common Shared shipX, shipY ' Position of the spaceship
Common Shared shipAngle ' Facing direction (in degrees)
Common Shared shipSpeed ' Current speed
' --- Bullet Variables ---
Common Shared bulletActive ' 0 = no bullet active, 1 = bullet is active
Common Shared bulletX, bulletY ' Bullet position
Common Shared bulletDX, bulletDY ' Bullet velocity
'============================================
' SET UP SCREEN AND INITIAL GAME VARIABLES
'============================================
'_FULLSCREEN
Screen _NewImage(SCREENSIZEX, SCREENSIZEY, 13) ' Third argument MUST be 13
_Font 8 ' Ensures the font is 8x8 (QB64-specific)
' Set play area (for example, if you want to use ASCII coordinate notions)
viewSizeX = 32
viewSizeY = 24
' Initialize spaceship in the center of the screen.
shipX = SCREENSIZEX / 2
shipY = SCREENSIZEY / 2
shipAngle = 0 ' Facing right (0 degrees)
shipSpeed = 0
' Bullet is initially inactive.
bulletActive = 0
' Randomize and initialize asteroid positions and velocities.
Randomize Timer
For ii = 1 To MAX_ASTEROIDS
astX(ii) = Rnd * SCREENSIZEX
astY(ii) = Rnd * SCREENSIZEY
' Give each asteroid a random velocity between -1 and 1 (pixels per frame)
astDX(ii) = (Rnd * 2 - 1)
astDY(ii) = (Rnd * 2 - 1)
Next ii
'============================================
' MAIN GAME LOOP
'============================================
Do
' --- Handle Input ---
' Rotate spaceship left: key 4 (keycode 52 or alternate 19200)
If _KeyDown(52) Or _KeyDown(19200) Then
shipAngle = shipAngle - 5
End If
' Rotate spaceship right: key 6 (keycode 54 or alternate 19712)
If _KeyDown(54) Or _KeyDown(19712) Then
shipAngle = shipAngle + 5
End If
' Accelerate forward: use key 8 (keycode 56 or alternate 18432) as “up”
If _KeyDown(56) Or _KeyDown(18432) Then
shipSpeed = shipSpeed + 0.1
End If
' Decelerate / Brake: use key 2 (keycode 50 or alternate 20480) as “down”
If _KeyDown(50) Or _KeyDown(20480) Then
shipSpeed = shipSpeed - 0.1
If shipSpeed < 0 Then shipSpeed = 0
End If
' Halt momentum: B key (keycode 66 or alternate 98) sets speed to 0.
If _KeyDown(66) Or _KeyDown(98) Then
shipSpeed = 0
End If
' Enforce maximum speed limit.
If shipSpeed > MAX_SPEED Then shipSpeed = MAX_SPEED
' Fire Bullet: space bar (keycode 32)
If _KeyDown(32) Then
If bulletActive = 0 Then
bulletActive = 1
bulletX = shipX
bulletY = shipY
' Set bullet velocity in the direction the ship is facing.
bulletDX = Cos(shipAngle * 3.14159 / 180) * 4
bulletDY = Sin(shipAngle * 3.14159 / 180) * 4
End If
End If
' --- Update Game Objects ---
' Update spaceship position (using its current speed and angle)
shipX = shipX + Cos(shipAngle * 3.14159 / 180) * shipSpeed
shipY = shipY + Sin(shipAngle * 3.14159 / 180) * shipSpeed
' Screen wrapping for spaceship:
If shipX < 0 Then shipX = SCREENSIZEX
If shipX > SCREENSIZEX Then shipX = 0
If shipY < 0 Then shipY = SCREENSIZEY
If shipY > SCREENSIZEY Then shipY = 0
' Update bullet if active:
If bulletActive = 1 Then
bulletX = bulletX + bulletDX
bulletY = bulletY + bulletDY
' If bullet goes off screen, deactivate it.
If bulletX < 0 Or bulletX > SCREENSIZEX Or bulletY < 0 Or bulletY > SCREENSIZEY Then
bulletActive = 0
End If
End If
Cls ' Clear the screen
' Update asteroids:
For ii = 1 To MAX_ASTEROIDS
astX(ii) = astX(ii) + astDX(ii)
astY(ii) = astY(ii) + astDY(ii)
' Wrap asteroids around the screen.
If astX(ii) < 0 Then astX(ii) = SCREENSIZEX
If astX(ii) > SCREENSIZEX Then astX(ii) = 0
If astY(ii) < 0 Then astY(ii) = SCREENSIZEY
If astY(ii) > SCREENSIZEY Then astY(ii) = 0
' Check collisions between bullet and asteroids.
If bulletActive = 1 Then
' Using 2 as bullet radius and 12 as asteroid radius.
If isCollision(bulletX, bulletY, 2, astX(ii), astY(ii), 12) Then
' On collision, reset the asteroid and deactivate the bullet.
astX(ii) = Rnd * SCREENSIZEX
astY(ii) = Rnd * SCREENSIZEY
bulletActive = 0
End If
End If
' Check collisions between spaceship and asteroids.
' Here, we use an approximate collision radius of 10 for the spaceship.
If isCollision(shipX, shipY, 10, astX(ii), astY(ii), 12) Then
Call ESCAPETEXT("GAME OVER")
End If
' Draw each asteroid as a circle.
Circle (astX(ii), astY(ii)), 12, 15
Next ii
' --- Collision Detection ---
' Check collisions between bullet and asteroids.
'If bulletActive = 1 Then
' For ii = 1 To MAX_ASTEROIDS
' ' Using 2 as bullet radius and 12 as asteroid radius.
' If isCollision(bulletX, bulletY, 2, astX(ii), astY(ii), 12) Then
' ' On collision, reset the asteroid and deactivate the bullet.
' astX(ii) = Rnd * SCREENSIZEX
' astY(ii) = Rnd * SCREENSIZEY
' bulletActive = 0
' Exit For
' End If
' Next ii
'End If
' Check collisions between spaceship and asteroids.
' Here, we use an approximate collision radius of 10 for the spaceship.
'For ii = 1 To MAX_ASTEROIDS
' If isCollision(shipX, shipY, 10, astX(ii), astY(ii), 12) Then
' Call ESCAPETEXT("GAME OVER")
' End If
'Next ii
' --- Draw Everything ---
'Cls ' Clear the screen
' Draw the spaceship as a triangle.
' Calculate the three vertices based on shipX, shipY and shipAngle.
shipTipX = shipX + Cos(shipAngle * 3.14159 / 180) * 10
shipTipY = shipY + Sin(shipAngle * 3.14159 / 180) * 10
shipLeftX = shipX + Cos((shipAngle + 140) * 3.14159 / 180) * 8
shipLeftY = shipY + Sin((shipAngle + 140) * 3.14159 / 180) * 8
shipRightX = shipX + Cos((shipAngle - 140) * 3.14159 / 180) * 8
shipRightY = shipY + Sin((shipAngle - 140) * 3.14159 / 180) * 8
Line (shipLeftX, shipLeftY)-(shipTipX, shipTipY), 15
Line (shipTipX, shipTipY)-(shipRightX, shipRightY), 15
Line (shipRightX, shipRightY)-(shipLeftX, shipLeftY), 15
' Draw bullet if it is active.
If bulletActive = 1 Then
Circle (bulletX, bulletY), 2, 15
End If
' Draw each asteroid as a circle.
'For ii = 1 To MAX_ASTEROIDS
' Circle (astX(ii), astY(ii)), 12, 15
'Next ii
_Display ' Refresh the graphics screen
' --- Exit Check ---
If _KeyDown(27) Then Call ESCAPE(0) ' Exit if ESC is pressed
_Limit 30 ' Limit frame rate to 30 FPS
Loop
'============================================
' COLLISION DETECTION FUNCTION
'============================================
Function isCollision (obj1X, obj1Y, r1, obj2X, obj2Y, r2)
Dim dx, dy, distanceSquared, rSumSquared
dx = obj1X - obj2X
dy = obj1Y - obj2Y
distanceSquared = dx * dx + dy * dy
rSumSquared = (r1 + r2) * (r1 + r2)
If distanceSquared <= rSumSquared Then
isCollision = 1
Else
isCollision = 0
End If
End Function
'============================================
' EXAMPLE FUNCTION (for reference)
'============================================
Function moveCheck (wPosX, wPosY)
' A dummy function to illustrate function returns.
moveCheck = 1
End Function
'============================================
' HELPER SUBROUTINES (NEVER MODIFY THESE)
'============================================
Sub ZLOCATE (wx, wy)
' Adjusts LOCATE since QB64’s LOCATE starts at row 1.
Locate wy + 1, wx + 1
End Sub
Sub ESCAPE (code)
' Display an exit message with the given code and end the program.
Color 15
ZLOCATE 10, 5: Print " "
ZLOCATE 10, 6: Print "ENDED WITH CODE:"; code; " "
ZLOCATE 10, 7: Print " "
End
End Sub
Sub ESCAPETEXT (wStr$)
' Display an exit message (for example, GAME OVER) and end the program.
Color 15
xPos = 1
yPos = 5
If wStr$ = "" Then wStr$ = "No text sent"
ZLOCATE xPos, yPos: Print " "
ZLOCATE xPos, yPos + 2: Print " " ' Clear line before text in case of wrap
ZLOCATE xPos, yPos + 1: Print " "; wStr$; " "
End
End Sub
it is just a curiosity and not a better code. But it still works.
The game sometimes starts with a GAME OVER. It is probable that sometimes the starship and an asteroid are born in the same place.

