Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wrangling chatgpt to get it to write a more complex program - Spacewar!
#2
Well here it is, the whole she-bang. 

Evidently when I asked it to email the program, and it listed that code, the second listing did things slightly differently than the first. However, each version had things the other was missing, so I merged them. There were places where the same thing was done slightly differently, and I had to use my best judgment / guess which version of the code to go with. Wherever I wasn't entirely sure, I also included the 2nd version commented out, just in case. To facilitate merging & de-duping, I changed the order of the subs/functions to be alphabetic, just to make them easy to find. 

This is a total hack, but all the code is here, and with some editing it should work. Some issues are easy to fix - for example chatgpt doesn't seem to realize that there is no native Boolean in QB64, so we just change "boolean" to "integer" and make constants for TRUE and FALSE. Some not so trivial, for example there is no such thing as a Collection in QB64! That, and maybe some other things, will have to be reworked. 

But for a general skeleton of a Spacewar! game, it's probably decent. If anyone wants to try to make this bad boy live, have at it! :-D

Code: (Select All)
' Spacewar! the classic game, which we tried to have chatgpt program for us...
'
' >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
' Struct to represent a player
TYPE Player
    X AS SINGLE
    Y AS SINGLE
    Angle AS SINGLE
    Speed AS SINGLE
    RotationSpeed AS SINGLE
    IsThrusting AS integer ' BOOLEAN
    Mass AS SINGLE
    'Color AS INTEGER
    Color AS LONG
    Shots AS Collection
    IsComputerControlled AS BOOLEAN
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

' Declare a type for the suns
' Struct to represent a sun
TYPE Sun
    X AS SINGLE
    Y AS SINGLE
    Mass AS SINGLE
    Color AS INTEGER
END TYPE

' Declare variables for the players
DIM Player1 AS Player
DIM Player2 AS Player
DIM CPUPlayer1 AS Player
DIM CPUPlayer2 AS Player

' Declare variables for the suns
DIM Sun1 AS Sun
DIM Sun2 AS Sun

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' Initialize the game
SCREEN Initialize

' Set the screen update rate
SCREEN UpdateRate(30)

' Set the window title
'WINDOW Title$ = "Spacewar!"
SCREEN SetTitle "Spacewar!"

' Set the window size
'WINDOW Width = 320, Height = 240
SCREEN SetRes 800, 600

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' Initialize the players
' Set the player properties

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)
'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 = 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)
'Player2.X = 400
'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 = 200
CPUPlayer1.Angle = 0
CPUPlayer1.RotationSpeed = 0
CPUPlayer1.Speed = 0
CPUPlayer1.IsThrusting = False
CPUPlayer1.Mass = 10
CPUPlayer1.Color = RGB(0, 0, 255)
'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 = 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)
'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

' Initialize the suns
' Set the sun properties
Sun1.X = 150
Sun1.Y = 150
Sun1.Mass = 1000
'Sun1.X = 100
'Sun1.Y = 500
'Sun1.Mass = 100

Sun2.X = 250
Sun2.Y = 150
Sun2.Mass = 1000
'Sun2.X = 700
'Sun2.Y = 100
'Sun2.Mass = 100

' 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.

' Main game loop
DO
    ' ================================================================================================================================================================
    ' Clear the screen
    CLS
    'SCREEN Cls

    ' ================================================================================================================================================================
    ' Update the players

    ' Update the player rotations
    UpdatePlayerRotation Player1, KEY(37), KEY(39)
    UpdatePlayerRotation Player2, KEY(65), KEY(68)
    'UpdatePlayerRotation(Player1, GetKeyState(VK_LEFT) AND 1, GetKeyState(VK_RIGHT) AND 1)
    'UpdatePlayerRotation(Player2, GetKeyState(VK_A) AND 1, GetKeyState(VK_D) AND 1)

    ' Update the player thrusts
    UpdatePlayerThrust Player1, KEY(38)
    UpdatePlayerThrust Player2, KEY(87)
    'UpdatePlayerThrust(Player1, GetKeyState(VK_UP) AND 1)
    'UpdatePlayerThrust(Player2, GetKeyState(VK_W) AND 1)

    ' Update the players' positions
    UpdatePlayerPosition Player1
    'UpdatePlayerPosition(Player1)
    UpdatePlayerPosition Player2
    'UpdatePlayerPosition(Player2)
    UpdateCPUPlayers

    ' Update the players' positions based on gravity
    UpdatePlayerGravity Player1
    'UpdatePlayerGravity(Player1)
    UpdatePlayerGravity Player2
    'UpdatePlayerGravity(Player2)
    UpdatePlayerGravity CPUPlayer1
    'UpdatePlayerGravity(CPUPlayer1)
    UpdatePlayerGravity CPUPlayer2
    'UpdatePlayerGravity(CPUPlayer2)

    ' 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
        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

    ' ================================================================================================================================================================
    ' 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)
    'SCREEN(Sun1.X, Sun1.Y) = RGB(255, 255, 0)
    'SCREEN(Sun2.X, Sun2.Y) = RGB(255, 255, 0)

    ' ================================================================================================================================================================
    ' Check for player collisions
    If PlayerCollision(Player1, Player2) Then
        ' 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 collisions
    'If PlayerCollision(Player1, 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 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
    '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 = ???
        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

    ' 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 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 shot and player collisions

    ' check for player 1
    For i = 0 To Player1.Shots.Count - 1
        If ShotPlayerCollision(Player1.Shots(i), Player2) Then
            'Player1.Shots.Remove(i)

            ' End the game
            EXIT DO
        End If
        If ShotPlayerCollision(Player1.Shots(i), CPUPlayer1) Then
            'Player1.Shots.Remove(i)

            ' End the game
            EXIT DO
        End If
        If ShotPlayerCollision(Player1.Shots(i), CPUPlayer2) Then
            'Player1.Shots.Remove(i)

            ' End the game
            EXIT DO
        End If
    Next

    ' check for player 2
    For i = 0 To Player2.Shots.Count - 1
        If ShotPlayerCollision(Player2.Shots(i), Player1) Then
            'Player2.Shots.Remove(i)

            ' End the game
            EXIT DO
        End If
        If ShotPlayerCollision(Player2.Shots(i), CPUPlayer1) Then
            'Player2.Shots.Remove(i)

            ' End the game
            EXIT DO
        End If
        If ShotPlayerCollision(Player2.Shots(i), CPUPlayer2) Then
            'Player2.Shots.Remove(i)

            ' End the game
            EXIT DO
        End If
    Next

    'check cpu player 1
    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
    ''*** REDUNDANT? ***
    'For i = 0 To CPUPlayer1.Shots.Count - 1
    '    If ShotPlayerCollision(CPUPlayer1.Shots(i), Player1) Then
    '        CPUPlayer1.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

    'check cpu player 2
    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
    ''*** REDUNDANT? ***
    '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
        'If 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) Or ShotSunCollision(Player2.Shots(i), Sun2) Then
            Player2.Shots.Remove(i)
        End If
        '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

' End the game
SCREEN Close

System

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' Function to calculate the distance between two players
FUNCTION DistanceToPlayer(Player1 AS Player, Player2 AS Player) AS SINGLE
    DistanceToPlayer = SQR((Player1.X - Player2.X) ^ 2 + (Player1.Y - Player2.Y) ^ 2)
END FUNCTION ' DistanceToPlayer

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' 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.
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 ' DrawPlayer

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'
'' 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 ' DrawShot

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' 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
    ???
END SUB ' InitializeGame

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' 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 ' PlayerCollision

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' 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 ' PlayerSunCollision

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' 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 ' ShotPlayerCollision

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' 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 ' ShotSunCollision

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' Update a computer-controlled player
' 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.
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
            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 ' UpdateCPUPlayer

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' Update the computer-controlled players
SUB UpdateCPUPlayers
    ' Update player 1
    UpdateCPUPlayer(CPUPlayer1)

    ' Update player 2
    UpdateCPUPlayer(CPUPlayer2)
END SUB ' UpdateCPUPlayers

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'' 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 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 ' UpdatePlayer

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' 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 ' UpdatePlayerGravity

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'
'' Update a player's position based on the gravity of the suns
'SUB UpdatePlayerGravity(Player AS Player)
'    Dim GravityForce As Single
'    GravityForce = (Sun1.Mass???
'???
'
'* 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 ' UpdatePlayerGravity

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' 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 ' UpdatePlayerPosition

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' Update the player's rotation speed based on their rotation
' 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.
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 ' UpdatePlayerRotation

'' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'' 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
'...
'
''.RotationSpeed
'END SUB ' UpdatePlayerRotation

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

' 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 ' UpdatePlayerThrust

' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'' 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 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 ' UpdateShot
Reply


Messages In This Thread
RE: wrangling chatgpt to get it to write a more complex program - Spacewar! - by madscijr - 12-16-2022, 11:01 PM



Users browsing this thread: 1 Guest(s)