-
[게임엔진활용] 8강 사과 게임 제작[DSU] 게임엔진활용 2026. 6. 8. 00:57728x90
사과 게임 제작
- 플래그값을 통한 게임씬 전환
- 버튼 제작
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403using System;using UnityEngine;public class GameScene : RenderView{int gameState;// 0:title, 1:inGamepublic override void load(){gameState = 0;loadTitle();}public override void free(){switch (gameState){case 0: freeTitle(); break;case 1: freeIngame(); break;}}public override void draw(float dt){switch (gameState){case 0: drawTitle(dt); break;case 1: drawIngame(dt); break;}}public override void mouse(int state, float x, float y){switch (gameState){case 0: mouseTitle(state, x, y); break;case 1: mouseIngame(state, x, y); break;}}// backgroud, button(play)Texture texLogo;struct Button{public Rect rt;public string s;public bool pressed;public Color cb;public Color ch;public Color cf;public float size;}Button btnPlay;void loadTitle(){texLogo = createImage("logo");Button b = new Button();b.rt = new Rect(10, 10, 400, 300);b.s = "Play";b.pressed = false;b.cb = Color.red;// 기본b.ch = Color.blue;// 커서b.cf = Color.white;b.size = 300;btnPlay = b;}void freeTitle(){}void drawButton(Button b){Rect rt = b.rt;Color c = b.pressed ? b.ch : b.cb;setRGBA(c.r, c.g, c.b, c.a);fillRect(rt.x, rt.y, rt.width, rt.height);setRGBA(1, 1, 1, 1);setStringName("굴림");float stringSize = b.size;setStringSize(stringSize);Vector2 size = sizeOfString(b.s);if (size.x > rt.width)setStringSize(stringSize * rt.width / size.x);setStringRGBA(b.cf.r, b.cf.g, b.cf.b, b.cf.a);drawString(b.s, rt.x + rt.width / 2,rt.y + rt.height / 2, VCENTER | HCENTER);}Image hero = null;void drawTitle(float dt){float rx = (float)mc.devWidth / texLogo.width;float ry = (float)mc.devHeight / texLogo.height;drawImage(texLogo, 0, 0, 0, 0, texLogo.width, texLogo.height,TOP | LEFT, rx, ry);drawButton(btnPlay);if (hero == null){hero = new Image(this);Texture[] texs = createImage(4, 1, "hero40");int[] idx = {1, 2, 3, 1, 2, 3, 0, 2, 3,1, 2, 3, 1, 2, 3, 1, 2, 3, 0, 2, 3,};for (int i = 0; i < idx.Length; i++)hero.add(texs[idx[i]]);hero._aniDt = 0.18f;hero.position = new Vector2(-72, -200);hero.startAnimation(true);}//for (int i = 0; i < 4; i++)//{// drawImage(texHero[i], 100 * i, 120 * i, TOP | LEFT);//}hero.paint(dt, new Vector2(100, 120));}public void mouseTitle(int state, float x, float y){switch (state){case 0:if (btnPlay.pressed){gameState = 1;freeTitle();loadIngame();}break;case 1:btnPlay.pressed = containPoint(x, y, btnPlay.rt);break;}}Button[] apple;//int score;AutoNumber score;float playTime, _playTime;ProgressBar progressBar;bool gameOver;Button[] btnResult;public static Color cGreen = new Color(0.1f, 0.5f, 0.0f, 1.0f);void loadIngame(){// 60 * 15float x = (mc.devWidth - 60 * 15) / 2;float y = (mc.devHeight - 60 * 10) / 2;apple = new Button[15 * 10];for (int i = 0; i < apple.Length; i++){Button b = new Button();b.s = "" + UnityEngine.Random.Range(1, 10);b.rt = new Rect(x + 60 * (i % 15), y + 60 * (i / 15), 50, 50);b.cb = cGreen;//Color.green;b.ch = Color.yellow;b.cf = Color.white;b.pressed = false;b.size = 30;apple[i] = b;}score = new AutoNumber(0);_playTime = 120f;playTime = 120f;progressBar = new ProgressBar(new Rect(x, y - 40, 950, 30));progressBar.bc = Color.clear;progressBar.fc = Color.red;progressBar.lineWidth = 2f;progressBar.curr = 0;progressBar.max = 2000;progressBar.rv = this;gameOver = false;btnResult = new Button[2];string[] s = new string[2] { "다시시작", "나가기" };for (int i = 0; i < btnResult.Length; i++){Button b = new Button();b.s = s[i];b.rt = new Rect(500 + 120 * i, 350, 100, 50);b.cb = Color.blue;b.ch = Color.black;b.cf = Color.white;b.pressed = false;b.size = 30;btnResult[i] = b;}}void freeIngame(){}void drawIngame(float dt){for (int i = 0; i < apple.Length; i++){if (apple[i].s == "0")continue;drawButton(apple[i]);}if (drag){setRGBA(0, 0, 1, 0.5f);Rect rt = dragRect();fillRect(rt.x, rt.y, rt.width, rt.height);setRGBA(1, 1, 1, 1);//setLineWidth(1);//drawRect(rt.x, rt.y, rt.width, rt.height);}#if falsesetRGBA(1, 0, 0, 1);setLineWidth(2);float x = (mc.devWidth - 60 * 15) / 2;float y = (mc.devHeight - 60 * 10) / 2;float w = 950;drawRect(x, y - 40, w, 30);w *= (float)score.get() / 2000;fillRect(x, y - 40, w, 30);setRGBA(1, 1, 1, 1);#else//progressBar.curr = score.get();progressBar.curr = score;progressBar.paint(Time.deltaTime);#endifsetStringName("고딕체");setStringSize(30);setStringRGBA(1, 0, 0, 1);score.update(Time.deltaTime);drawString("Score : " + score.get(), mc.devWidth - 10, 10, TOP | RIGHT);setRGBA(0, 1, 0, 1);float h = mc.devHeight - 100;float dh = h * playTime / _playTime;fillRect(mc.devWidth - 20, 50 + h - dh, 5, dh);drawRect(mc.devWidth - 20, 50, 5, h);setRGBA(1, 1, 1, 1);playTime -= dt;if (playTime <= 0f){gameOver = true;}if (gameOver){setStringName("고딕체");setStringSize(100);setStringRGBA(1, 0, 0, 1);drawString("Score : " + score, 400, 200, TOP | LEFT);for (int i = 0; i < btnResult.Length; i++)drawButton(btnResult[i]);}}bool drag = false;float sx, sy, ex, ey;Rect dragRect(){float x0, x1, y0, y1;if (sx < ex) { x0 = sx; x1 = ex; }else { x0 = ex; x1 = sx; }if (sy < ey) { y0 = sy; y1 = ey; }else { y0 = ey; y1 = sy; }return new Rect(x0, y0, x1 - x0 - 1, y1 - y0 - 1);}public void mouseIngame(int state, float x, float y){if (mouseGameOver(state, x, y))return;if (state == 0){sx = ex = x;sy = ey = y;drag = true;}else if (state == 2){drag = false;int sum = 0;for (int i = 0; i < apple.Length; i++){if (apple[i].pressed){int n = Int32.Parse(apple[i].s);sum += n;}}if (sum == 10){Debug.Log("제거하자");for (int i = 0; i < apple.Length; i++){if (apple[i].pressed){apple[i].s = "0";score.add(100);}}}else{for (int i = 0; i < apple.Length; i++){if (apple[i].pressed)apple[i].pressed = false;}}}else if (drag){ex = x;ey = y;Rect rt = dragRect();for (int i = 0; i < apple.Length; i++)apple[i].pressed = false;for (int i = 0; i < apple.Length; i++){if (containRect(rt, apple[i].rt)){if (apple[i].s != "0")apple[i].pressed = true;}}}}bool mouseGameOver(int state, float x, float y){if (gameOver == false)return false;switch (state){case 0:for (int i = 0; i < btnResult.Length; i++){if (btnResult[i].pressed){if (i == 0){// 다시시작for (int j = 0; j < apple.Length; j++){apple[j].s = "" + UnityEngine.Random.Range(1, 10);apple[j].pressed = false;}score.set(0);playTime = _playTime;gameOver = false;}else{// 나가기gameState = 0;freeIngame();loadTitle();}break;}}break;case 1:for (int i = 0; i < btnResult.Length; i++)btnResult[i].pressed = containPoint(x, y, btnResult[i].rt);break;}return true;}}cs - AutoNumber : 값이 단계를 거쳐 변화도록 구현
- 프로그래스바
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126using UnityEngine;public class AutoNumber{int number;int numberS, numberE;float delta, _delta;public AutoNumber(int v, float duration = 0.3f){number = v;_delta = duration;delta = duration;}// 객체가 같은지, 값이 같은지 구분public override bool Equals(object obj){return base.Equals(obj);}public override int GetHashCode(){return base.GetHashCode();}public static bool operator ==(AutoNumber a, AutoNumber b){return a.get() == b.get();}public static bool operator !=(AutoNumber a, AutoNumber b){return a.get() != b.get();}// int n = an;public static implicit operator int(AutoNumber p){return p.get();}// AutoNumber an = (AutoNumber)5;public static explicit operator AutoNumber(int n){return new AutoNumber(n);}public static AutoNumber operator +(AutoNumber a, AutoNumber b){AutoNumber an = new AutoNumber(a.number);an.add(b.number);return an;}public static AutoNumber operator +(AutoNumber a, int b){AutoNumber an = new AutoNumber(a.number);an.add(b);return an;}public static AutoNumber operator -(AutoNumber a, AutoNumber b){AutoNumber an = new AutoNumber(a.number);an.add(-b.number);return an;}public static AutoNumber operator -(AutoNumber a, int b){AutoNumber an = new AutoNumber(a.number);an.add(-b);return an;}// ++, --public static AutoNumber operator ++(AutoNumber a){a.add(1);return a;}public static AutoNumber operator --(AutoNumber a){a.add(-1);return a;}public void set(int v){number = v;}public void add(int v){//number += v;if (delta < _delta){numberS = get();numberE = numberE + v;}else{numberS = number;numberE = number + v;}delta = 0.0f;}public int get(){if( delta < _delta ){float r = delta / _delta;return (int)(numberS * (1.0f - r) + numberE * r);}return number;}public void update(float dt){if( delta < _delta){delta += dt;if( delta >= _delta ){delta = _delta;number = numberE;}}}}cs 
'[DSU] 게임엔진활용' 카테고리의 다른 글
[게임엔진활용] 10강 파티클 시스템 (0) 2026.06.08 [게임엔진활용] 9강 플랫포머 게임 제작 (0) 2026.06.08 [게임엔진활용] 7강 게임 시스템 구성 (0) 2026.06.08 [게임엔진활용] 6-2강 이미지 분할 및 애니메이션 (0) 2026.04.20 [게임엔진활용] 6-1강 operator (0) 2026.04.20

