Fig. 2. PIC Shooter sprite definitions. Fig. 3. PIC shooter sprites (in the order defined from left to right). Unlike our previous programs, we have two initialization subroutines. Initial, seen in previous labs, initializes the port and LCD and is only called once when the PIC is turned on. We copied this from Experiment 1 then made sure pins D0 through D3 were set as input, as we will be using them to control the game. ‘GameInit’ initializes variables used by the game, such as the ship and enemy display strings, and prepares the LCD to be used by the game. GameInit is called every time the game restarts, which will be after a game over occurs (the GameOver subroutine will be discussed later). This guarantees the game begins with a fresh slate and a score of zero. The variables in GameInit will be discussed together with the subroutines which use them. Fig. 4. The GameInit subroutine with comments. Mainline for the game is extremely simple because of how modular the code is. The PIC is initialized, the sprites are loaded to the LCD, “PIC Shooter” is displayed on the LCD for 2 seconds, the game variables are initialized, then the main game loop starts. The main game loop is simply a call to DrawShip, which handles ship movement and shooting, DrawEnemy, which handles enemy generation and interaction, and Vsync. Vsync simply calls LoopTime twice (which is defined to wait for 10ms), resulting in a 20ms pause before the loop occurs again. We do this to slow down how many times we draw to the LCD because drawing too quickly will result in stuttering characters. If we refresh the display too slowly, characters on the display will appear to have a delayed response to the input. 20ms for Vsync was chosen subjectively through trial and error. After game over occurs, only GameInit will be called before entering the main game loop again. Fig. 5. Mainline for the GamePic program. Part 2: The DrawShip subroutine DrawShip handles drawing the ship sprite to the bottom row of the LCD. The position of the ship is determined by the rotary pulse generator and whether the ship is shooting or not is determined by the button being pressed. DrawShip will always clear the entire bottom row of the LCD before drawing the
Fig. 2. PIC Shooter sprite definitions. Fig. 3. PIC shooter sprites (in the order defined from left to right). Unlike our previous programs, we have two initialization subroutines. Initial, seen in previous labs, initializes the port and LCD and is only called once when the PIC is turned on. We copied this from Experiment 1 then made sure pins D0 through D3 were set as input, as we will be using them to control the game. ‘GameInit’ initializes variables used by the game, such as the ship and enemy display strings, and prepares the LCD to be used by the game. GameInit is called every time the game restarts, which will be after a game over occurs (the GameOver subroutine will be discussed later). This guarantees the game begins with a fresh slate and a score of zero. The variables in GameInit will be discussed together with the subroutines which use them. Fig. 4. The GameInit subroutine with comments. Mainline for the game is extremely simple because of how modular the code is. The PIC is initialized, the sprites are loaded to the LCD, “PIC Shooter” is displayed on the LCD for 2 seconds, the game variables are initialized, then the main game loop starts. The main game loop is simply a call to DrawShip, which handles ship movement and shooting, DrawEnemy, which handles enemy generation and interaction, and Vsync. Vsync simply calls LoopTime twice (which is defined to wait for 10ms), resulting in a 20ms pause before the loop occurs again. We do this to slow down how many times we draw to the LCD because drawing too quickly will result in stuttering characters. If we refresh the display too slowly, characters on the display will appear to have a delayed response to the input. 20ms for Vsync was chosen subjectively through trial and error. After game over occurs, only GameInit will be called before entering the main game loop again. Fig. 5. Mainline for the GamePic program. Part 2: The DrawShip subroutine DrawShip handles drawing the ship sprite to the bottom row of the LCD. The position of the ship is determined by the rotary pulse generator and whether the ship is shooting or not is determined by the button being pressed. DrawShip will always clear the entire bottom row of the LCD before drawing the
pic_shooter_report.docx