Sunteți pe pagina 1din 5

; ; ; ; ; ; ; ; ; ; ; ; ; ;

this is the screen eating snake game... this game pushes the emulator to its limits, and even with maximum speed it still runs slowly. to enjoy this game it's recommended to run it on real computer, however the emulator can be useful to debug tiny games and other similar programs such as this before they become bug-free and workable. you can control the snake using arrow keys on your keyboard. all other keys will stop the snake. press esc to exit.

name "snake" org 100h

; jump over data section: jmp start ; ------ data section -----s_size equ 7

; the snake coordinates ; (from head to tail) ; low byte is left, high byte ; is top - [top, left] snake dw s_size dup(0) tail dw ?

; direction constants ; (bios key codes): left equ 4bh right equ 4dh up equ 48h down equ 50h ; current snake direction: cur_dir db right wait_time dw 0

; welcome message msg db "==== how to play ====", 0dh,0ah db "this game was debugged on emu8086", 0dh,0ah db "but it is not designed to run on the emulator", 0dh,0ah db "because it requires relatively fast video card and cpu.", 0dh,0ah, 0 ah db "if you want to see how this game really works,", 0dh,0ah db "run it on a real computer (click external->run from the menu).", 0dh ,0ah, 0ah db "you can control the snake using arrow keys", 0dh,0ah db "all other keys will stop the snake.", 0dh,0ah, 0ah

db "press esc to exit.", 0dh,0ah db "====================", 0dh,0ah, 0ah db "press any key to start...$" ; ------ code section -----start: ; print welcome message: mov dx, offset msg mov ah, 9 int 21h ; wait for any key: mov ah, 00h int 16h ; hide mov mov mov int text ah, ch, cl, 10h cursor: 1 2bh 0bh

game_loop: ; === select first video page mov al, 0 ; page number. mov ah, 05h int 10h ; === show new head: mov dx, snake[0] ; set cursor at dl,dh mov ah, 02h int 10h ; print mov mov mov mov int '*' al, ah, bl, cx, 10h at the location: '*' 09h 0eh ; attribute. 1 ; single char.

; === keep the tail: mov ax, snake[s_size * 2 - 2] mov tail, ax call move_snake

; === hide old tail: mov dx, tail ; set cursor at dl,dh mov ah, 02h

int ; print mov mov mov mov int

10h ' ' al, ah, bl, cx, 10h at the location: ' ' 09h 0eh ; attribute. 1 ; single char.

check_for_key: ; === check for player commands: mov ah, 01h int 16h jz no_key mov int cmp je mov no_key: ah, 00h 16h al, 1bh ; esc - key? stop_game ; cur_dir, ah

; === wait a few moments here: ; get number of clock ticks ; (about 18 per second) ; since midnight into cx:dx mov ah, 00h int 1ah cmp dx, wait_time jb check_for_key add dx, 4 mov wait_time, dx

; === eternal game loop: jmp game_loop stop_game: ; show mov mov mov int ret ; ------ functions section -----; this procedure creates the cursor back: ah, 1 ch, 0bh cl, 0bh 10h

; ; ; ; ; ;

animation by moving all snake body parts one step to tail, the old tail goes away: [last part (tail)]-> goes away [part i] -> [part i+1] ....

move_snake proc near ; set es to bios info segment: mov ax, 40h mov es, ax ; point di to tail mov di, s_size * 2 - 2 ; move all body parts ; (last one simply goes away) mov cx, s_size-1 move_array: mov ax, snake[di-2] mov snake[di], ax sub di, 2 loop move_array cmp je cmp je cmp je cmp je jmp cur_dir, left move_left cur_dir, right move_right cur_dir, up move_up cur_dir, down move_down stop_move ; no direction.

move_left: mov al, b.snake[0] dec al mov b.snake[0], al cmp al, -1 jne stop_move mov al, es:[4ah] ; col number. dec al mov b.snake[0], al ; return to right. jmp stop_move move_right: mov al, b.snake[0] inc al mov b.snake[0], al cmp al, es:[4ah] ; col number. jb stop_move mov b.snake[0], 0 ; return to left. jmp stop_move move_up: mov al, b.snake[1] dec al

mov cmp jne mov mov jmp

b.snake[1], al al, -1 stop_move al, es:[84h] ; row number -1. b.snake[1], al ; return to bottom. stop_move

move_down: mov al, b.snake[1] inc al mov b.snake[1], al cmp al, es:[84h] ; row number -1. jbe stop_move mov b.snake[1], 0 ; return to top. jmp stop_move stop_move: ret move_snake endp

S-ar putea să vă placă și