C++ (Qt)while(!asleep()) sheep++;
C++ (Qt)struct CLinearMove { CMove ( ... ) // обычное заполнение членов класса QPointF GetPosF( float curT ) const { curT = qMin(curT, mEndT); return (mBegPos * (mEndT - curT) + mEndPos * (curT - mBegT)) / (mEndT - mBegT); } QPoint GetPos( float curT ) const { QPoint temp = GetPosF(curT); return QPoint(int(temp.x() + 0.5f), int(temp.y() + 0.5f)); } QPointF mBegPos, mEndPos; float mBegT, mEndT; // время начала и конца};
void Player::movePlayer(bool go){ if(go) { const int deltaX = abs(mPlayerXDestPos - mPlayerXPos); const int deltaY = abs(mPlayerYDestPos - mPlayerYPos); const int signX = mPlayerXPos < mPlayerXDestPos ? mSpeedX : -mSpeedX; const int signY = mPlayerYPos < mPlayerYDestPos ? mSpeedY : -mSpeedY; int error = deltaX - deltaY; const int error2 = error * 2; if(error2 > -deltaY) { error -= deltaY; mPlayerXPos += signX; } if(error2 < deltaX) { error += deltaX; mPlayerYPos += signY; } } if(mPlayerXPos == mPlayerXDestPos || mPlayerYPos == mPlayerYDestPos) { mGo=false; }}
#include "window.h"#include <QPainter>#include <QMouseEvent>#include <QTime>#include "player.h"Window::Window() : m_width(320), m_height(240){ qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); startTimer(5); resize(m_width,m_height); player = new Player(width()/2,height()/2);}void Window::mousePressEvent(QMouseEvent *mouseEvent){ destPos = mouseEvent->posF(); player->Move(destPos);}void Window::timerEvent(QTimerEvent *){ update();}void Window::paintEvent(QPaintEvent*){ QPainter painter(this); player->Advance(&painter); painter.drawRect(player->x(),player->y(),player->width(),player->height());//}
#include "player.h"#include "item.h"#include <QtGui>Player::Player(float x, float y){ m_x=x; m_y=y; m_width=10; m_height=10; deltaX=0; deltaY=0; signX=0; signY=0; error=0; m_dest_x=0; m_dest_y=0; speed = 1;}void Player::Move(QPointF destPos){ m_dest_x = destPos.x(); m_dest_y = destPos.y(); deltaX = abs(m_dest_x - m_x); deltaY = abs(m_dest_y - m_y); signX = m_x < m_dest_x ? speed : -speed; signY = m_y < m_dest_y ? speed : -speed; error = deltaX - deltaY;}void Player::Advance(){ if(m_x != m_dest_x || m_y != m_dest_y) { const int error2 = error * 2; if(error2 > -deltaY) { error -= deltaY; m_x += signX; } if(error2 < deltaX) { error += deltaX; m_y += signY; } }}