반응형
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
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;
 
        int randnumber = 0;
        // 볼 사이즈와 컬러 구조체 선언
        Point[] ballPoint = new Point[missile_quantity];
        int sizePoint_x = 5;
        int sizePoint_y = 5;
        // 비행기컬러 , 비행기 사이즈 구조체 선언
        int ship_x = 300;
        int ship_y = 300;
        int shipsize = 25;
 
     
 
        // 볼 속도 선언
        int[] x_speed = new int[100];
        int[] y_speed = new int[100];
        
 
        public Form1()
        {
            InitializeComponent();
           
            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(-33);
                y_speed[i] = rs.Next(-33);
                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)
        {
         
            //비행기 그리는 소스
            Image ship = Bitmap.FromFile("ship.png"); // 이미지로 가져오는 소스
            e.Graphics.DrawImage(ship, ship_x, ship_y, shipsize, shipsize);
       
           
            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)
        {
            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;
            }
         
 
        }
        // 키보드 이동용 bool변수
        bool bleft = false, bright = false, bup = false, bdown = false;
 
        private void pictureBox1_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;
 
            
        }
 
        private void timer2_Tick(object sender, EventArgs e)
        {
            // 비행기 움직임
            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; }
 
            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

 

 

 

 

// 수정 코드

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
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;
 
        int randnumber = 0;
        // 볼 사이즈와 컬러 구조체 선언
        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 = 100;
 
        int gameover_x = 0;
        int gameover_y = 0;
        int gameover_size_x = 0;
        int gameover_size_y = 0;
 
        int gameState = 0;
 
        // 볼 속도 선언
        int[] x_speed = new int[100];
        int[] y_speed = new int[100];
        
 
        public Form1()
        {
            InitializeComponent();
            ship_x = (pictureBox1.Width / 2);
            ship_y = (pictureBox1.Height / 2);
 
            ready_x = (pictureBox1.Width/2- (readysize_x/2);
            ready_y = (pictureBox1.Height/2- (readysize_y /2);
 
            gameover_x = (pictureBox1.Width / 2- (gameover_size_x/2);
            gameover_y = (pictureBox1.Height / 2- (gameover_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(-33);
                y_speed[i] = rs.Next(-33);
                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);
            }
            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);
            }
           
            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;
                }
            }
 
        }
        // 키보드 이동용 bool변수
        bool bleft = false, bright = false, bup = false, bdown = false;
 
        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 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 == 0 && gameState == 3)
            {
                timer3.Stop();
                Watch.status = 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; }
            }
            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

// Timer 스톱워치 참고 자료

// http://baealex.tistory.com/89

 

반응형

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

C# - 활쏘기 게임  (0) 2018.08.31
C# - 닷지 게임 만들기 결과  (2) 2018.08.31
C# - 비행기 자유롭게 움직이기  (0) 2018.08.30
C# - 공튀기기 (particles.js)  (0) 2018.08.30
C# - 이벤트  (0) 2018.08.30

+ Recent posts