附上镇楼图
进入主题
学编程其实没有你们想的那么难(也没你们想的那么简单),学习的第一步就是先决定你要学什么语言
这里先列出几种常用的编程语言
C/C++我个人认为最强大的,适用于编写底层驱动或者一些对于一款操作系统比较基础的程序,同时能写的东西也很多,可以写游戏的(前提是你不怕头秃)
C#没怎么接触,但是可以直接设计图形界面,这个比较友好,可以配合Unity3D使用
Unity3D 写游戏的最佳选择之一,软件内自带商店,可以下载素材,插件等东西,功能比较强大,需要配合C#食用,我没去深究过,电脑带不动(赛扬能带的动什么3D软件)
Html 这东西你写网页肯定用得到,你用稍微主流一点的浏览器随便打开一个网页,按下F12你会发现里面有一堆网页html源码,写网页必不可少的东西
Python和PHP我没去了解过,但是听说Python是多用途语言,PHP用于WEB,另外Python可以写的东西很多的,可以写人工智能
易语言 这个不用说了,可以写外挂之类的,但是写出来大多杀软会报毒,但是写起来确实比其他编程语言舒服不少
Java 目前大多数公司都在使用的语言,语法没有C/C艹严格,功能较为强大,可以写android程序,跨平台的编程语言,非常推荐
现在介绍思路,学编程最忌讳的就是:“我今天要学基本语法,我明天要把这个类的东西学完,我后天要学这个方法”其实吧,我也只把某些语言的基本语法过了一遍,我现在写程序都是直接网上找某个功能的实现方法,然后把找到的实现方法用代码体现出来,然后拼成一个程序,当然不要复制,要自己敲,敲的时候可以改变一下代码内容,如果想更深入的了解那段代码的意义,你可以直接百度那段代码,然后进行了解,多搜几次,这么学习之后,时间稍微久一点,你基本上可以不用百度就能写出一个你想要的程序了
举个例子
我想写一小游戏,第一步就是拿出笔和纸或者直接打开SAI,在画布中央画一个圈,表示你游戏的完全体,在多画几个圈,那些是你游戏的几个部分,在通过多画几个圈来表示那几个小圈的构成,说简单点就是结构图,像这样
画完图就开始实现这些功能吧,这里用Java作示范,因为只是介绍思路,以下代码不作具体解释
基本语法过一遍之后你就可以写程序了,系统这里面包含窗口和规则(反正我是这样分类),窗口又包含一堆控件,控件和规则先放一边去,没有窗口哪来的控件,你控件都没有还想做游戏?
然后我们就需要一个窗口
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;//载入一堆包,这些都是稍后要用的
按照Java的基本语法把程序的基本框架弄出来,然后一个静态方法
public static void StartWindow(){
//一个静态的启动界面的方法
}
你可以在main中直接调用这个,你甚至可以不写这个方法,直接把代码放进main里
然后就是创建窗口,下面一段代码是创建窗口的一堆基本代码
JFrame Window=new JFrame();
Window.setSize(800, 450);
Window.setTitle(WindowTitle);
Window.setLocationRelativeTo(null);
Window.setResizable(false);
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Window.setVisible(true);
有了窗口就要有控件,下面创建两个按钮和一个LOGO(我代码里的LOGO路径是自己设置过的)
Window.getContentPane().setLayout(null);
JLabel logo=new JLabel(new ImageIcon("./assets/LOGO.png"));
Window.add(logo);
logo.setBounds(0, 0, 454, 340);
Button Start=new Button("开始游戏");
Button Exit=new Button("退出");
JPanel button=new JPanel();
button.add(Start);
button.add(Exit);
Start.setBounds(600, 140, 100, 40);
Exit.setBounds(600,200,100,40);
Window.add(button);
Exit.addActionListener(e ->{
System.exit(0);
});
Start.addActionListener(e ->{
game();
Window.disable();
Window.setVisible(false);
});
有了启动界面就可以写正式的游戏界面了,首先需要规则,我这是控制人物撞一个物体,然后撞到之后物体就随机移动到其他位置,再去撞,控制是用键盘控制
这里谈到了物体,我们就要载入它们,上面的代码里面有个“game()”这个就是载入正式的游戏界面,我们先编写一个静态方法
public static void game() {
}
然后再这个方法里面加上载入物体和创建窗体的代码,
最后加入控制人物的代码和系统规则的代码,因为我是控制人物去碰撞物体,所以我这里就把规则代码写进控制系统里,还是刚才新建的静态方法,在里面加入有关键盘控制的代码和有关游戏规则的代码,做到控制一下人物就检测人物是不是碰撞了物体
GameWin.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (e.getKeyCode()==KeyEvent.VK_UP) {
player.setBounds(0, 0, player.getWidth(), player.getHeight()-10);
if((player.getWidth()>=basket.getWidth()-50 && player.getWidth()<=basket.getWidth()+50)&&((player.getHeight()>=basket.getHeight()-50 && player.getHeight()<=basket.getHeight()+50))) {
int Minw = 100;
int Minh =100;
int Maxw = 800;
int Maxh =400;
int w = Minw + (int)(Math.random() * ((Maxw - Minw) + 1));
int h = Minh + (int)(Math.random() * ((Maxh - Minh) + 1));
basket.setBounds(0, 0, w, h);
}
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
player.setBounds(0, 0, player.getWidth(), player.getHeight()+10);
if((player.getWidth()>=basket.getWidth()-50 && player.getWidth()<=basket.getWidth()+50)&&((player.getHeight()>=basket.getHeight()-50 && player.getHeight()<=basket.getHeight()+50))) { int Minw = 100;
int Minh =100;
int Maxw = 800;
int Maxh =400;
int w = Minw + (int)(Math.random() * ((Maxw - Minw) + 1));
int h = Minh + (int)(Math.random() * ((Maxh - Minh) + 1));
basket.setBounds(0, 0, w, h);
}
}else if(e.getKeyCode()==KeyEvent.VK_LEFT) {
player.setBounds(0, 0, player.getWidth()-10, player.getHeight());
if((player.getWidth()>=basket.getWidth()-50 && player.getWidth()<=basket.getWidth()+50)&&((player.getHeight()>=basket.getHeight()-50 && player.getHeight()<=basket.getHeight()+50))) { int Minw = 100;
int Minh =100;
int Maxw = 800;
int Maxh =400;
int w = Minw + (int)(Math.random() * ((Maxw - Minw) + 1));
int h = Minh + (int)(Math.random() * ((Maxh - Minh) + 1));
basket.setBounds(0, 0, w, h);
}
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
player.setBounds(0, 0, player.getWidth()+10, player.getHeight());
if((player.getWidth()>=basket.getWidth()-50 && player.getWidth()<=basket.getWidth()+50)&&((player.getHeight()>=basket.getHeight()-50 && player.getHeight()<=basket.getHeight()+50))) { int Minw = 100;
int Minh =100;
int Maxw = 800;
int Maxh =400;
int w = Minw + (int)(Math.random() * ((Maxw - Minw) + 1));
int h = Minh + (int)(Math.random() * ((Maxh - Minh) + 1));
basket.setBounds(0, 0, w, h);
}
}
}
});
然后各个部分都实现了,就可以测试你的程序了
这里因为分成几个部分在说代码,所以代码不完整,容易误导萌新,这里附上完整代码
package YouPlayBasketballLikeCaiXuKun;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class YouAreCaiXukun {
public static void main(String[] args) {
Window_Create("篮球人 Ver114514.1919810");
}
@SuppressWarnings("deprecation")
public static void Window_Create(String WindowTitle) {
JFrame Window=new JFrame();
Window.setSize(800, 450);
Window.setTitle(WindowTitle);
Window.setLocationRelativeTo(null);
Window.setResizable(false);
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Window.setVisible(true);
Window.getContentPane().setLayout(null);
JLabel logo=new JLabel(new ImageIcon("./assets/LOGO.png"));
Window.add(logo);
logo.setBounds(0, 0, 454, 340);
Button Start=new Button("开始游戏");
Button Exit=new Button("退出");
JPanel button=new JPanel();
button.add(Start);
button.add(Exit);
Start.setBounds(600, 140, 100, 40);
Exit.setBounds(600,200,100,40);
Window.add(button);
Exit.addActionListener(e ->{
System.exit(0);
});
Start.addActionListener(e ->{
game();
Window.disable();
Window.setVisible(false);
});
}
public static void game() {
JFrame GameWin=new JFrame();
GameWin.setSize(800, 450);
GameWin.setTitle("篮球人 上下左右控制鲲鲲移动,去撞篮球吧");
GameWin.setLocationRelativeTo(null);
GameWin.setResizable(false);
GameWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GameWin.setVisible(true);
GameWin.getContentPane().setLayout(null);
JLabel player=new JLabel(new ImageIcon("./assets/Kun.png"));
GameWin.add(player);
player.setBounds(0, 0, 700, 730);
JLabel basket=new JLabel(new ImageIcon("./assets/basketball.png"));
GameWin.add(basket);
basket.setBounds(0, 0, 500, 500);
GameWin.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (e.getKeyCode()==KeyEvent.VK_UP) {
player.setBounds(0, 0, player.getWidth(), player.getHeight()-10);
if((player.getWidth()>=basket.getWidth()-50 && player.getWidth()<=basket.getWidth()+50)&&((player.getHeight()>=basket.getHeight()-50 && player.getHeight()<=basket.getHeight()+50))) {
int Minw = 100;
int Minh =100;
int Maxw = 800;
int Maxh =400;
int w = Minw + (int)(Math.random() * ((Maxw - Minw) + 1));
int h = Minh + (int)(Math.random() * ((Maxh - Minh) + 1));
basket.setBounds(0, 0, w, h);
}
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
player.setBounds(0, 0, player.getWidth(), player.getHeight()+10);
if((player.getWidth()>=basket.getWidth()-50 && player.getWidth()<=basket.getWidth()+50)&&((player.getHeight()>=basket.getHeight()-50 && player.getHeight()<=basket.getHeight()+50))) { int Minw = 100;
int Minh =100;
int Maxw = 800;
int Maxh =400;
int w = Minw + (int)(Math.random() * ((Maxw - Minw) + 1));
int h = Minh + (int)(Math.random() * ((Maxh - Minh) + 1));
basket.setBounds(0, 0, w, h);
}
}else if(e.getKeyCode()==KeyEvent.VK_LEFT) {
player.setBounds(0, 0, player.getWidth()-10, player.getHeight());
if((player.getWidth()>=basket.getWidth()-50 && player.getWidth()<=basket.getWidth()+50)&&((player.getHeight()>=basket.getHeight()-50 && player.getHeight()<=basket.getHeight()+50))) { int Minw = 100;
int Minh =100;
int Maxw = 800;
int Maxh =400;
int w = Minw + (int)(Math.random() * ((Maxw - Minw) + 1));
int h = Minh + (int)(Math.random() * ((Maxh - Minh) + 1));
basket.setBounds(0, 0, w, h);
}
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
player.setBounds(0, 0, player.getWidth()+10, player.getHeight());
if((player.getWidth()>=basket.getWidth()-50 && player.getWidth()<=basket.getWidth()+50)&&((player.getHeight()>=basket.getHeight()-50 && player.getHeight()<=basket.getHeight()+50))) { int Minw = 100;
int Minh =100;
int Maxw = 800;
int Maxh =400;
int w = Minw + (int)(Math.random() * ((Maxw - Minw) + 1));
int h = Minh + (int)(Math.random() * ((Maxh - Minh) + 1));
basket.setBounds(0, 0, w, h);
}
}
}
});
}
}
现在你已经掌握编程语言的选择,编程的思路和对应的例子了,来试着用汇编和C++来复刻Windows10吧!
萌新第一次写博客,大佬轻喷QWQ