Re: Not to be impatient.....

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Fri, 15 Dec 2000 15:43:23 -0800, Thomas wrote:

>Hello,
>
>    I don't want to come across as impatient, but I sent an e-mail a while
>ago regarding my Snake program. I've got the message here. Please respond,
>I'm waiting in programming limbo:

OK Thomas, here is but one possible solution.  Somehow I experienced 'de ja
vue' while working on this (only the first time it seemed like I was
working with some kind of aircraft...).

I'll leave it to you to clean up the trails and make it look like a real
snake.

I hope this helps.
(and I hope I haven't done too much for you; I know you don't like having
people actually doing things for you but as you already know, I feel I can
teach best by example...)
  --------------------

include graphics.e
include get.e
include image.e

constant
  -- adjustable parameters
  DELAY = .05,
  STEP  = 5,

  -- resolution for graphics_mode(259)
  MAX_X = 800,
  MAX_Y = 600,

  -- index parameters
  X = 1,
  Y = 2,

  -- define special keys
  ESC   = 27,
  S_DIR = {
    328,  -- UP
    331,  -- LEFT
    333,  -- RIGHT
    336 },-- DOWN
  DXDY = { {0,-STEP}, {-STEP,0}, {STEP,0}, {0,STEP} }

  --------------------

-- try setting 800x600x256 graphics mode
if graphics_mode(259) then
  -- try forcing VESA graphics mode
  --  (I'm not sure if this is useful since the first try
  --   might succeed but it just doesn't look right...??)
  use_vesa(1)
  if graphics_mode(259) then
    -- if still no go, then give up
    puts( 1, "Unable to set graphics mode... aborting." )
    abort(1)
  end if
end if

object snake
integer keypressed, s_dir
sequence s_pos, dxdy, s_size

s_pos = {100,100}
dxdy  = {STEP,0}

snake = read_bitmap( "snake.bmp" )
if atom( snake ) then
  printf( 1, "Error #%d opening 'snake.bmp'.", snake )
  if wait_key() then
    abort( 2 )
  end if
else
  s_size = { length( snake[2][1] ), length( snake[2] ) }
end if

all_palette( snake[1]/4 )
bk_color( BLUE )

  --------------------

procedure delay( atom i )
  i += time()
  while time() < i do
    -- nothing
  end while
end procedure

  --------------------

procedure end_game()
  -- restore graphics mode
  if not graphics_mode(-1) then
    puts( 1, "The end.  Next key press exits..." )
    -- wait for next key
    if wait_key() then
      abort(0)
    end if
  end if
end procedure

  --------------------

procedure bound_chk()
  if s_pos[X] < 0 or s_pos[Y] < 0
  or s_pos[X] > MAX_X - s_size[X]
  or s_pos[Y] > MAX_Y - s_size[Y] then
    position(2,1)
    puts( 1, "You crashed.  Press a key..." )
    if wait_key() then
      end_game()
    end if
  end if
end procedure

  --------------------
  -- main program loop
  --------------------
while 1 do
  keypressed = get_key()

  -- this if-block is just some debugging code
  if keypressed > 0 then
    position( 1, 1 )
    printf( 1, "key pressed: %d  ", keypressed )
  end if

  -- is input an arrow key?
  s_dir = find( keypressed, S_DIR )
  -- if so, change direction
  if s_dir then
    dxdy = DXDY[s_dir]
  -- if ESC pressed, then exit loop
  elsif keypressed = ESC then
    exit
  end if
  -- move snake
  s_pos += dxdy
  -- make sure we don't go off the screen
  bound_chk()
  display_image( s_pos, snake[2] )
  delay( DELAY )
end while

end_game()

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu