Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pac man type game code, need help fast !!!! 1

Status
Not open for further replies.
Nov 28, 2003
3
US
Problems: Player One won't stay in the confines of the grid
Player Two won't even show up on the grid
messages don't show up

If you can help me out reply please

STACKSEG SEGMENT PARA STACK 'STACK'
DB 32 DUP("STACK...")
STACKSEG ENDS
;
;
;
DATSEG SEGMENT PARA PUBLIC 'DATA'
grid db 0e0h, 00h, 00h, 00h, 00h, 20h,07h,0ffh,0f0h, 00h, 3ch, 00h, 01h, 00h,3ch
grid1 db 07h, 00h, 7fh, 0ffh,0e0h,05h,80h,40h, 01h, 20h, 04h, 80h, 40h, 01h,20h
grid3 db 0fch, 0e0h,40h, 01h, 20h, 04h,20h,4fh, 0ffh,20h, 04h, 20h, 48h, 04h,20h
grid4 db 00h, 3eh, 48h, 04h, 20h, 01h,02h,48h, 04h, 20h, 0ffh,03h, 0f8h,04h,7eh
grid5 db 20h, 02h, 88h, 07h, 0c2h,20h,02h,8fh, 84h, 42h, 3fh, 0feh,80h, 84h,42h
grid6 db 00h, 40h, 80h, 84h, 42h, 00h,40h,80h, 84h, 42h, 00h, 40h, 80h, 84h,42h
grid7 db 00h, 40h, 0bch,87h, 0c2h,3eh,7fh,0a4h,0f0h,02h, 22h, 40h, 0a4h,10h,02h
grid8 db 2ah, 40h, 0a4h,1fh, 02h, 2eh,40h,0fch,09h, 0feh,20h, 40h, 04h, 08h,20h
grid9 db 3fh, 0c0h,04h, 08h, 38h, 00h,00h,07h, 0f8h,08h, 00h, 00h, 00h, 10h,0fh

countfive db 0
msg1 db "Player One $", 52, 10, 0
db "Player Two $", 52, 14, 2

DATSEG ENDS
;
;
;
CODSEG SEGMENT PARA PUBLIC 'CODE'
ASSUME CS:CODSEG, SS:STACKSEG
MAIN PROC FAR
PUSH DS
XOR AX,AX
PUSH AX
MOV AX, DATSEG
MOV DS,AX
ASSUME DS:DATSEG

mov ax, 4f02h ;These three statements must be inserted in the program
mov bx, 108h ;in order to directly write to the video buffer
int 10h

call clearscreen



mov di,0
mov bx, 0

t1: mov al, [grid + di]
inc countfive
mov si, 0
t2: shl al, 1
jc co
mov ah, 00000000b
mov es:[bx],ax
jmp ov
co: mov ah, 01110111b ; white on white (i.e. white square)
mov es:[bx],ax ; dumps the ax register at offset position bx in video RAM
ov: add bx,2
inc si
cmp si,8
jne t2
; if at the end of processing 5 consecutive bytes from the grid array. then move bx
; to the beginning of the next line on the screen
cmp countfive,5
jne nxt
add bx, 80
mov countfive,0
nxt: inc di
cmp di,135
jne t1

mov bx, 01h
mov ah, 01110111b ; white on white (i.e. white square)
mov es:[bx],ax ; dumps the ax register at offset position bx in video RAM

mov ah,09 ; subfunction 9 output a string
mov dx,offset msg1 ; DX points to (holds the address of) the string
int 21h



;******************************************************************
;player one movement
;******************************************************************


mov ah, 01110111b ; Set up display attribute for a colored square
mov bx, 0 ; Sets up bx to point to offset position 0 in video RAM
mov es:[bx], ax

mov dx, 46h
mov ah, 01000111b
mov es:[bx], ax



top: mov ah, 0 ; get a keypress
int 16h
cmp al, 'q' ; If it is a q then game is over
je done
cmp ah, 01010101b ; If it is not a left arrow, check to see if it is right arrow
jne ov1
;----------------------- Keypress is a left arrow

; Before player moves left, make sure the offset postition in bx does not point to
; any character in the first column (i.e. it is evenly divisable by 160)

mov ax, bx
mov dl, 160
div dl ; Takes ax and divides it by dl. Remainder in the ah register
cmp ah, 0
cmp dx, 00000000b ; If no remainder after the divide by 160

je top ; Ignore the keypress and get another one

mov ah, 0 ; Writes a black square at player position
mov es:[bx], ax
sub bx,2 ; Writes a colored square to the left one position
mov ah, 01010101b
mov es:[bx],ax

jmp top ; Gets another keypress

ov1: cmp ah, 4dh ; If the keypress is not a right arrow
jne ov2 ; Check to see if it is an up arrow

;----------------------- Keypress is a right arrow

; Before player moves right, make sure the offset postition in bx does not point to
; any character in the 80th column (i.e. bx+2 is evenly divisable by 160)

mov ax, bx
add ax,2 ; Takes bx and adds 2 to it
mov dl, 160
div dl ; Takes ax and divides it by dl. Remainder in the ah register
cmp ah, 0 ; If no remainder after the divide by 160

je top ; Ignore the keypress and get another one

ov6: mov ah, 0 ; Writes a black square at player position
mov es:[bx], ax
add bx,2 ; Writes a colored square to the right one position
mov ah, 01010101b
mov es:[bx],ax

jmp top

ov2: cmp ah, 48h ; If the keypress is not an up arrow, check to see if it is a down arrow

jne ov3
;----------------------- Keypress is an up arrow

; Before player moves up, make sure the offset postition in bx does not point to
; any character in the first row (i.e. bx < 160)

cmp bx, 160
jb top ; Notice this is an unsigned jump b/c bx would be considered negative if it had
; any value more than 127 in it
mov ah, 0 ; Writes a black square at player position
mov es:[bx], ax
sub bx,160 ; Writes a colored square down one row
mov ah, 01010101b
mov es:[bx],ax

jmp top

ov3: cmp ah, 50h ; If the keypress is not the down arrow, then

jne top ; just ignore the keyypress
;----------------------- keypress is a down arrow

; Before player moves down, make sure the offset postition in bx does not point to
; any character in the 30th row (i.e. bx >= 4640)

cmp bx, 4640 ; bx is 4640 or greater if we are somewhere on the 30th line
jae top
mov ah, 0 ; Moves a black square at player position
mov es:[bx], ax
add bx,160 ; Writes a colored square up one row
mov ah, 01010101b
mov es:[bx],ax

jmp top

done: ret


;******************************************************************
;player one movement
;******************************************************************


mov al, 01111111b ; Set up display attribute for a colored square
mov bx, 4678 ; Sets up bx to point to offset position 0 in video RAM
mov es:[bx], ax

mov dx, 01111111b
mov al, 01000111b
mov es:[bx], ax



top10: mov al, 0 ; get a keypress
int 16h
cmp al, 'q' ; If it is a q then game is over
je done
cmp al, 6ah ; If it is not a left arrow, check to see if it is right arrow
cmp dx, 01110111b
jne ov11
;----------------------- Keypress is a left arrow

; Before player moves left, make sure the offset postition in bx does not point to
; any character in the first column (i.e. it is evenly divisable by 160)

mov ax, bx
mov dl, 160
div dl ; Takes ax and divides it by dl. Remainder in the ah register
cmp al, 0
cmp dx, 00000000b ; If no remainder after the divide by 160

je top10 ; Ignore the keypress and get another one

mov ah, 0 ; Writes a black square at player position
mov es:[bx], ax
sub bx,2 ; Writes a colored square to the left one position
mov al, 01010101b
mov es:[bx],ax

jmp top10 ; Gets another keypress

ov11: cmp al, 6ch ; If the keypress is not a right arrow
jne ov12 ; Check to see if it is an up arrow

;----------------------- Keypress is a right arrow

; Before player moves right, make sure the offset postition in bx does not point to
; any character in the 80th column (i.e. bx+2 is evenly divisable by 160)

mov ax, bx
add ax,2 ; Takes bx and adds 2 to it
mov dl, 160
div dl ; Takes ax and divides it by dl. Remainder in the ah register
cmp al, 0 ; If no remainder after the divide by 160

je top10 ; Ignore the keypress and get another one

ov16: mov al, 0 ; Writes a black square at player position
mov es:[bx], ax
add bx,2 ; Writes a colored square to the right one position
mov al, 01010101b
mov es:[bx],ax

jmp top10

ov12: cmp al, 69h ; If the keypress is not an up arrow, check to see if it is a down arrow

jne ov13
;----------------------- Keypress is an up arrow

; Before player moves up, make sure the offset postition in bx does not point to
; any character in the first row (i.e. bx < 160)

cmp bx, 160
jb top10 ; Notice this is an unsigned jump b/c bx would be considered negative if it had
; any value more than 127 in it
mov al, 0 ; Writes a black square at player position
mov es:[bx], ax
sub bx,160 ; Writes a colored square down one row
mov al, 01010101b
mov es:[bx],ax

jmp top10

ov13: cmp al, 6bh ; If the keypress is not the down arrow, then

jne top10 ; just ignore the keyypress
;----------------------- keypress is a down arrow

; Before player moves down, make sure the offset postition in bx does not point to
; any character in the 30th row (i.e. bx >= 4640)

cmp bx, 4640 ; bx is 4640 or greater if we are somewhere on the 30th line
jae top10
mov al, 0 ; Moves a black square at player position
mov es:[bx], ax
add bx,160 ; Writes a colored square up one row
mov al, 01010101b
mov es:[bx],ax

jmp top10



main endp
;****************************************************************
; Description: This procedure clears the screen
; Expects: nothing
; Changes: scrambles the ax, bx, cx registers
; Returns: Nothing
;****************************************************************
CLEARSCREEN PROC NEAR
mov ax, 0b800h ;points to beginning
mov es, ax
mov bx, 0 ; Sets bx to point to offset position 0 in video RAM
mov cx, 0 ; Cx will count the number of character written to the screen
mov ah, 07h ; Sets up ax to hold the values for a space
mov al, 20h ; White foreground on a black background, the character is a space


top4: mov es:[bx], ax ; Dumps a value into video RAM
inc bx ; Go to next position in video RAM
inc bx
inc cx
cmp cx, 5400 ; 60 rows x 80 characters in a row 5400 = 60*80
jne top4
ret

CLEARSCREEN ENDP
;*********************************************************************************************************************
;prints out &quot;player one&quot; and &quot;player two&quot;
;expects: nothing
;changes: AL, DX
;returns: nothing
;*********************************************************************************************************************
print_string PROC NEAR

PUSH dx ; store registers...
PUSH si ;

next_char:
MOV dx, [si]
CMP dx, 0
JZ printed
INC si
MOV dx, 0Eh ; teletype function.
JMP next_char
printed:

POP si ; re-store registers...
POP dX ;

RET
print_string ENDP




CODSEG ENDS
END MAIN

 
normalguy,

I'm not exactly sure this is the problem. But I have one question about your code.

Player one:
------------
top: mov ah, 0 ; get a keypress
int 16h

By using INT 16h, I think the the computer will *WAIT* until a player press something on the keyboard. In other words, it will not process another instruction!

Then after the comparison, finally you jump back to the &quot;top&quot; routine. So my question is when the player two will go into the process?

Correct me if I'm wrong. In this case I think you better read from the keyboard port directly and do the comparison for all key. Then you can decide where to jump (player 1 or 2)

-- AirCon --
 
well the purpose of that was to directly get to the keyboard the key to begin.. so your right the second player has to wait, which is one of the big problems, yes. Thank you, if you have any other ideas, please feel free to share them with me
 
Well, my idea is not to &quot;wait&quot; for the key but &quot;scan&quot; for the key, so both player can play at (almost) the same time.
This is just a skeleton. I can't help much with code cause i'm not a game coder also I can't do the test. Sorry if there is a mistake :)

Create a readkey routine. I'm using function 01h (AH=1) to check the keystroke.

-----------------
Readkey:
mov ah,1 ; ah=1, doesn't wait
int 16h ; check for the key
jz readkey ; no keystroke, scan next key

cmp al, P1_Key ; is the key for P1 ?
jne CheckP2_Key ; if not, check the key for P2
call P1_Move ; Key for P1, call P1 move routine
jmp Readkey ; read next key

CheckP2_Key:
cmp al, P2_Key ; is the key for P2 ?
jne CheckQuit_Key ; if not, check the key for Quit
call P2_Move ; Key for P2, call P2 move routine
jmp Readkey ; read next key

CheckQuit_Key:
cmp al, Quit_Key ; Quit ?
jne Readkey ; if not read next key
jmp Quit ; yes, return to main program or exit
-------------------

There is one thing about function 01. I think it doesn't clear the keyboard, you better check you documentation. Or you can look for a better routine


-- AirCon --
 
Can you use something like...
IN 60h
To read scancodes from the KB Port?

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
No, you don't need to use in, you can just use bios interrupts, but you need to choose the right one!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top