⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 runner.cpp

📁 用SDL写的一个小游戏
💻 CPP
字号:
#include "Runner.h"

void CRunner::UpdateRunner()
{
	SDL_PumpEvents();
	Uint8 * key = SDL_GetKeyState(NULL);
    int stepx = RUNNERSPEED;
    int stepy = RUNNERSPEED;
	if (key[SDLK_LEFT])	
	{
		stepy = 0;
		stepx = -stepx;
	}
	else if (key[SDLK_RIGHT])
	{
		stepy = 0;
	}
	else if (key[SDLK_UP])
	{
		stepx = 0;
		stepy = -stepy;
	}
	else if (key[SDLK_DOWN])
	{
		stepx = 0;
	}
	else 
	{
		stepy = stepx = 0;
	}
	m_RunnerPos.x +=stepx;
	m_RunnerPos.y +=stepy;

	if(m_RunnerPos.x <= 0) 
		m_RunnerPos.x = RUNNERWIDTH;
	if(m_RunnerPos.y <= 0)
		m_RunnerPos.y = RUNNERWIDTH;
	if(m_RunnerPos.x >= SCREEN_WIDTH)
		m_RunnerPos.x = SCREEN_WIDTH - RUNNERWIDTH;
	if(m_RunnerPos.y >=SCREEN_HEIGHT) 
		m_RunnerPos.y = SCREEN_HEIGHT - RUNNERWIDTH;
}

void CRunner::DrawRunner(SDL_Surface* g_pDisplay)
{
	SDL_Rect Rect;

	Rect.h = RUNNERWIDTH;
	Rect.w = RUNNERWIDTH;
	Rect.x = m_RunnerPos.x;
	Rect.y = m_RunnerPos.y;

	SDL_FillRect(g_pDisplay,&Rect,m_RunnerColor);
}

void CRunner::CleanRunner(SDL_Surface * g_pDisplay)
{
	SDL_Rect Rect;
	Rect.h = RUNNERWIDTH;
	Rect.w = RUNNERWIDTH;
	Rect.x = m_RunnerPos.x;
	Rect.y = m_RunnerPos.y;
	SDL_FillRect(g_pDisplay,&Rect,0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -