Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module cfunctions
- use, intrinsic :: iso_c_binding, only : c_int
- interface
- function c_rand() bind(c, name="rand")
- import c_int
- integer(c_int) c_rand
- end function c_rand
- end interface
- end module cfunctions
- subroutine IOHandle_Init()
- use, intrinsic :: iso_c_binding, only: c_null_char
- use :: m_ncurses
- integer :: rc
- stdscr = initscr()
- rc = raw()
- rc = nodelay(stdscr, true)
- rc = keypad(stdscr, true)
- rc = noecho()
- rc = curs_set(0)
- end subroutine IOHandle_Init
- subroutine IOHandle_Quit()
- use, intrinsic :: iso_c_binding, only: c_null_char
- use :: m_ncurses
- implicit none
- integer :: rc
- rc = endwin()
- end subroutine IOHandle_Quit
- subroutine ClearScreen()
- use, intrinsic :: iso_c_binding, only: c_null_char
- use :: m_ncurses
- integer :: i, rc
- do i = 0, LINES - 1
- rc = mvhline(i, 0, ichar(' ', 8), COLS)
- end do
- end subroutine ClearScreen
- function RandomRange(minimum, maximum)
- use :: cfunctions
- integer :: RandomRange
- integer :: minimum, maximum
- RandomRange = modulo(c_rand() + minimum, maximum)
- end function RandomRange
- function NewAppleLocation(boardSize, player)
- use :: cfunctions
- type Vec2_t
- integer :: x, y
- end type
- type(Vec2_t) :: NewAppleLocation
- type Player_t
- type(Vec2_t) :: pos
- integer :: direction
- type(Vec2_t), dimension(:), allocatable :: tail
- end type
- type(Player_t) :: player
- type(Vec2_t) :: test
- integer :: i ! iterator
- integer :: j ! iterator
- logical :: created = .false.
- logical :: valid
- ! generate an array of valid apple positions
- logical, dimension(boardSize.y, boardSize.x) :: available
- integer :: numberAvailable
- numberAvailable = boardSize%y * boardSize%x
- do i = 1, boardSize%y
- do j = 1, boardSize%x
- available(i, j) = .true.
- end do
- end do
- do i = 1, size(player%tail) ! remove taken places
- available(player%tail(i)%y, player%tail(i)%x) = .false.
- numberAvailable = numberAvailable - 1
- end do
- type(Vec2_t), dimension(numberAvailable) availablePositions
- type(Vec2_t) :: add
- integer :: posIndex = 1
- do i = 1, boardSize%y
- do j = 1, boardSize%x
- if (available(i, j)) then
- add%x = j
- add%y = i
- availablePositions(posIndex) = add
- posIndex = posIndex + 1
- end if
- end do
- end do
- NewAppleLocation = availablePositions(RandomRange(1, numberAvailable))
- end function NewAppleLocation
- program snake
- use, intrinsic :: iso_c_binding, only: c_null_char
- use :: m_ncurses
- use :: posix
- implicit none
- type Vec2_t
- integer :: x, y
- end type
- enum, bind(c)
- enumerator :: SnakeDirection_Up = 1
- enumerator :: SnakeDirection_Down
- enumerator :: SnakeDirection_Left
- enumerator :: SnakeDirection_Right
- enumerator :: SnakeDirection_None
- end enum
- type Player_t
- type(Vec2_t) :: pos
- integer :: direction
- type(Vec2_t), dimension(:), allocatable :: tail
- end type
- integer :: rc
- integer (kind = 16) :: input
- logical :: run = .true.
- type(Player_t) :: player
- integer :: sleepTime = 100 ! 1000/10 ms (10fps)
- integer :: i ! iterator
- type(Vec2_t) :: boardSize
- integer :: score = 0
- type(Vec2_t) :: apple
- player%pos%x = 5
- player%pos%y = 5
- player%direction = SnakeDirection_None
- allocate(player%tail(0))
- boardSize%x = 30
- boardSize%y = 15
- call IOHandle_Init
- do while (run)
- ! handle input
- input = getch()
- select case (input)
- case (KEY_RIGHT)
- player%direction = SnakeDirection_Right
- case (KEY_LEFT)
- player%direction = SnakeDirection_Left
- case (KEY_UP)
- player%direction = SnakeDirection_Up
- case (KEY_DOWN)
- player%direction = SnakeDirection_Down
- case (ichar('q'))
- run = .false.
- end select
- ! update player
- select case (player%direction)
- case (SnakeDirection_Right)
- player%pos%x = player%pos%x + 1
- case (SnakeDirection_Left)
- player%pos%x = player%pos%x - 1
- case (SnakeDirection_Down)
- player%pos%y = player%pos%y + 1
- case (SnakeDirection_Up)
- player%pos%y = player%pos%y - 1
- end select
- ! render
- call ClearScreen
- do i = 0, boardSize%y
- rc = mvhline(i, 0, ichar('.', 8), boardSize%x)
- end do
- rc = attron(A_REVERSE);
- rc = mvaddch(player%pos%y, player%pos%x, ichar('#', 8))
- rc = attroff(A_REVERSE);
- rc = refresh()
- rc = napms(sleepTime)
- end do
- deallocate(player%tail)
- call IOHandle_Quit
- print *, "Your score was", score
- end program snake
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement