学习目标:
上一周学校一直在实训,所以一直没有更新博客(虽然也没有人看),但作为一个坚持更新半年多的UP主习惯还是要保持住的。
学习内容:
利用javaSE开发了两个小游戏 雪花飘落和弹弹球 由于博主以及学习了javaEE的知识,对程序做了一个简单的迁移,最终目标本来是部署到云端服务器的,但能力有限,老师也都撤了,最后还是失败了,不过起码努力过了。
学习时间:
提示:这里可以添加计划学习的时间 例如: 7.15-7.20 上午9:00-12:00 下午14:00-18:00
学习产出:
雪花飘落(效果图)
结果是动态的,奈何博主技术有限不会上传视频和动图。 简单说一下代码 创建窗口类
package game;
import javax.swing.JFrame;
public class JFDemo extends JFrame{
public JFDemo(int num) {
super();
this.setVisible(true);
this.setSize(600, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("雪花飘落");
this.setResizable(false);
JPDemo jp=new JPDemo(num);
this.add(jp);
}
}
创建画布类
package ball;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class JPDemo extends JPanel implements Runnable{
public static int[] arry1;
public static int[] arry2;
int x;
int y;
public JPDemo(int a) {
int [] arr1=new int[a];
int [] arr2=new int[a];
for (int i = 1; i <a; i++) {
int x=(int)(Math.random()*1200+1);
int y=(int)(Math.random()*800+1);
arr1[i-1]=x;
arr2[i-1]=y;
JPDemo.arry1=arr1;
JPDemo.arry2=arr2;
}
}
public void paint(Graphics g) {
super.paint(g);
this.setBackground(Color.BLACK);
g.setColor(Color.WHITE);
for (int i = 1; i<JPDemo.arry1.length; i++) {
Math.random();
g.drawString("*", JPDemo.arry1[i-1], JPDemo.arry2[i-1]);
}
}
public void move() {
new Thread(){
@Override
public void run() {
while (true) {
for(int i=0;i<JPDemo.arry2.length;i++){
if (arry2[i]>800) {
arry2[i]=0;
}
arry2[i]++;
}
for(int i=0;i<JPDemo.arry2.length/2;i++){
if (arry1[i]>1200) {
arry1[i]=0;
}
arry1[i]++;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}.start();
}
@Override
public void run() {
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="gbk"%>
<%@ page import="ball.*" %>
导入需要的包
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
设置背景图片
body{background:url(5b58376f45c37.jpg) no-repeat center center;
background-size:cover;
background-attachment:fixed;
background-color:#CCCCCC;}
</style>
</head>
<embed src="1878018903.mp3" loop="true" hidden="true"></embed>
设置背景音乐
<body>
设置文本输入框
<form action="game.jsp" method=post>
在这个炎炎夏日,你是否期待一场白雪来浸透炎热的内心?<br>
请输入雪花数量,沐浴你的雪天<br>
<input type=text name="num">
<input type="submit" value="开始下雪">
</form>
</body>
</html>
<%
try{
String num=request.getParameter("num");
int number=Integer.parseInt(num);
JFDemo jf=new JFDemo(number);
}
catch(NumberFormatException ee){
}
%>
弹球小游戏
窗体类(几乎一样)
package ball;
import java.awt.Graphics;
import java.awt.JobAttributes;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TanQiuJFrame extends JFrame{
public TanQiuJFrame(){
this.setVisible(true);
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("弹球小游戏");
this.setResizable(false);
TanQiuJPFrame jp=new TanQiuJPFrame();
this.add(jp);
jp.move();
}
}
画布类
package ball;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TanQiuJPFrame extends JPanel implements MouseMotionListener,Runnable{
int x=100,y=100;
int d=100;
int dx=100,dy=500,dd=200,dh=10;
int direction=2;
int count;
boolean flag=false;
boolean game=false;
public TanQiuJPFrame() {
addMouseMotionListener(this);
}
public void paint(Graphics g) {
super.paint(g);
this.setBackground(Color.GRAY);
g.setColor(Color.PINK);
g.fillOval(x, y, d, d);
g.setColor(Color.BLUE);
g.fillRect(dx, dy, dd, dh);
g.setFont(new Font("分数"+count,Font.BOLD,50));
g.drawString("分数"+count, 100, 50);
if (flag) {
g.drawString("游戏结束", 100, 150);
}
if (game) {
g.drawString("游戏通关", 100, 150);
}
}
public void move() {
new Thread(){
@Override
public void run() {
super.run();
while (true) {
if (d==0) {
game=true;
}
if(direction==1){
x--;
y++;
}
if (x<=0) {
if(direction==1){
direction=2;
}
if(direction==4){
direction=3;
}
}
if(direction==2){
x++;
y++;}
if (y>=500-d) {
if (x+d/2>=dx&(x+d/2<=dx+dd)) {
d=d-10;
dd=dd-10;
count++;
if (direction==2) {
direction=3;
}
if (direction==1) {
direction=4;
}
}else {
flag=true;
this.stop();
}
}
if(direction==3){
x++;
y--;
}
if(x+d>=800){
if (direction==3) {
direction=4;
}
if (direction==2) {
direction=1;
}
}
if(direction==4){
x--;
y--;
}
if(y<=0){
if (direction==4) {
direction=1;
}
if (direction==3) {
direction=2;
}
}
try {
if (count<10) {
Thread.sleep(5-count/2);
}else {
Thread.sleep(1);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}.start();
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
dx=e.getX()-dd/2;
}
@Override
public void run() {
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="ball.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<embed src="2451457534.mp3" loop="true" hidden="true"></embed>
<%
TanQiuJFrame jp=new TanQiuJFrame();
%>
</body>
</html>
以上就是一周的实训成果。
|