1.JFrame
Gitee地址: https://gitee.com/lwt121788/untitled
吉士先生带你学习! 2022-10-14 19-33-21
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class MyFrame_one extends JFrame {
public MyFrame_one(){
//窗体展示
this.setTitle("吉士先生带你学习!");
MyPanel_Snack myPanel_snack=new MyPanel_Snack();
this.addKeyListener(myPanel_snack);
myPanel_snack.addKeyListener(myPanel_snack);
this.add(myPanel_snack);
this.setBackground(new Color(21, 20, 20));
this.setSize(1000,1000);
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
2.JPanel
package KuangStudy;
import javax.swing.*;
import javax.xml.crypto.Data;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class MyPanel_Snack extends JPanel implements KeyListener, ActionListener {
int Snake_length;
int []Snake_X=new int[600];
int []Snake_Y=new int[500];
String Snake_direction;
Boolean Snake_state=false;
//定时器
Timer timer=new Timer(100,this); //移动速度 ,监听谁
int food_x;
int food_y;
Random random=new Random();
//判断身体
Boolean fail_if=false;
//分数
int Score;
//构造器
public MyPanel_Snack(){
init();
this.setFocusable(true);
Snake_direction="Right";
timer.start(); //开始
}
//初始化
public void init(){
Snake_length=3;
//脑袋
Snake_X[0]=100; Snake_Y[0]=100;
//身体
Snake_X[1]=75; Snake_Y[1]=100;
Snake_X[2]=50; Snake_Y[2]=100;
food_x=25+25*random.nextInt(34); //边界850
food_y=75+25*random.nextInt(24); //边界
Score=0;
}
@Override
protected void paintComponent(Graphics g) {
//清屏
super.paintComponent(g);
//绘制静态的面板
//面板的颜色
this.setBackground(Color.YELLOW);
//插入广告位置
MyURL_bar.header.paintIcon(this,g,25,11);
g.fillRect(25,75,850,600);
//分数
g.setColor(Color.red);
g.setFont(new Font("黑体",Font.BOLD,25));
g.drawString("长度 :"+Snake_length,750,35);
g.drawString("分数 :"+Score,750,65);
//判断蛇头方向
if(Snake_direction.equals("Right")){ //右
//插入小蛇头
MyURL_bar.header_right.paintIcon(this,g,Snake_X[0],Snake_Y[0]);
}else if(Snake_direction.equals("Left")){ //左
MyURL_bar.header_lift.paintIcon(this,g,Snake_X[0],Snake_Y[0]);
}else if(Snake_direction.equals("Down")){ //下
MyURL_bar.header_down.paintIcon(this,g,Snake_X[0],Snake_Y[0]);
}else if(Snake_direction.equals("Up")){ //上
MyURL_bar.header_up.paintIcon(this,g,Snake_X[0],Snake_Y[0]);
}
//身体
for (int i = 1; i <Snake_length; i++) {
MyURL_bar.header_body.paintIcon(this,g,Snake_X[i],Snake_Y[i]);
}
//食物坐标
MyURL_bar.header_food.paintIcon(this,g,food_x,food_y);
if(!Snake_state){
g.setColor(new Color(255, 255, 255));
g.setFont(new Font("黑体",Font.BOLD,50));
g.drawString("点击空格继续游戏!",200,350);
}
//判断游戏成功与否
if(fail_if){
g.setColor(Color.red);
g.setFont(new Font("黑体",Font.BOLD,50));
g.drawString("游戏失败 点击空格继续!",200,350);
}
}
@Override
public void keyPressed(KeyEvent e) {
//按下空格
if(e.getKeyCode()==32){
if(fail_if){
fail_if=false;
//初始化
init();
}else {
Snake_state=!Snake_state;
}
repaint();
}
//小蛇移动方向
if(e.getKeyCode()==37||e.getKeyChar()=='a'){
if(Snake_direction!="Right"){
Snake_direction="Left";
}
}else if(e.getKeyCode()==38||e.getKeyChar()=='w'){
if(Snake_direction!="Down"){
Snake_direction="Up";
}
}else if(e.getKeyCode()==39||e.getKeyChar()=='d'){
if(Snake_direction!="Left"){
Snake_direction="Right";
}
}else if(e.getKeyCode()==40||e.getKeyChar()=='s'){
if(Snake_direction!="Up") {
Snake_direction = "Down";
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(Snake_state && fail_if==false){
if(Snake_X[0]==food_x&&Snake_Y[0]==food_y){
Snake_length++;
food_x=25+25*random.nextInt(34); //边界850
food_y=75+25*random.nextInt(24); //边界,
Score=Score+10; //分数
}
//移动小蛇的身体
for (int i = Snake_length-1; i >0 ; i--) {
Snake_X[i]=Snake_X[i-1];
Snake_Y[i]=Snake_Y[i-1];
}
if(Snake_direction=="Right"){ //右
Snake_X[0]=Snake_X[0]+25;
//越界重新出入
if(Snake_X[0]>850){
Snake_X[0]=25;
}
}else if(Snake_direction=="Left"){ //左
Snake_X[0]=Snake_X[0]-25;
//越界重新出入
if(Snake_X[0]<25){
Snake_X[0]=850;
}
}else if(Snake_direction=="Up"){
Snake_Y[0]=Snake_Y[0]-25;
if(Snake_Y[0]<75){
Snake_Y[0]=650;
}
}else if(Snake_direction=="Down"){
Snake_Y[0]=Snake_Y[0]+25;
if(Snake_Y[0]>650){
Snake_Y[0]=75;
}
}
//判断失败,撞到自己就算失败
for (int i = 1; i <Snake_length ; i++) {
if(Snake_X[0]==Snake_X[i]&& Snake_Y[0]==Snake_Y[i]){
fail_if=true;
}
}
repaint();
}
timer.start(); //开始
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
3.ICON
package KuangStudy;
import javax.swing.*;
import java.net.URL;
public class MyURL_bar {
//广告位置
public static URL headURL=MyURL_bar.class.getResource("imag.jpg");
public static ImageIcon header=new ImageIcon(headURL);
//上向
public static URL headURL_up=MyURL_bar.class.getResource("head.jpg");
public static ImageIcon header_up=new ImageIcon(headURL_up);
//左向
public static URL headURL_lift=MyURL_bar.class.getResource("left.jpg");
public static ImageIcon header_lift=new ImageIcon(headURL_lift);
//右向
public static URL headURL_right=MyURL_bar.class.getResource("right.jpg");
public static ImageIcon header_right=new ImageIcon(headURL_right);
//下向
public static URL headURL_down=MyURL_bar.class.getResource("down.jpg");
public static ImageIcon header_down=new ImageIcon(headURL_down);
//身体
public static URL headURL_body=MyURL_bar.class.getResource("body.jpg");
public static ImageIcon header_body=new ImageIcon(headURL_body);
/*
//尾巴
public static URL headURL_tail=MyURL_bar.class.getResource("tail.jpg");
public static ImageIcon header_tail=new ImageIcon(headURL_tail);
*/
//食物
public static URL headURL_food=MyURL_bar.class.getResource("food.jpg");
public static ImageIcon header_food=new ImageIcon(headURL_food);
}
4.Test
package KuangStudy;
import java.awt.*;
public class Test {
public static void main(String[] args) {
MyFrame_one myFrame_one=new MyFrame_one();
}
}
5.图片: