The first stage: JAVA Quick Start (Lesson 21: 37 minutes zero-based development projects billiard game)

[Project] snooker small projects

Exercise objectives:

      1. Locate the knock code feeling

      2. Interest harvest knock code

      3. To make the effect, find confidence

      4. From the outset Society debugging error

      5. master the basic structure of Java code

Project requirements:

      Snooker move in certain lines and angles in the pool table, the border will be encountered automatically spring back.

Claim:

      Even I do not understand, but also shining knock following the game code, at least 5 times. Requires that all the characters and the same source file. If the reported abnormal, please carefully look at what is the difference row and teacher code. At this stage we do not need to understand the syntax of the function code, simply enter the code in accordance with the structure of the code, debugging can be achieved through normal operation to the code.

Used in the project two small pictures, as follows:

ball.png

Ball picture above, billiard table picture is as follows:

desk.jpg

 

Development steps:

version 1:

    Goal: Create a project, create the window.

     Create a project and a copy of the picture:  

        On the project name MyPro01 Right-click, select new --- Folder in the menu to create a name images folder and copy two pictures to the directory. And create a class BallGame.java in src. Eclipse the final structure shown in Figure 1-40 project.

QQ picture 20170516170142.png

Figure 1-40 Snooker project structure

  Code:

import  java.awt.*;
import javax.swing.*;
  
public class BallGame extends JFrame {//继承JFrame也就是窗口的类
      
      
     //窗口加载
     void launchFrame(){
         setSize(300,300);//窗口大小
         setLocation(400,400);//起始位置
         setVisible(true);//可视
          
     }
      
     //main方法是程序执行的入口
     public static void main(String[] args){
         System.out.println(" 成功运行!");
         BallGame game = new BallGame();//类的创建
         game.launchFrame();//调用类方法
     }
      
}

running result:

1.png

 

Version 2:

    Goal: to load two pictures

    Code:

import  java.awt.*;
import javax.swing.*;
 
public class BallGame extends JFrame {
     
    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.jpg");
     
    double  x=100;    //小球的横坐标
    double  y=100; //小球的纵坐标
    //画窗口的方法
    public void paint(Graphics  g){
        System.out.println("窗口被画了一次!");
        g.drawImage(desk, 0, 0, null);
        g.drawImage(ball, (int)x, (int)y, null);
    }
     
    //窗口加载
    void launchFrame(){
        setSize(856,500);
        setLocation(50,50);
        setVisible(true);
    }
     
    //main方法是程序执行的入口
    public static void main(String[] args){
        System.out.println(" 我是尚学堂高淇,这个游戏项目让大家体验编程的快感,寓教于乐!");
        BallGame game = new BallGame();
        game.launchFrame();
    }
     
}

running result:

    2.png

 

note:

    As the lazy loading problem, there may be cases for the first time Load picture appears invalid; please minimize the window to open again (see the instructions on the video). We completed the next version of the animation, it is entirely absent this problem.

 

Version 3:

    Objective: To achieve the animation, the ball moves in a horizontal direction and make boundary detection

    Code:

import  java.awt.*;
import javax.swing.*;
 
public class BallGame extends JFrame {
     
    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.jpg");
     
    double  x=100;    //小球的横坐标
    double  y=100; //小球的纵坐标
    boolean  right = true;   //方向
    //画窗口的方法
    public void paint(Graphics  g){
        System.out.println("窗口被画了一次!");
        g.drawImage(desk, 0, 0, null);
        g.drawImage(ball, (int)x, (int)y, null);
         
         
        if(right){//为真的就一直右跑,假的话回弹
            x = x +10;            
        }else{
            x = x - 10;
        }
         
        if(x>856-40-30){    //856是窗口宽度,40是桌子边框的宽度,30是小球的直径
            right = false;//如果
        }
         
        if(x<40){        //40是桌子边框的宽度
            right = true;
        }
 
         
    }
     
    //窗口加载
    void launchFrame(){
        setSize(856,500);
        setLocation(50,50);
        setVisible(true);
         
        //重画窗口,每秒画25次
        while(true){//死循环一直让球跑起来
            repaint(); 
            try{//抛出异常,异常后续会说到!
                Thread.sleep(40);   //40ms,   1秒=1000毫秒.  大约一秒画25次窗口
            }catch(Exception e){
                e.printStackTrace();
            }
             
        }
         
    }
     
    //main方法是程序执行的入口
    public static void main(String[] args){
        System.out.println(" 我是赵广陆,成功执行!");
        BallGame game = new BallGame();
        game.launchFrame();
    }
     
}

running result:

Version 4:

    Objective: To achieve a small ball flying along at any angle

    Code (Code 3 version to save, we have created a new class BallGame2):

import  java.awt.*;
import javax.swing.*;
 
public class BallGame2 extends JFrame {
     
    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.jpg");
     
    double  x=100;    //小球的横坐标
    double  y=100; //小球的纵坐标
     
    double degree = 3.14/3;    //弧度。此处就是:60度(相当于较低3.14相当于Π,180°)
     
    //画窗口的方法
    public void paint(Graphics  g){
        System.out.println("窗口被画了一次!");
        g.drawImage(desk, 0, 0, null);
        g.drawImage(ball,(int)x, (int)y, null);
         
         
        x  = x+ 10*Math.cos(degree);//(cos60°角的方向执行)
        y  = y +10*Math.sin(degree); //同理
         
        //碰到上下边界
        if(y>500-40-30||y<40+40){//500是窗口高度;40是桌子边框,30是球直径;最后一个40是标题栏的高度
            degree = -degree;
        }
         
        //碰到左右边界
        if(x<40||x>856-40-30){
            degree = 3.14 - degree;
        }
         
    }
     
    //窗口加载
    void launchFrame(){
        setSize(856,500);
        setLocation(50,50);
        setVisible(true);
         
        //重画窗口,每秒画25次
        while(true){
            repaint(); 
            try{
                Thread.sleep(40);   //40ms,   1秒=1000毫秒.  大约一秒画25次窗口
            }catch(Exception e){
                e.printStackTrace();
            }
             
        }
         
    }
     
    //main方法是程序执行的入口
    public static void main(String[] args){
        System.out.println(" 我是赵广陆,成功执行!");
        BallGame2 game = new BallGame2();
        game.launchFrame();
    }
     
}

running result:

Published 30 original articles · won praise 3 · Views 2862

Guess you like

Origin blog.csdn.net/ZGL_cyy/article/details/104082939