runner.cpp

来自「用SDL写的一个小游戏」· C++ 代码 · 共 64 行

CPP
64
字号
#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 + =
减小字号Ctrl + -
显示快捷键?