This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

159.261 GamingProgramming

游戏编程

159.261 Gaming Programming 游戏编程

文字笔记记录于此,代码请见 仓库

要求

  • 文件名小写
  • 标题首字母大写
  • 所需文件以Onedrive直链接形式嵌入

1 - 日志

游戏开发日志

1.1 - Assignment1

Snake Game

Snake Game — Assignment 1

by Hamhuo

🕹 Introduction

This project is a classic Snake game implemented using Java AWT and a custom GameEngine.
The player controls a snake moving on a 25×25 grid, growing longer by eating apples.
The game ends if the snake hits the wall or collides with itself.

✨ Highlights

1. Efficient Direction Control

  • The snake’s movement direction is managed using numeric encoding combined with XOR operations.
  • Direct reversal (e.g., moving from LEFT to RIGHT immediately) is prevented with a simple and efficient one-line check:
if ((direction.getValue() ^ d.getValue()) != 2) {  
    this.direction = d;  
}
  • This approach is cleaner and more efficient than traditional if-else chains.

2. Flexible Speed and Length Configuration

Before starting the game, a settings dialog allows players to customize:

  • Initial snake length
  • Movement speed (cells per second)
  • Grass background color

image.png

3. Smooth Time-Step Movement

  • Snake movement is controlled by accumulated elapsed time rather than frame count.
  • This ensures consistent movement speed across different machines or frame rates.
  • The snake moves only when accumulated time exceeds its speed threshold.
  public void init(int length, Direction d, Double s) {  
  
    speed = 1 / s;  
    // Set the snake's initial length  
    initLength = length;  
  
    // Initialize the snake's body as an ArrayList of Points  
    snakeBody = new ArrayList<>();  
  
    // Add points to the snake's body, starting at position (3, 5)  
    for (int i = 3; i < initLength + 3; i++) {  
        snakeBody.add(new Point(i, 5)); // Add each segment of the snake to the body  
    }  
  
    // Set the snake's head to be the first point in the body  
    snakeHead = snakeBody.get(0);  
  
    direction = d;  
}

4. Resource Loading and Image Rendering

Separate sprite images are used for:

  • Snake head
  • Snake body
  • Apples
  • Proper resource management ensures a polished and visually consistent appearance.

image.png

image.png

🛠 Technical Features

  • Object-Oriented Design (OOD) with clear class separation:
    • Snake class handles snake logic (movement, growth, shrink).
    • MainGame class manages overall game state, rendering, and input.
  • Collision detection:
    • Wall collision leads to gradual shrinking.
    • Self-collision (snake colliding with its body) triggers immediate game over.
  • Score tracking:
    • The current score is displayed near the apple’s position.
  • Restart mechanism:
    • Pressing R after game over will reset and restart the game.

image.png

📦 How to Run

  1. Open the project with IntelliJ IDEA or any Java IDE.
  2. Ensure the resource images (apple.png, dot.png, head.png) are correctly placed under src/main/resources/.
  3. Run MainGame.java.
  4. Set your desired snake length, speed, and background color via the settings dialog.
  5. Play and enjoy!

2 - 日志

游戏开发日志

2.1 - Assignment2

Slapocalypse

地图制作指南

本指南帮助用户为《Slapocalypse》游戏创建自定义地图。以下是地图中使用的障碍物贴图类型及其对应的图像。每个贴图编号对应 GameController.java 中的障碍物类型,用于在地图文件(如 map5.txt)中指定地形。地板贴图(编号 0)由系统随机选择 floorTile1floorTile2,此处仅列出障碍物贴图(1-10)。

贴图概览

地图使用以下贴图编号(1-10)表示障碍物(如墙壁、连接器、角落)。请在地图文件中使用这些编号来设计布局。

1号贴图 - 垂直墙

描述:垂直方向的墙壁,用于分隔区域,阻挡移动。

2号贴图 - 水平墙下方

描述:水平方向的墙壁下方房间,用于分隔区域,阻挡移动。 垂直墙贴图

3号贴图 - 水平墙端点(左侧)

描述:水平墙的左侧端点,用于墙壁的起始点。 水平墙贴图

4号贴图 - 连接器(右侧)

描述:连接墙壁的右侧部分,通常用于墙壁的延续或连接。 水平墙端点(左侧)贴图

5号贴图 - 右上角

描述:墙壁的右上角,用于角落转折。 连接器(右侧)贴图

6号贴图 - 水平墙(旋转90度)

描述:垂直墙(基于1号贴图旋转90度),用于特定方向的阻挡。 右上角贴图

7号贴图 - 连接器(左侧)

描述:连接墙壁的左侧部分(基于4号贴图旋转180度)。 水平墙(旋转)贴图

8号贴图 - 右下角

描述:墙壁的右下角(基于5号贴图旋转90度)。 连接器(左侧)贴图

9号贴图 - 左下角

描述:墙壁的左下角(基于5号贴图旋转180度)。 右下角贴图

10号贴图 - 左上角

描述:墙壁的左上角(基于5号贴图旋转270度)。 左下角贴图

贴图编号表格

以下表格总结了障碍物贴图的编号和描述,供快速参考:

贴图编号 描述
1 垂直墙
2 水平墙下方
3 水平墙端点(左侧)
4 连接器(右侧)
5 右上角
6 水平墙(旋转90度)
7 连接器(左侧)
8 右下角
9 左下角
10 左上角

地图文件格式

  • 地图文件(如 src/main/resources/map5.txt)是一个文本文件,表示地图的网格布局。
  • 每行代表地图的一行,使用空格分隔的数字(0-10)表示贴图类型,0 表示地板,1-10 表示障碍物。
  • 左右必须留有 1 层厚的墙(贴图随意)
  • 上下必须各留有 2 层厚的墙(贴图随意)
  • 示例(7x7地图):
0 0 0 0 0 0 0  
0 0 0 0 0 0 0  
0 1 2 3 4 5 0  
0 1 2 3 4 5 0  
0 1 2 3 4 5 0  
0 0 0 0 0 0 0  
0 0 0 0 0 0 0