帮我写一个贪吃蛇的代码
时间: 2023-06-05 20:08:03 浏览: 51
好的,这是一份用 Python 实现的简单的贪吃蛇游戏代码:
```
import turtle
import time
# 设置窗口大小
turtle.setup(width=600, height=600)
# 创建一个 Turtle 对象
snake = turtle.Turtle()
# 设置速度
snake.speed(10)
# 设置蛇头的形状
snake.shape("square")
# 向上移动
snake.up()
# 设置起始位置
snake.goto(0, 0)
# 设置方向向右
snake.right(90)
# 开始移动
while True:
snake.forward(10)
time.sleep(0.2)
```
这份代码只是一个简单的演示,仅能实现蛇头向右移动,你可以根据需要添加更多的功能。
相关问题
用java帮我写一个贪吃蛇代码
好的,我可以回答这个问题。以下是一个简单的贪吃蛇代码示例,使用Java编写:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private int x[] = new int[ALL_DOTS];
private int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public SnakeGame() {
setTitle("Snake");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
ImageIcon iid = new ImageIcon(getClass().getResource("dot.png"));
ball = iid.getImage();
ImageIcon iia = new ImageIcon(getClass().getResource("apple.png"));
apple = iia.getImage();
ImageIcon iih = new ImageIcon(getClass().getResource("head.png"));
head = iih.getImage();
addKeyListener(new TAdapter());
timer = new Timer(DELAY, this);
timer.start();
initGame();
}
public void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
inGame = true;
}
public void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
public void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
public void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] >= HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
public void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
apple_y = ((r * DOT_SIZE));
}
public void paint(Graphics g) {
super.paint(g);
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(ball, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
public void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
}
public static void main(String[] args) {
new SnakeGame().setVisible(true);
}
}
```
希望这个示例对你有所帮助!
能使用st语言帮我写一个贪吃蛇代码吗》
当然可以,以下是一个简单的贪吃蛇游戏的 ST 代码:
VAR
snake_array : ARRAY[1..100] OF INT; // 蛇身数组
snake_length : INT := 3; // 蛇的初始长度
food_x : INT := 5; // 食物的横坐标
food_y : INT := 5; // 食物的纵坐标
direction : INT := 2; // 初始方向:2表示向右
score : INT := 0; // 分数
// 初始化蛇身
FOR i := 1 TO snake_length DO
snake_array[i] := (i - 1) * 2 + 1;
// 游戏循环
WHILE TRUE DO
// 画出蛇和食物
FOR y := 1 TO 9 DO
FOR x := 1 TO 9 DO
IF x = food_x AND y = food_y THEN
WRITE("x"); // 画出食物
ELSE
is_snake := FALSE;
FOR i := 1 TO snake_length DO
IF x = snake_array[i] DIV 2 + 1 AND y = 10 - snake_array[i] MOD 2 THEN
WRITE("*"); // 画出蛇身
is_snake := TRUE;
END_IF
END_FOR
IF NOT is_snake THEN
WRITE("."); // 画出空格
END_IF
END_IF
END_FOR
WRITE("\n"); // 换行
END_FOR
// 检查键盘输入并改变方向
key := READ_KEY();
IF key = "up" AND direction <> 3 THEN
direction := 1;
ELSIF key = "right" AND direction <> 4 THEN
direction := 2;
ELSIF key = "down" AND direction <> 1 THEN
direction := 3;
ELSIF key = "left" AND direction <> 2 THEN
direction := 4;
END_IF
// 计算蛇头的新位置
IF direction = 1 THEN // 向上
snake_head := snake_array[1] - 2;
ELSIF direction = 2 THEN // 向右
snake_head := snake_array[1] + 1;
ELSIF direction = 3 THEN // 向下
snake_head := snake_array[1] + 2;
ELSIF direction = 4 THEN // 向左
snake_head := snake_array[1] - 1;
END_IF
// 检查是否吃到了食物
IF snake_head DIV 2 + 1 = food_x AND 10 - snake_head MOD 2 = food_y THEN
snake_length := snake_length + 1;
snake_array[snake_length] := snake_array[snake_length - 1];
score := score + 10;
food_x := RANDOM(1, 9);
food_y := RANDOM(1, 9);
FOR i := 1 TO snake_length DO
IF food_x = snake_array[i] DIV 2 + 1 AND food_y = 10 - snake_array[i] MOD 2 THEN
food_x := RANDOM(1, 9);
food_y := RANDOM(1, 9);
i := 0;
END_IF
END_FOR
END_IF
// 移动蛇尾
FOR i := snake_length DOWNTO 2 DO
snake_array[i] := snake_array[i - 1];
END_FOR
// 移动蛇头
snake_array[1] := snake_head;
// 检查是否撞墙或撞到自己
IF snake_head DIV 2 + 1 < 1 OR snake_head DIV 2 + 1 > 9 OR 10 - snake_head MOD 2 < 1 OR 10 - snake_head MOD 2 > 9 THEN
WRITE("Game over! ");
EXIT;
END_IF
FOR i := 2 TO snake_length DO
IF snake_head = snake_array[i] THEN
WRITE("Game over! ");
EXIT;
END_IF
END_FOR
// 显示分数
WRITE("Score: ", score, "\n");
// 等待一段时间
WAIT(0.5);
END_WHILE
注意:这段代码仅供参考,可能存在错误或不足之处,仅用于学习交流之用,建议在实际使用前进行完善和测试。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)