Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
improve cpu movement in pong
#2
In most cases, making a computer opponent seem human is about making it more stupid - make it do stupid little moves, make pauses etc Smile

Johnno posted a pong on the forum written in n7. I think it was based on something I wrote in n6. So I'll post the n6 source code here. I just tried it, and the computer really does make silly little moves and pauses - making it seem more human.

Code:
rem ==================================================================
rem Pong, from basicprogramming.org.
rem
rem By Marcus Johansson.
rem ==================================================================

import "Speed.lib"

set window 0, 0, 320, 240, false, 2
set redraw off

plyX = 8
comX = 320 - 24
comY# = 120.0
comWantedY# = 120.0
comTimer = 25
hasBall = 1
ballX#
ballY#
ballDX#
ballDY#
ballSpeed# = 3.0
comScore = 0
plyScore = 0

do
    plyY = mousey()

    if hasBall = 1
        ballX# = float(plyX + 16)
        ballY# = float(plyY - 4)
        if mousebutton(0)
            ballDX = 1.0
            ballDY = 0.0
            ballSpeed = 2.0
            hasBall = 0
        endif
    elseif hasBall = 2
        ballX = float(comX - 8)
        ballY = comY - 4.0
    else
        ballX = ballX + ballDX*ballSpeed
        ballY = ballY + ballDY*ballSpeed
        if ballY < 0.0
            ballY = 0.0
            ballDY = -ballDY
        elseif ballY# > 240.0 - 8.0
            ballY = 240.0 - 8.0
            ballDY = -ballDY
        endif
        
        
        if ballDX < 0.0
            if RectsOverlap(plyX, plyY - 24, 16, 48, int(ballX), int(ballY), 8, 8)
                dy# = (ballY - float(plyY))/64.0
                dx# = 0.5
                k# = 1.0/sqr(dx*dx + dy*dy)
                ballDX# = k*dx
                ballDY# = k*dy
                ballSpeed = min#(ballSpeed*1.1, 6.0)
            endif
        else
            if RectsOverlap(comX, int(comY) - 24, 16, 48, int(ballX), int(ballY), 8, 8)
                dy# = (ballY - comY)/64.0
                dx# = -0.5
                k# = 1.0/sqr(dx*dx + dy*dy)
                ballDX# = k*dx
                ballDY# = k*dy
                ballSpeed = min#(ballSpeed*1.1, 6.0)
            endif
        endif

        if ballX > 320.0
            hasBall = 1
            plyScore = plyScore + 1
        elseif ballX < -8.0
            hasBall = 2
            comTimer = 50
            comWantedY = float(rnd(240))
            comScore = comScore + 1
        endif
    endif

    comTimer = comTimer - 1
    if comTimer <= 0
        if hasBall = 1
            comWantedY = ballY - 16.0 + float(rnd(32)) + (float(comX) - ballX)/ballSpeed*ballDY
            comTimer = comTimer + 25 + rnd(25)
        elseif hasBall = 2
            hasBall = 0
            ballDX = -1.0
            ballDY = 0.0
            ballSpeed = 2.0
        else
            comWantedY = ballY - 20.0 + float(rnd(40)) + (float(comX) - ballX)/ballSpeed*ballDY
            if ballDX < 0.0
                comTimer = comTimer + 20 + rnd(20)
            else
                comTimer = comTimer + 6 + rnd(6)
            endif
        endif
    endif
    comY = 0.90*comY + 0.1*comWantedY

    set color 0, 0, 0
    cls
    set color 255, 255, 255
    for y = 0 to 29
        draw rect 160 - 2, y*8 + 2, 4, 4, true
    next

    draw rect plyX, plyY - 24, 16, 48, true
    draw rect comX, int(comY) - 24, 16, 48, true
    draw rect int(ballX), int(ballY), 8, 8, true
    set caret 16, 8
    center plyScore
    set caret 320 - 16, 8
    center comScore

    redraw
    proc SPD_HoldFrame(60)    
until keydown(27) or not running()

function RectsOverlap(x0, y0, w0, h0, x1, y1, w1, h1)
    if x0 + w0 < x1 then return false
    if y0 + h0 < y1 then return false
    if x1 + w1 < x0 then return false
    if y1 + h1 < y0 then return false
    return true
endfunc
Reply


Messages In This Thread
improve cpu movement in pong - by aliensoldier - 01-26-2024, 02:10 PM
RE: improve cpu movement in pong - by Marcus - 01-26-2024, 03:51 PM
RE: improve cpu movement in pong - by johnno56 - 01-26-2024, 08:56 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)