Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Pacman Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
monkeynamedsteve




PostPosted: Thu May 11, 2006 1:39 pm   Post subject: Pacman Help

I'm working on Pacman for my computers final and I've done pretty well so far with help from this site. Most things are working properly and all I need to add is ghosts and dots. When I added the first ghost this morning, it made Pacman flicker like crazy. I was wondering how to fix this, I've allredy tried View.Update, I don't know If I did it right. If anyone has suggestions on this or how to make the ghost have "a mind of its own". Right now, I hooked him up to the pacman movement just so that he moves around.

code:

setscreen ("graphics:490;540")
View.Set ("offscreenonly")
const BACKGROUND : int := black
const PACMAN_RADIUS : int := 11
const DOT_RADIUS : int := 2
const WALL : int := 30
const ROOM : int := 4
var direction : int := 360
var x : int := 245
var y : int := 230
var x_temp, y_temp : int := 0
var xInc, yInc : int := 0
var yDot, xDot : int
var MouthAngle : int := 0
var move : string (1) := KEY_RIGHT_ARROW
%Ghost
const GHOST_RADIUS : int := 12
var ghostX : int := 220
var ghostX_temp, ghostY_temp : int := 0
var ghostY : int := 270
var ghostCol : array 1 .. 4 of int := init (40, 43, 53, 13)


colourback (BACKGROUND)
cls
%Draw Walls
var box1x : array 1 .. 49 of int := init (0, 0, 0, 490, 0, 480, 50, 140, 190, 240, 290, 340, 0, 440, 50, 90, 140, 290, 390, 390, 0, 90, 0, 140, 190, 240, 340, 390, 390, 400, 0, 90, 0, 390, 390, 390,
    140, 340, 140, 290, 50, 390, 190, 240, 50, 140, 290, 390, 240)
var box1y : array 1 .. 49 of int := init (0, 0, 530, 260, 300, 300, 50, 50, 100, 100, 50, 60, 100, 100, 150, 160, 160, 160, 160, 160, 210, 260, 260, 260, 210, 210, 260, 260, 260, 200, 300, 360, 350,
    350, 300, 300, 300, 300, 350, 350, 400, 400, 400, 410, 450, 450, 450, 450, 450)
var box2x : array 1 .. 49 of int := init (10, 490, 490, 480, 10, 490, 200, 150, 300, 250, 440, 350, 50, 490, 100, 100, 200, 350, 400, 440, 100, 100, 90, 150, 300, 250, 350, 480, 400, 480, 100, 100,
    100, 480, 480, 400, 150, 350, 200, 350, 100, 440, 300, 250, 100, 200, 350, 440, 250)
var box2y : array 1 .. 49 of int := init (260, 10, 540, 0, 540, 540, 60, 110, 110, 50, 60, 110, 110, 110, 160, 100, 150, 150, 100, 150, 200, 200, 250, 200, 200, 150, 200, 250, 200, 210, 310, 310,
    360, 360, 310, 360, 410, 410, 360, 360, 410, 410, 410, 350, 490, 490, 490, 490, 540)

for i : 1 .. 49
    drawfillbox (box1x (i), box1y (i), box2x (i), box2y (i), grey)
end for
for i : 45 .. 48
    drawfillbox (box1x (i) + 10, box1y (i) + 10, box2x (i) - 10, box2y (i) - 10, black)
end for
%Mouth Opening and closing....

%Draw mouth close
procedure drawMouthClose (x : int, y : int)
    MouthAngle := MouthAngle mod 45 + 1
    drawfillarc (x, y, PACMAN_RADIUS, PACMAN_RADIUS, MouthAngle + direction, -MouthAngle + direction, 14)
end drawMouthClose



%Erase Pacman
procedure erasePacman (x : int, y : int)
    %The "+1" allows the prgram to remove any extra pixels
    drawfillarc (x, y, PACMAN_RADIUS, PACMAN_RADIUS, MouthAngle + direction, -MouthAngle + direction, BACKGROUND)
end erasePacman

%Change Direction
procedure directionChange
    if hasch then
        getch (move)
        if ord (move) = ORD_DOWN_ARROW then
            direction := 270
            xInc := 0
            yInc := -1
        elsif ord (move) = ORD_UP_ARROW then
            direction := 90
            xInc := 0
            yInc := 1
        elsif ord (move) = ORD_LEFT_ARROW then
            direction := 180
            xInc := -1
            yInc := 0
        elsif ord (move) = ORD_RIGHT_ARROW then
            direction := 0
            xInc := 1
            yInc := 0

        end if
    end if
end directionChange

%Ghost
procedure ghost
    drawfillarc (ghostX, ghostY, GHOST_RADIUS, GHOST_RADIUS, 0, 180, ghostCol (1))
    Draw.FillBox (ghostX - 12, ghostY, ghostX + 12, ghostY - 10, ghostCol (1))
    Draw.FillOval (ghostX - 10, ghostY - 10, 2, 2, ghostCol (1))
    Draw.FillOval (ghostX - 3, ghostY - 10, 2, 2, ghostCol (1))
    Draw.FillOval (ghostX + 3, ghostY - 10, 2, 2, ghostCol (1))
    Draw.FillOval (ghostX + 10, ghostY - 10, 2, 2, ghostCol (1))
    Draw.FillOval (ghostX - 5, ghostY, 3, 3, white)
    Draw.FillOval (ghostX + 5, ghostY, 3, 3, white)
    Draw.FillOval (ghostX - 5, ghostY, 1, 1, black)
    Draw.FillOval (ghostX + 5, ghostY, 1, 1, black)

end ghost

%Ghost Erase
procedure ghostErase
    drawfillarc (ghostX, ghostY, GHOST_RADIUS, GHOST_RADIUS, 0, 180, black)
    Draw.FillBox (ghostX - 12, ghostY, ghostX + 12, ghostY - 10, black)
    Draw.FillOval (ghostX - 10, ghostY - 10, 2, 2, black)
    Draw.FillOval (ghostX - 3, ghostY - 10, 2, 2, black)
    Draw.FillOval (ghostX + 3, ghostY - 10, 2, 2, black)
    Draw.FillOval (ghostX + 10, ghostY - 10, 2, 2, black)
end ghostErase

%Dots
procedure dots
    for xDot : 26 .. 480 by 23
        drawfilloval (xDot, 27, DOT_RADIUS, DOT_RADIUS, 31)

    end for
end dots

%Movement
const DEL : int := 15
dots
loop
    for i : x .. maxx
        drawMouthClose (x, y)
        View.Update
        delay (DEL)
        erasePacman (x, y)
        ghost
        View.Update
        delay (DEL)
        ghostErase
        if ghostX_temp = 15 then
            ghostX := 475
        elsif ghostX_temp = 475 then
            ghostX := 15
        end if
        x_temp := x + xInc
        y_temp := y + yInc
        directionChange
        if x_temp = 15 then
            x := 475
        elsif x_temp = 475 then
            x := 15
        end if
        ghostX := ghostX + xInc
        ghostY := ghostY + yInc
        if whatdotcolor (x_temp + PACMAN_RADIUS, y_temp) ~= grey
                and whatdotcolor (x_temp + PACMAN_RADIUS + ROOM, y_temp) ~= grey
                and whatdotcolor (x_temp - PACMAN_RADIUS - ROOM, y_temp) ~= grey
                and whatdotcolor (x_temp, y_temp + PACMAN_RADIUS + ROOM) ~= grey
                and whatdotcolor (x_temp, y_temp - PACMAN_RADIUS - ROOM) ~= grey
                and whatdotcolor (x_temp + PACMAN_RADIUS + ROOM, y_temp + PACMAN_RADIUS + ROOM) ~= grey
                and whatdotcolor (x_temp - PACMAN_RADIUS - ROOM, y_temp - PACMAN_RADIUS - ROOM) ~= grey
                and whatdotcolor (x_temp - PACMAN_RADIUS - ROOM, y_temp + PACMAN_RADIUS + ROOM) ~= grey
                and whatdotcolor (x_temp + PACMAN_RADIUS + ROOM, y_temp - PACMAN_RADIUS - ROOM) ~= grey
                and whatdotcolor (x_temp + PACMAN_RADIUS, y_temp + PACMAN_RADIUS + ROOM) ~= grey
                and whatdotcolor (x_temp - PACMAN_RADIUS, y_temp - PACMAN_RADIUS - ROOM) ~= grey
                and whatdotcolor (x_temp - PACMAN_RADIUS, y_temp + PACMAN_RADIUS + ROOM) ~= grey
                and whatdotcolor (x_temp + PACMAN_RADIUS, y_temp - PACMAN_RADIUS - ROOM) ~= grey
                and whatdotcolor (x_temp + PACMAN_RADIUS + ROOM, y_temp + PACMAN_RADIUS) ~= grey
                and whatdotcolor (x_temp - PACMAN_RADIUS - ROOM, y_temp - PACMAN_RADIUS) ~= grey
                and whatdotcolor (x_temp - PACMAN_RADIUS - ROOM, y_temp + PACMAN_RADIUS) ~= grey
                and whatdotcolor (x_temp + PACMAN_RADIUS + ROOM, y_temp - PACMAN_RADIUS) ~= grey
                then

            x := x + xInc
            y := y + yInc


        end if


    end for

end loop


Any help on ghost movement, getting rid of the flicker or just general comments would be very much appreciated.
Sponsor
Sponsor
Sponsor
sponsor
monkeynamedsteve




PostPosted: Thu May 11, 2006 2:31 pm   Post subject: (No subject)

Here's the file, makes it easier


PacManEDIT[Smooth]).t
 Description:

Download
 Filename:  PacManEDIT[Smooth]).t
 Filesize:  6.09 KB
 Downloaded:  118 Time(s)

do_pete




PostPosted: Thu May 11, 2006 2:36 pm   Post subject: (No subject)

Why do you have that for loop in your loop?
monkeynamedsteve




PostPosted: Thu May 11, 2006 2:45 pm   Post subject: (No subject)

Oh, hey, thanks. I had that in from before. I was trying several different approaches at the boundaries and I must have forgotten to get rid of it.
Clayton




PostPosted: Thu May 11, 2006 3:04 pm   Post subject: (No subject)

im not too sure why pacman flickers like that, but it might have something to do with the way you are drawing it, also, try and speed it up a tad (its waaaaaaaaaay slow) looks okay though, look forward to seeing it done Very Happy
monkeynamedsteve




PostPosted: Thu May 11, 2006 3:10 pm   Post subject: (No subject)

Ya, I tried slowing it down to see if it would help the flickering. I promise to post the final program.
Jenkinz




PostPosted: Fri May 12, 2006 8:07 am   Post subject: (No subject)

yea, if you didn't see theres a for loop in your loop?
do_pete




PostPosted: Fri May 12, 2006 11:42 am   Post subject: (No subject)

do_pete wrote:
Why do you have that for loop in your loop?

monkeynamedsteve wrote:
Oh, hey, thanks. I had that in from before. I was trying several different approaches at the boundaries and I must have forgotten to get rid of it.
I think he saw it.
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Fri May 12, 2006 4:24 pm   Post subject: (No subject)

now that i think about it, it might be that drawing the ghosts is slowing the computer down and causing a flicker from your erasing part (for pacman) because it is being drawn in black, and the slower speed is causing it to be seen, but i dont know for sure, i hope you figure it out Very Happy
monkeynamedsteve




PostPosted: Fri May 12, 2006 5:49 pm   Post subject: (No subject)

Thanks alot Super Freak, that seems like the only reason I can think of. The school computers are slow and stupid, I think I'm just going to use sprites. What fun, need to learn about those.
NikG




PostPosted: Fri May 12, 2006 5:50 pm   Post subject: (No subject)

I looked at the code you provided as a download, not what you posted in your first post.

The reason pacman is flickering is that you have 2 View.Updates.
You should use only one! It's an easy fix though. Change it from:
code:
        drawMouthClose (x, y)
        View.Update
        delay (DEL)
        erasePacman (x, y)
        ghost
        View.Update
        delay (DEL)
        ghostErase
to
code:
        drawMouthClose (x, y)
        ghost
        View.Update
        delay (DEL)
        erasePacman (x, y)
        ghostErase

Notice how I moved ghost above the View.Update and removed the second delay.
monkeynamedsteve




PostPosted: Fri May 12, 2006 5:55 pm   Post subject: (No subject)

Thank you so much, you saved me so much time.
It works so smoothly now.
I'm gonna give you 5 bits casue I'm never gonna use them.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: