文章目录
功能简介
准备材料
实物展示
代码实现
一、功能简介
本项目使用STM32F103C8T6单片机控制器,使用按键、IIC OLED模块等。
主要功能:
系统运行后,将代码烧录,按下reset复位键复位随后即可开始游戏,左键用于跳跃,右键用于开始。游戏右上角为本局游戏得分
本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。
二、准备材料
一块stm32f103c8t6芯片,两个按键,一块oled屏,一块stlingk转串口,几条杜邦线,一块面包板
三、实物展示
正在游戏中的画面
四、代码实现
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdlib.h>
#include "OLED.h"
#include "Key.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void OLED_Proc(void);
void Key_Proc(void);
//ؖ˽
uint8_t Grade_Count = 0;
uint16_t Grade = 0;
//Љɋֆ
uint16_t Cactus_CreatTime = 3000;
uint16_t Cactus_CreatTime_Multiplier = 1000;
uint8_t Cactus_CreatNumber = 0;
const int8_t Cactus_Length1 = 8;
int8_t Cactus_Position1 = 127;
uint8_t Cactus_Flag1 = 1;
const int8_t Cactus_Length2 = 16;
int8_t Cactus_Position2 = 127;
uint8_t Cactus_Flag2 = 1;
const int8_t Cactus_Length3 = 16;
int8_t Cactus_Position3 = 127;
uint8_t Cactus_Flag3 = 1;
uint16_t Cactus_Count = 0;
//Dino
uint8_t Height = 0;
uint8_t Dino_Flag = 0;
uint8_t Dino_Jump_Key = 0;
uint8_t Dino_Jump_Flag = 0;
uint8_t Dino_Jump_Flag_Flag = 0;
uint8_t Dino_Count = 0;
uint8_t Jump_FinishFlag = 0;
//Cloud
const uint8_t Cloud_Length = 27;
int8_t Cloud_Positon_1 = 100;
int8_t Cloud_Positon_2 = 0;
//Ground
uint8_t OLED_Slow = 0;
uint16_t Ground_Move_Number = 0;
uint8_t Speed = 3;
//Key
uint8_t Key_Slow = 0;
uint8_t Key_Value = 0;
uint8_t Key_Old = 0;
uint8_t Key_Down = 0;
uint8_t Key_Test = 0;
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
OLED_Init();
HAL_Delay(100);
OLED_Clear();
OLED_ShowGameBegin(0, 0, 127, 7);
while(1)
{
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15) == 0)
break;
}
OLED_Clear();
HAL_TIM_Base_Start_IT(&htim2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
OLED_Proc();
Key_Proc();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void OLED_Proc(void)
{
if(OLED_Slow) return;
OLED_Slow = 1;
OLED_ShowNum(1, 12, Grade, 5);
//֘Ħ
Ground_Move_Number += Speed; //لҤƫӆ
Ground_Move_Number %= 560; //
OLED_ShowGround(0, 6 , 127, 7, Ground_Move_Number); //Дʾ՚كƫӆЂք֘Ħ
//Ն1
OLED_ClearPicture(Cloud_Positon_1 + 1, 3, Cloud_Positon_1 + Cloud_Length - 1, 3); //лӰ
Cloud_Positon_1 -= (Speed - 1); //لҤՆפ1քλ׃
if(Cloud_Positon_1 < -27) //ɧڻӬԶǁĻسӠìղܘսǁĻԒӠ
Cloud_Positon_1 = 127; //
OLED_ShowCloud(Cloud_Positon_1, 3, Cloud_Positon_1 + Cloud_Length - 1, 3); //ДʾՆפ1
//Ն2
OLED_ClearPicture(Cloud_Positon_2 + 1, 2, Cloud_Positon_2 + Cloud_Length - 1, 2); //лӰ
Cloud_Positon_2 -= (Speed - 1); //لҤՆפ2քλ׃
if(Cloud_Positon_2 < -27) //ɧڻӬԶǁĻسӠìղܘսǁĻԒӠ
Cloud_Positon_2 = 127; //
OLED_ShowCloud(Cloud_Positon_2, 2, Cloud_Positon_2 + Cloud_Length - 1, 2); //ДʾՆפ2
//Сࠖº
if(Dino_Jump_Key == 0)
{
OELD_ShowDino(0, 5, 15, 6, Dino_Flag); //ДʾࠖºѼƜքۭĦ
}
else
{
if(Jump_FinishFlag == 1)
Jump_FinishFlag = 2;
OELD_ShowDino_Jump(0, 2, 15, 6, Dino_Jump_Flag); //ДʾࠖºǰքۭĦ
if(Jump_FinishFlag == 2 && Dino_Jump_Flag == 0)
{
Jump_FinishFlag = 0;
Dino_Jump_Key = 0;
}
}
//Љɋֆ1
if(Cactus_Flag1 == 0) //ʺӺЉɋֆ1քҪ־,ɴCactus_Flag1 = 0,ղʺԉЉɋֆ1
{
OLED_ClearPicture(Cactus_Position1 + 3, 5, Cactus_Position1 + Cactus_Length1 - 1, 6); //лӰ
Cactus_Position1 -= Speed; //вسӆ֯
if(Cactus_Position1 < -8) //ɧڻӬԶǁĻسӠ,ղܘսǁĻԒӠ,Ȓֹ
{ //һ֯ìһՓǁĻԒӠԶЖ
Cactus_Flag1 = 1; //
Cactus_Position1 = 127; //
} //
OLED_ShowCactus1(Cactus_Position1, 5, Cactus_Position1 + Cactus_Length1 - 1, 6); //ДʾЉɋֆ1
}
//Љɋֆ2
if(Cactus_Flag2 == 0) //ʺӺЉɋֆ2քҪ־,ɴCactus_Flag2 = 0,ղʺԉЉɋֆ2
{
OLED_ClearPicture(Cactus_Position2 + 8, 5, Cactus_Position2 + Cactus_Length2 - 1, 6); //лӰ
Cactus_Position2 -= Speed; //вسӆ֯
if(Cactus_Position2 < -16) //ɧڻӬԶǁĻسӠ,ղܘսǁĻԒӠ,Ȓֹ
{ //һ֯ìһՓǁĻԒӠԶЖ
Cactus_Flag2 = 1; //
Cactus_Position2 = 127; //
} //
OLED_ShowCactus2(Cactus_Position2, 5, Cactus_Position2 + Cactus_Length2 - 1, 6); //ДʾЉɋֆ2
}
//Љɋֆ3
if(Cactus_Flag3 == 0) //ʺӺЉɋֆ3քҪ־,ɴCactus_Flag3 = 0,ղʺԉЉɋֆ3
{
OLED_ClearPicture(Cactus_Position3 + 8, 6, Cactus_Position3 + Cactus_Length3 - 1, 6); //лӰ
Cactus_Position3 -= Speed; //вسӆ֯
if(Cactus_Position3 < -16) //ɧڻӬԶǁĻسӠ,ղܘսǁĻԒӠ,Ȓֹ
{ //һ֯ìһՓǁĻԒӠԶЖ
Cactus_Flag3 = 1; //
Cactus_Position3 = 127; //
} //
OLED_ShowCactus3(Cactus_Position3, 6, Cactus_Position3 + Cactus_Length3 - 1, 6); //ДʾЉɋֆ3
}
//Game Over
if(Cactus_Position3 + Cactus_Length3 - 1 <= 26 && Cactus_Position3 + Cactus_Length3 - 1 >= 0 && Height <= 6 || //Ɛ֨ˇرԥƶ
Cactus_Position2 + Cactus_Length2 - 1 <= 26 && Cactus_Position2 + Cactus_Length2 - 1 >= 0 && Height <= 14 || //սЉɋֆ
Cactus_Position1 + Cactus_Length1 - 1 <= 24 && Cactus_Position1 + Cactus_Length1 - 1 >= 0 && Height <= 14) //
{
while(1) //ԎϷޡ˸
{ //
HAL_Delay(1000); //
OLED_ShowGameOver(0, 0, 127, 7); //
} //
}
}
void Key_Proc(void)
{
if(Key_Slow) return;
Key_Slow = 1;
Key_Value = Get_KeyNumber();
Key_Down = Key_Value & (Key_Value ^ Key_Old);
Key_Old = Key_Value;
if(Key_Down == 12 && Dino_Jump_Key == 0)
{
Dino_Jump_Key = 1;
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim == &htim2)//1msɫһՎאؾϱگ˽
{
if(++OLED_Slow == 40) OLED_Slow = 0;
if(++Key_Slow == 10) Key_Slow = 0;
//ԦmСࠖºքѼƜԾ
Dino_Count++;
if(Dino_Count == 50)
{
Dino_Flag ^= 1;
if(Dino_Jump_Key == 1)
{
if(Dino_Jump_Flag_Flag == 0 && Jump_FinishFlag == 0)
{
Dino_Jump_Flag ++;
if(Dino_Jump_Flag == 8)
Dino_Jump_Flag_Flag = 1;
}
else if(Dino_Jump_Flag_Flag == 1)
{
Dino_Jump_Flag --;
if(Dino_Jump_Flag == 0)
{
Dino_Jump_Flag_Flag = 0;
Jump_FinishFlag = 1;
}
}
}
switch(Dino_Jump_Flag)
{
case 0:Height = 0; break;
case 1:Height = 6; break;
case 2:Height = 10;break;
case 3:Height = 15;break;
case 4:Height = 18;break;
case 5:Height = 21;break;
case 6:Height = 23;break;
case 7:Height = 25;break;
case 8:Height = 25;break;
}
Dino_Count = 0;
}
//̦ܺʺԉЉɋֆ
Cactus_Count++;
if(Cactus_Count >= Cactus_CreatTime)
{
Cactus_CreatTime = rand() % 3;
Cactus_CreatTime += 1;
Cactus_CreatTime *= Cactus_CreatTime_Multiplier;
Cactus_CreatNumber = rand() % 3;
switch(Cactus_CreatNumber)
{
case 0:
Cactus_Flag1 = 0;
break;
case 1:
Cactus_Flag2 = 0;
break;
case 2:
Cactus_Flag3 = 0;
break;
}
Cactus_Count= 0;
}
//ݓ̙
Grade_Count++;
if(Grade_Count == 200)
{
Grade ++;
if(Grade == 50)
Speed ++;
if(Grade == 100)
{
Speed ++;
Cactus_CreatTime_Multiplier = 500;
}
if(Grade == 150)
{
Speed ++;
Cactus_CreatTime_Multiplier = 800;
}
if(Grade == 200)
Speed ++;
Grade_Count = 0;
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
以上为开源代码主函数main.c文件内容,开源文件放在最后。
五、开源地址
源视频来自bilibili博主 “庚庚庚敏敏敏稚稚稚”
链接:https://pan.baidu.com/s/1Hrwn7aI6QnB11osurl4ylw?pwd=yuan
提取码:yuan