반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
 
namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        Random rs = new Random(); // 랜덤 함수 출력
                                  // 볼 갯수 
        const int missile_quantity = 100;
 
        
        // 볼 사이즈와 컬러 구조체 선언
        Point[] ballPoint = new Point[missile_quantity];
        int sizePoint_x = 5;
        int sizePoint_y = 5;
        // 비행기컬러 , 비행기 사이즈 구조체 선언
        int ship_x = 0;
        int ship_y = 0;
        int shipsize = 25;
 
        int ready_x = 0;
        int ready_y = 0;
        int readysize_x = 200;
        int readysize_y = 200;
 
        int gameover_x = 0;
        int gameover_y = 0;
        int gameover_size_x = 200;
        int gameover_size_y = 200;
 
        int space_x = 0;
        int space_y = 0;
        int space_size_x = 200;
        int space_size_y = 200;
 
        int gameState = 0;
        
        // 볼 속도 선언
        int[] x_speed = new int[100];
        int[] y_speed = new int[100];
 
        // 위치판단
        Rectangle rt = new Rectangle();
 
        public Form1()
        {
            InitializeComponent();
 
            rt.X = ship_x;
            rt.Y = ship_y;
            rt.Width = shipsize;
            rt.Height = shipsize;
 
 
            ship_x = (pictureBox1.Width / 2);
            ship_y = (pictureBox1.Height / 2);
 
            ready_x = (pictureBox1.Width/2- (readysize_x/2);
            ready_y = (pictureBox1.Height*1/3- (readysize_y /2);
 
            gameover_x = (pictureBox1.Width / 2- (gameover_size_x/2);
            gameover_y = (pictureBox1.Height / 2- (gameover_size_x / 2);
 
            space_x = (pictureBox1.Width / 2- (space_size_x / 2);
            space_y = (pictureBox1.Height*2 / 3- (space_size_y / 2);
 
            for (int i = 0; i < missile_quantity; i++)
            {
                ballPoint[i].X = rs.Next(0, pictureBox1.Width);
                ballPoint[i].Y = rs.Next(0, pictureBox1.Height);
                x_speed[i] = rs.Next(-22);
                y_speed[i] = rs.Next(-22);
                if (x_speed[i] == 0)
                    x_speed[i] = 1;
                else if (y_speed[i] == 0)
                    y_speed[i] = 1;
            }
        }
 
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
 
            if (gameState == 0)
            {
                Image ready = Bitmap.FromFile("ready.png"); // 이미지로 가져오는 소스
                e.Graphics.DrawImage(ready, ready_x, ready_y, readysize_x, readysize_y);
 
                Image space = Bitmap.FromFile("space.png"); // 이미지로 가져오는 소스
                e.Graphics.DrawImage(space, space_x, space_y, space_size_x, space_size_y);
 
            }
            else if (gameState == 3)
            {
                Image gameover = Bitmap.FromFile("gameover.png"); // 이미지로 가져오는 소스
                e.Graphics.DrawImage(gameover, gameover_x, gameover_y, gameover_size_x, gameover_size_y);
            }
            else if (gameState == 1)
            {
                //비행기 그리는 소스
                Image ship = Bitmap.FromFile("ship.png"); // 이미지로 가져오는 소스
                e.Graphics.DrawImage(ship, ship_x, ship_y, shipsize, shipsize);
                //
               // e.Graphics.FillRectangle(Brushes.Orange, rt);
            }
           
            for (int i = 0; i < missile_quantity; i++)
            {
                if (i % 2 == 0) e.Graphics.FillEllipse(Brushes.Blue,ballPoint[i].X,ballPoint[i  ].Y, sizePoint_x, sizePoint_y);
                else e.Graphics.FillEllipse(Brushes.Purple,ballPoint[i].X,ballPoint[i].Y, sizePoint_x, sizePoint_y);
 
            }
 
 
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (gameState == 1)
            {
                for (int i = 0; i < missile_quantity; i++)
                {
                    // 볼을 이동시키는 소스
                    ballPoint[i].X += x_speed[i];
                    ballPoint[i].Y += y_speed[i];
                    pictureBox1.Invalidate(); // 화면 갱신
 
                    // 벽을 만나면 튕기는 소스
                    if (ballPoint[i].X < 0) x_speed[i] *= -1;
                    if (ballPoint[i].X > pictureBox1.Width) x_speed[i] *= -1;
                    if (ballPoint[i].Y < 0) y_speed[i] *= -1;
                    if (ballPoint[i].Y > pictureBox1.Height) y_speed[i] *= -1;
                }
                
            }
 
        }
        
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }
  
        class Watch
        {
            static public int milisec = 0;
            static public int sec = 0;
 
            static public bool status = false;
        }
        
        private void timer3_Tick(object sender, EventArgs e)
        {
            Watch.milisec++;
            if (Watch.milisec > 59)
            {
                Watch.milisec = 0;
                Watch.sec++;
                if (Watch.sec > 59)
                    Watch.sec = 0;
            }
            textBox1.Text = Watch.sec + ":" + Watch.milisec+"초";
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        private void Form1_Click(object sender, EventArgs e)
        {
 
        }
 
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            //키코드를 뗄 때 입력받는 함수
           
                if (e.KeyCode == Keys.Right) bright = false;
                if (e.KeyCode == Keys.Left) bleft = false;
                if (e.KeyCode == Keys.Down) bdown = false;
                if (e.KeyCode == Keys.Up) bup = false;
 
            if (e.KeyCode == Keys.Space) gameState = 1;
               
                 
               
                
 
            
                //
            if (Watch.status == false && gameState == 1)
            {
                timer3.Start();
                timer3.Interval = 10;
                Watch.status = true;
            }
            else if (Watch.status == true && gameState == 3)
            {
                timer3.Stop();
                Watch.status = false;
                Watch.milisec = 0;
                Watch.sec = 0;
                gameState = 0;
                
            }
            else if (gameState == 0)
            {
                Watch.milisec = 0;
                Watch.sec = 0;
                Watch.status = false;
                textBox1.Text = Watch.sec + ":" + Watch.milisec +"초";
                timer3.Stop();
            }
           
        }
        // 키보드 이동용 bool변수
        bool bleft = false, bright = false, bup = false, bdown = false, bspace = false;
 
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (gameState == 1)
            {
 
                // 비행기 움직임
                if (bright) { if (ship_x < pictureBox1.Width - 30) ship_x += 5; }
                if (bleft) { if (ship_x > 0) ship_x -= 5; }
                if (bup) { if (ship_y > 0) ship_y -= 5; }
                if (bdown) { if (ship_y < pictureBox1.Height - 30) ship_y += 5; }
 
                for (int i = 0; i < missile_quantity; i++)
                {
                    if (rt.Contains(ballPoint[i].X  , ballPoint[i].Y) == true)
                    {
                        gameState = 3;
                        textBox1.Text = Watch.sec + ":" + Watch.milisec + "초";
                        timer3.Stop();
                        MessageBox.Show("기록 : "+Watch.sec + ":" + Watch.milisec + "초");
                        break;
 
                    }
                }
            }
            else if (gameState == 3)
            {
                Watch.status = false;
                bleft = false;
                bright = false;
                bup = false;
                bdown = false;
                if (bspace) gameState = 0;
                for (int i =0; i<missile_quantity; i++)
                {
                    ballPoint[i].X += x_speed[i];
                    ballPoint[i].Y += y_speed[i];
                    pictureBox1.Invalidate();
                }
            }
            else if (gameState == 0)
            {
                Watch.status = false;
               
            }
            pictureBox1.Invalidate();
 
        }
 
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
 
             // 키코드를 누를 때 입력 받는 함수
            if (e.KeyCode == Keys.Right) bright = true;
            if (e.KeyCode == Keys.Left) bleft = true;
            if (e.KeyCode == Keys.Down) bdown = true;
            if (e.KeyCode == Keys.Up) bup = true;
            
 
        }
    }
 
    
    
}
 
cs

 

 

 

 

WindowsFormsApp3.zip

WindowsFormsApp3.sln

 

반응형

'Study > C#' 카테고리의 다른 글

C# 메모장 프로그램  (0) 2021.01.22
C# - 활쏘기 게임  (0) 2018.08.31
C# - 닷지 게임 만들기  (0) 2018.08.30
C# - 비행기 자유롭게 움직이기  (0) 2018.08.30
C# - 공튀기기 (particles.js)  (0) 2018.08.30

+ Recent posts