9.18
package Sanke;
import javax.swing.*;
/**
* @author Swyee
**/
public class SnakeGame extends JFrame {
SnakeGrid snakeGrid= new SnakeGrid();
Button button = new Button(snakeGrid);
SnakeGame(){
this.setBounds(100, 50, 700, 500);//设置窗口大小
this.setLayout(null);//更改layout 以便添加组件
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗口的状态
this.setResizable(false);//窗口不可以改变大小
this.add(snakeGrid);
this.add(button);
//设置焦点
snakeGrid.setFocusable(true);
snakeGrid.requestFocus();
snakeGrid.Music();//调用打开音乐的方法
this.setVisible(true);//设置焦点状态为true
}
public static void main(String[] args) {
new SnakeGame();
}
}
import javax.swing.*;
/**
* @author Swyee
**/
public class SnakeGame extends JFrame {
SnakeGrid snakeGrid= new SnakeGrid();
Button button = new Button(snakeGrid);
SnakeGame(){
this.setBounds(100, 50, 700, 500);//设置窗口大小
this.setLayout(null);//更改layout 以便添加组件
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗口的状态
this.setResizable(false);//窗口不可以改变大小
this.add(snakeGrid);
this.add(button);
//设置焦点
snakeGrid.setFocusable(true);
snakeGrid.requestFocus();
snakeGrid.Music();//调用打开音乐的方法
this.setVisible(true);//设置焦点状态为true
}
public static void main(String[] args) {
new SnakeGame();
}
}
package Sanke;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* @author Swyee
**/
public class SnakeGrid extends JPanel {
Food food = new Food();
Snake snake = new Snake(food);//创建蛇
ImageIcon image = new ImageIcon("sky.jpg");//图片文件地址
File f= new File("music.wav");//音乐文件地址
SnakeThread snakeThread = new SnakeThread();
SnakeGrid(){
this.setBounds(0, 0, 700, 400);
this.setBackground(Color.black);
snakeThread.start();
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
snake.keyboard(e);
}
});
}
/**
* 设置画笔
* @param g
*/
@Override
public void paint(Graphics g) {
super.paint(g);
image.paintIcon(this, g, 0, 0); //设置背景图片
snake.move();//蛇移动
snake.draw(g);
food.draw(g);
}
//读取音乐文件
void Music(){
try {
URI uri = f.toURI();
URL url = uri.toURL();
AudioClip aau= Applet.newAudioClip(url);
aau.loop();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class SnakeThread extends Thread{
boolean flag = true;
@Override
public void run() {
while(Snake.islive &&flag){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(Snake.islive&& Button.isMove){
repaint();
}
}
if(!flag==false){
JOptionPane.showMessageDialog(SnakeGrid.this, "游戏结束");
}
}
public void stopThread(){
flag=false;
}
}
}
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* @author Swyee
**/
public class SnakeGrid extends JPanel {
Food food = new Food();
Snake snake = new Snake(food);//创建蛇
ImageIcon image = new ImageIcon("sky.jpg");//图片文件地址
File f= new File("music.wav");//音乐文件地址
SnakeThread snakeThread = new SnakeThread();
SnakeGrid(){
this.setBounds(0, 0, 700, 400);
this.setBackground(Color.black);
snakeThread.start();
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
snake.keyboard(e);
}
});
}
/**
* 设置画笔
* @param g
*/
@Override
public void paint(Graphics g) {
super.paint(g);
image.paintIcon(this, g, 0, 0); //设置背景图片
snake.move();//蛇移动
snake.draw(g);
food.draw(g);
}
//读取音乐文件
void Music(){
try {
URI uri = f.toURI();
URL url = uri.toURL();
AudioClip aau= Applet.newAudioClip(url);
aau.loop();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class SnakeThread extends Thread{
boolean flag = true;
@Override
public void run() {
while(Snake.islive &&flag){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(Snake.islive&& Button.isMove){
repaint();
}
}
if(!flag==false){
JOptionPane.showMessageDialog(SnakeGrid.this, "游戏结束");
}
}
public void stopThread(){
flag=false;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
2023-09-30 java动手动脑