📄 simplepong.java
字号:
import java.applet.Applet;import java.awt.*;import java.awt.event.*;import java.lang.Math;import java.net.*;import java.awt.image.*;public class SimplePong extends Applet{ public void init() { //设置默认值:颜色为黑色,宽度为300,高度为80 //难度为5, 背景图为 backing.gif int bcRed = 0; int bcBlue = 0; int bcGreen = 0; int Width = 300; int Height = 80; int Difficulty = 5; String backimage = "backing.gif"; //检测用户输入的参数值 if(getParameter("Red")!=null) bcRed = new Integer(getParameter("Red").trim()).intValue(); if(getParameter("Blue")!=null) bcBlue = new Integer(getParameter("Blue").trim()).intValue(); if(getParameter("Green")!=null) bcGreen = new Integer(getParameter("Green").trim()).intValue(); if(getParameter("Width")!=null) Width = new Integer(getParameter("Width").trim()).intValue() -20; if(getParameter("Height")!=null) Height = new Integer(getParameter("Height").trim()).intValue()-20; if(getParameter("Difficulty")!=null) Difficulty = new Integer(getParameter("Difficulty").trim()).intValue(); if(getParameter("BackImage")!=null) backimage= getParameter("BackImage"); //设置参数 setBackground(new Color(bcRed,bcGreen,bcBlue)); Image ball = getImage(getCodeBase(), "ball.gif"); Image paddle1 = getImage(getCodeBase(),"panel1.gif"); Image paddle2 = getImage(getCodeBase(),"panel2.gif"); Image back = getImage(getCodeBase(),backimage); Image toplay = getImage(getCodeBase(),"Toplay.gif"); //使用正确的参数值创建一个壁球平台 PongTable pongtable = new PongTable(Width,Height,ball,paddle1,paddle2,back,toplay, Difficulty); setLayout(new BorderLayout()); Panel centredpanel = new Panel(); centredpanel.add(pongtable); add(centredpanel, "Center"); }}//创建一个PongTable类class PongTable extends Canvas implements Runnable, MouseMotionListener, MouseListener{ int Xsize, Ysize, Xball, Yball, Xbat, Ybat, XbatC, YbatC, difficulty; double direction[] = new double[2]; Thread thisthread; int Xbatdir[] = new int[2]; int Ybatdir[] = new int[2]; boolean Centred = true; Image ball, paddle1, paddle2, back, toplay; //构造函数 PongTable(int xsize, int ysize, Image Ball, Image Paddle1, Image Paddle2, Image Back, Image Toplay, int Difficulty) { //该方法对所有的变量进行设置 Toolkit toolkit = Toolkit.getDefaultToolkit(); addMouseMotionListener(this); addMouseListener(this); setSize(xsize,ysize); Xsize = xsize; Ysize = ysize; startpositions(); Xball = (int)Xsize/2; Yball = (int)Ysize/2; direction[0] = 0; direction[1] = 0; ball = Ball; paddle1 = Paddle1; paddle2 = Paddle2; back = Back; toplay = Toplay; difficulty = Difficulty; //启动线程 thisthread = new Thread(this); thisthread.start(); } void startpositions() { //指定开始位置 Xball = 20; Yball = 20; Xbat = 20; Ybat = 40; XbatC = Xsize -20; YbatC = 40; direction[0] = 4.0; //X 速度向量 direction[1] = 4.0; //Y 速度向量 } public void update(Graphics g) { //使用标准的双缓冲区技术更新数据 Graphics offgraph; Image offscreen = null; Dimension d = getSize(); offscreen = createImage(d.width, d.height); offgraph = offscreen.getGraphics(); offgraph.setColor(getBackground()); offgraph.fillRect(0, 0, d.width, d.height); offgraph.setColor(getForeground()); paint(offgraph); g.drawImage(offscreen, 0, 0, this); offscreen.flush(); } public void paint(Graphics g) { //画背景图 g.drawImage(back,5,5,Xsize-10,Ysize-10,this); //画用户的球拍 g.drawImage(paddle1,XbatC-2,YbatC-10,this); //画计算机的球拍 g.drawImage(paddle2,Xbat-2,Ybat-10,this); //画球 g.drawImage(ball,Xball-5, Yball-5,this); //如果没有人开始击球,就显示"click to play" if(Centred) g.drawImage(toplay,10, 10,this); } public void run() { double Xballdb, Yballdb; Xballdb = Xball; Yballdb = Yball; int n; while(true) { //在每次repaint()方法中,4次检测球体是否碰撞到边界 for(n=0;n<4;n++) { //用速度/4来增加X & Y Xballdb = Xballdb + direction[0]/4; Yballdb = Yballdb + direction[1]/4; //如果有任何一方没有打中球体,就说明游戏结束 if(Xballdb<10 || Xballdb>Xsize-10) { Xballdb = Xsize/2; Yballdb = Ysize/2; direction[0] = 0; direction[1] = 0; Centred = true; } //判断球体是否碰到上、下墙壁,如果发生碰撞,就将y调整为-y if(Yballdb<10 || Yballdb>Ysize-10) { direction[1]=-1*direction[1]; } //判断球体是否被用户的球拍所碰到 if(Xballdb>18 && Xballdb<27)//X 为球拍的厚度 //判断球体与球拍之间的y方向的距离小于10个象素 if(Math.abs(Yballdb-Ybat)<=10) { //如果是,那么设x = -x + x为球拍运动的x方向速度 //y = -y + y 为球拍运动的y方向速度 direction[0] = -1*direction[0]+(int)((Xbatdir[0]-Xbatdir[1])/8); direction[1] = direction[1]+(int)((Ybatdir[0]-Ybatdir[1])/8); //将球放置在球拍的前面 Xballdb=28; } //对计算机的球拍作同样的判断 if(Xballdb<(Xsize-15) && Xballdb>(Xsize-22)) if(Math.abs(Yballdb-YbatC)<=10) { //设x = -x - random . direction[0] = -1*direction[0] + -1*Math.random(); //y = -y + random no proportional to x speed //阻止球体向后运动,继续在相同的y方向上运动 direction[1] = direction[1] + (direction[0]/4)*Math.sin(Math.random()*2*Math.PI); //将球放置在球拍的前面 Xballdb=Xsize-23; } } //更新坐标值 Xball = (int)Xballdb; Yball = (int)Yballdb; //实现计算机AI if(Xball>Xsize/2) { //使用一个简单而实用的逻辑来控制球拍的运动: //首先判断球拍和球体不在同一高度; //然后在判断球体在球拍的上面还是下面 //最后根据难度参数决定球拍向上或者向下移动的象素值 if((Yball-YbatC)!=0) YbatC = (Yball-YbatC)/(Math.abs(Yball-YbatC))*difficulty + YbatC; } //重新调用paint()方法 repaint(); //让线程睡眠50 ms try { thisthread.sleep(50); } catch(Exception e) { System.out.println("Error on sleep"); } } } public void mouseMoved(MouseEvent e) { int x = e.getX(); int y = e.getY(); //实现球拍与鼠标一起运动, if(y>15 && y<Ysize-10) { Ybat = y; //设置球拍运动方向,以便用户可以影响球拍的运动方向 Xbatdir[1] = Xbatdir[0]; Ybatdir[1] = Ybatdir[0]; Xbatdir[0] = x; Ybatdir[0] = y; } repaint(); } public void mouseDragged(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { //检测是否开始新的游戏 if(Centred) { startpositions(); Centred = false; } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -