티스토리 뷰
테트리스 자작 소스코드
// SDrawingView.cpp : implementation of the CSDrawingView class
//
#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "SDrawing.h"
#endif
#include "SDrawingDoc.h"
#include "SDrawingView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
int x = 160; // 초기 블럭 나올때 좌표
int y = 100;
int w = 200;
int h = 120;
UINT htimer; // 타이머
int gap = 20; // 간격
int xy[7][10]; // 테트리스 칸
int mvblock[7][10]; //움직이는 블럭
int fixblock[7][10]; // 고정된 블럭
int mbblockX = 3; //블록 최초 위치 x 좌표
int mbblockY = 0; //블록 최초 위이 y 좌표
// CSDrawingView
IMPLEMENT_DYNCREATE(CSDrawingView, CView)
BEGIN_MESSAGE_MAP(CSDrawingView, CView)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_KEYDOWN()
ON_WM_TIMER()
END_MESSAGE_MAP()
// CSDrawingView construction/destruction
CSDrawingView::CSDrawingView()
: m_ptX(0)
, m_ptY(0)
, m_crColor(0)
,m_reRect(0,0,3000,3000)
{
// TODO: add construction code here
m_crColor = BLACK_BRUSH;
}
CSDrawingView::~CSDrawingView()
{
}
BOOL CSDrawingView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// CSDrawingView drawing
void CSDrawingView::OnDraw(CDC* pDC)
{
CSDrawingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
int i, j;
int tempi, tempj;
int beforePositionY, beforePositionH; //이전 위치
beforePositionY = y;
beforePositionH = h;
htimer = SetTimer(1, 500, NULL);//0.5초마다 블록이 떨어진다.
CPen redpen(PS_SOLID, 1, RGB(255, 0, 0)); // 벽 그리기
pDC->SelectObject(&redpen);
for (i = 0; i < 10; i++) //좌,우 벽 =3
{
xy[0][i] = 3;
pDC->Rectangle(100, 100 + (gap*i), 120, 120 + (gap*i)); //좌측 벽
xy[6][i] = 3;
pDC->Rectangle(220, 100 + (gap*i), 240, 120 + (gap*i)); //우측 벽
}
for (j = 0; j < 7; j++) //하단 벽 =3
{
xy[j][9] = 3;
pDC->Rectangle(100 + (gap*j), 280, 120 + (gap*j), 300); //하단 벽
}
for (i = 1; i < 9; i++) //빈공간
{
for (j = 1; j < 6;j++)
{
xy[j][i] = 0; //빈공간 = 0
}
}
CPen blackpen(PS_SOLID, 1, RGB(0, 0, 0)); // 블록 그림
pDC->SelectObject(&blackpen);
pDC->Rectangle(x, y, x + gap, y + gap);
mvblock[mbblockX][mbblockY] = 1; // 최초 위치 [3][0]
xy[mbblockX][mbblockY] = mvblock[mbblockX][mbblockY];
if (xy[mbblockX][mbblockY] == 3)
{
y = beforePositionY;
h = beforePositionH;
}
CClientDC dc(this);
CString strPoint;
for (i = 0; i < 7; i++)
{
for (j = 0; j < 10; j++)
{
strPoint.Format(_T("%d"), xy[i][j]);
dc.TextOutW(280 + (gap * i), 100 + (gap * j),strPoint);
}
}
tempi = mbblockX;
tempj = mbblockY;
CString strPoint2;
strPoint2.Format(_T("좌표 %d"), xy[tempi][++tempj]);
dc.TextOutW(0, 0, strPoint2);
}
void CSDrawingView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CSDrawingView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CSDrawingView diagnostics
#ifdef _DEBUG
void CSDrawingView::AssertValid() const
{
CView::AssertValid();
}
void CSDrawingView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSDrawingDoc* CSDrawingView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSDrawingDoc)));
return (CSDrawingDoc*)m_pDocument;
}
#endif //_DEBUG
// CSDrawingView message handlers
void CSDrawingView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
}
void CSDrawingView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
InvalidateRect(m_reRect);
CView::OnLButtonDown(nFlags, point);
}
void CSDrawingView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handwler code here and/or call default
int tempi, tempj, tempk;
CClientDC dc(this);
CString strPoint;
switch (nChar)
{
case VK_LEFT: //방향키 왼쪽이 눌러지면..
tempi = mbblockX;
tempj = mbblockY;
mbblockX -= 1;
if (xy[mbblockX][mbblockY]==0)
{
mvblock[mbblockX][mbblockY] = 1;
xy[tempi][tempj] = 0;
xy[mbblockX][mbblockY] = mvblock[mbblockX][mbblockY];
x -= gap;
w -= gap;
}
else if (xy[mbblockX][mbblockY] == 3)
{
mvblock[mbblockX][mbblockY] = 1;
xy[tempi][tempj] = 0;
xy[mbblockX][mbblockY] = mvblock[mbblockX][mbblockY];
}
strPoint.Format(_T("LEFT: %d"), xy[mbblockX][mbblockY]);
dc.TextOutW(100, 0, strPoint);
Invalidate();
break;
case VK_RIGHT: // 방향키 오른쪽이 눌러지면
x += gap;
w += gap;
tempi = mbblockX;
tempj = mbblockY;
mbblockX += 1;
mvblock[mbblockX][mbblockY] = 1;
xy[tempi][tempj] = 0;
xy[mbblockX][mbblockY] = mvblock[mbblockX][mbblockY];
Invalidate();
break;
case VK_DOWN:
y += gap;
h += gap;
tempi = mbblockX;
tempj = mbblockY;
mbblockY += 1;
mvblock[mbblockX][mbblockY] = 1;
xy[tempi][tempj] = 0;
xy[mbblockX][mbblockY] = mvblock[mbblockX][mbblockY];
Invalidate();
break;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CSDrawingView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
int i, j;
//숫자값으로 움직임 보기
CClientDC dc(this);
CString strPoint;
for (i = 0; i < 7; i++)
{
for (j = 0; j < 10; j++)
{
strPoint.Format(_T("%d"), xy[i][j]);
dc.TextOutW(280 + (gap * i), 100 + (gap * j), strPoint);
}
}
Invalidate();
CView::OnTimer(nIDEvent);
}