各种曲线运动、弹球、笔记

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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// 绘制曲线
import java.awt.*;
 
public class BallGame extends Frame {
     
    Image img = Toolkit.getDefaultToolkit().getImage("pingpang.jpg"); // 这种写法的问题就是开始时静态图像不会显示,需要最小化或最大化后才能显示
    double x = 200;
    double y = 200;
    double degree = 0;
    public void paint(Graphics g) // 自动调用, g相当于画笔
    {
            System.out.println("窗口被画了一次");
            g.drawImage(img, (int)x, (int)y, null);
            // 椭圆
            /*x = 250 + 200*Math.cos(degree);
            y = 250 + 100*Math.sin(degree);*/
             
            // 正弦曲线
            x = x + 2;
            y = 200 + 100*Math.sin(degree);
            degree = degree + 0.1;
             
         
// g.drawImage(img, 100, 100, null);
         
        /*g.setColor(Color.GREEN);
        g.drawLine(100, 100, 200, 200);
        g.drawRect(50, 50, 100, 80);
        g.drawOval(50, 50, 100, 80);
        g.setColor(Color.YELLOW);
        g.drawString("游戏开始!", 80, 80);*/
    }
    void launchFrame()
    {
        setSize(500, 300);
        setLocation(50, 50);
        setTitle("hallo ball");
        setBackground(Color.blue);
        setVisible(true);
        new PaintThread().start();
    }
     
     
    public static void main(String[] args)
    {
        new BallGame().launchFrame();
         
    }
     
    class PaintThread extends Thread
    {
        public void run()
        {
            while(true)
            {
                repaint(); // 重画窗口!
                try
                {
                    Thread.sleep(40); // 40MS, 1S画25次
                 
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                 
            }
        }
    }
}
//------------------------------我是分割线---------------------------------------------------------------
弹球:
import java.awt.*;
 
public class BallGame extends Frame {
     
    Image img = Toolkit.getDefaultToolkit().getImage("pingpang.png"); // 这种写法的问题就是开始时静态图像不会显示,需要最小化或最大化后才能显示
    double x = 100;
    double y = 100;
    double degree = 3.14 / 3;
    public void paint(Graphics g) // 自动调用, g相当于画笔
    {
            System.out.println("窗口被画了一次");
            g.drawImage(img, (int)x, (int)y, null);
             
            if(y > 300 - 30)
            {
                degree = -degree;
            }
            else if(y < 30)
            {
                degree = - degree;
            }
            else if(x > 500 - 30)
            {
                degree = 3.14 - degree;
            }
            else if(x < 0)
            {
                degree = 3.14 - degree;
            }
             
            x = x + 10*Math.cos(degree);
            y = y + 10*Math.sin(degree);
                 
         
//        g.drawImage(img, 100, 100, null);
         
        /*g.setColor(Color.GREEN);
        g.drawLine(100, 100, 200, 200);
        g.drawRect(50, 50, 100, 80);
        g.drawOval(50, 50, 100, 80);
        g.setColor(Color.YELLOW);
        g.drawString("游戏开始!", 80, 80);*/
    }
    void launchFrame()
    {
        setSize(500, 300);
        setLocation(50, 50);
        setTitle("hallo ball");
        setBackground(Color.blue);
        setVisible(true);
        new PaintThread().start();
    }
     
     
    public static void main(String[] args)
    {
        new BallGame().launchFrame();
         
    }
     
    class PaintThread extends Thread
    {
        public void run()
        {
            while(true)
            {
                repaint(); // 重画窗口!
                try
                {
//                    Thread.sleep(40); // 40MS, 1S画25次
                    Thread.sleep(40);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                 
            }
        }
    }
}
//----------------------------我是分割线--------------------------------------------------
笔记:
1.JAVA开发环境安装和配置
a)下载JDK(Java Development Kit)
b)安装JDK、 JRE(Java Runtime environment)
c)配置环境变量:
i.path(系统通过path指定的目录寻找相关的可执行文件!)
右键单击我的电脑属性高级环境变量修改path即可!
ii.classpath(系统通过classpath寻找类文件)
JDK1.5之后,可以不设置!
2.Eclipse的使用(目前使用最多的IDE:integrated development environment集成开发环境)
a)下载
b)解压即可
3.新建一个JAVA项目
a)New Java Project只需要输入project name,回车即可!
4.新建一个JAVA类
a)右键单击srcnew Class键入类名:BallGame
b)输入代码:
public class BallGame {
    public static void main(String[] args){
        System.out.println("我是尚学堂的高淇!");
    }
}
c)运行java程序的三种方式:
i.右键单击Run asjava application
ii.Alt+shift+X,再按J
iii.Ctrl+F11
iv.单击运行按钮
 
 
开始开发我们的小游戏
1.增加窗口
import java.awt.*;
public class BallGame extends Frame {
     
    void launchFrame(){
        setSize(500, 300);
        setLocation(50, 50);
        setVisible(true);
        setTitle("尚学堂---张三作品");
        setBackground(Color.black);
    }
     
    public static void main(String[] args){
        System.out.println("我是尚学堂的高淇!");
        new BallGame().launchFrame();
    }
}
 
2.加载图片
在项目下新建images文件夹,将sun.jpg拷贝到images下面
 
import java.awt.*;
 
public class BallGame extends Frame {
     
    Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的!
     
    public void paint(Graphics g){
        g.drawImage(sun, 100, 100, null);
    }
     
    void launchFrame(){
        setSize(500, 300);
        setLocation(50, 50);
        setTitle("尚学堂---张三作品");
        setBackground(Color.black);
        setVisible(true);
    }
     
    public static void main(String[] args){
        System.out.println("我是尚学堂的高淇!");
        new BallGame().launchFrame();
    }
}
注意:运行时,第一次打开窗口看不到图片。需要将窗口最小化再打开即可看到!
 
3.学习画各种曲线、形状、字符串:
 
import java.awt.*;
 
public class BallGame extends Frame {
     
    Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的!
     
    public void paint(Graphics g){
        g.drawImage(sun, 100, 100, null);
        g.setColor(Color.blue);
        g.drawLine(100, 100, 200, 200);
        g.drawRect(50, 50, 100, 80);
        g.drawOval(50, 50, 100, 80);
        g.setColor(Color.yellow);
        g.drawString("游戏开始啦!!", 80, 80);
[暂时跟我们的游戏无关,但是一些基本的编程知识!]    }
     
    void launchFrame(){
        setSize(500, 300);
        setLocation(50, 50);
        setTitle("尚学堂---张三作品");
        setBackground(Color.black);
        setVisible(true);
    }
     
    public static void main(String[] args){
        System.out.println("我是尚学堂的高淇!");
        new BallGame().launchFrame();
    }
}
 
4.增加动画
 
import java.awt.*;
 
public class BallGame extends Frame {
     
    Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的!
    int x=100;
    int y=100;
    public void paint(Graphics g){
        System.out.println("窗口被画了一次!");
        g.drawImage(sun, x, y, null);
        x = x+2;
    }
     
    void launchFrame(){
        setSize(500, 300);
        setLocation(50, 50);
        setTitle("尚学堂---张三作品");
        setBackground(Color.black);
        setVisible(true);
        new PaintThread().start();
    }
     
    public static void main(String[] args){
        System.out.println("我是尚学堂的高淇!");
        new BallGame().launchFrame();
    }
 
    class PaintThread extends Thread {
        public void run(){
            while(true){
                repaint(); //重画窗口!
                try{
                    Thread.sleep(40); //40ms 1s=1000ms
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
5.通过我们掌握的数学函数(抛物线、正弦曲线、椭圆等),控制游戏中物体的运动!
 
import java.awt.*;
 
public class BallGame extends Frame {
     
    Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的!
    double x=100;
    double y=100;
    double degree = 0;
    public void paint(Graphics g){
        System.out.println("窗口被画了一次!");
        g.drawImage(sun, (int)x,(int)y, null);
        x =150+ 100*Math.cos(degree);
        y = 150+100*Math.sin(degree);
        degree = degree + 0.1;
    }
     
    void launchFrame(){
        setSize(500, 300);
        setLocation(50, 50);
        setTitle("尚学堂---张三作品");
        setBackground(Color.black);
        setVisible(true);
        new PaintThread().start();
    }
     
    public static void main(String[] args){
        System.out.println("我是尚学堂的高淇!");
        new BallGame().launchFrame();
    }
 
    class PaintThread extends Thread {
        public void run(){
            while(true){
                repaint(); //重画窗口!
                try{
                    Thread.sleep(40); //40ms 1s=1000ms
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
6.实现台球的运动!
 
import java.awt.*;
 
public class BallGame extends Frame {
     
    Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg"); //这种写法并不好,但是是最简单的!
    double x=100;
    double y=100;
    double degree = 3.14/3;
    public void paint(Graphics g){
        System.out.println("窗口被画了一次!");
        g.drawImage(sun, (int)x,(int)y, null);
        x = x+ 10*Math.cos(degree);
        y = y+10*Math.sin(degree);
         
        if(y>300-30){
            degree = - degree;
        }
        if(x>500-30){
            degree = 3.14-degree;
        }
        if(x<0){
            degree = 3.14-degree;
        }
        if(y<30){
            degree = -degree;
        }
    }
     
    void launchFrame(){
        setSize(500, 300);
        setLocation(50, 50);
        setTitle("尚学堂---张三作品");
        setBackground(Color.black);
        setVisible(true);
        new PaintThread().start();
    }
     
    public static void main(String[] args){
        System.out.println("我是尚学堂的高淇!");
        new BallGame().launchFrame();
    }
 
    class PaintThread extends Thread {
        public void run(){
            while(true){
                repaint(); //重画窗口!
                try{
                    Thread.sleep(40); //40ms 1s=1000ms
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
7.通过键盘来控制物体的运动!

参考:尚学堂高淇游戏视频

posted @   Code_Rush  阅读(854)  评论(0编辑  收藏  举报
编辑推荐:
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
阅读排行:
· dotnet 源代码生成器分析器入门
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 一步一步教你部署ktransformers,大内存单显卡用上Deepseek-R1
· 一次Java后端服务间歇性响应慢的问题排查记录
点击右上角即可分享
微信分享提示