Python辅导 | CSSE1001 Assignment 3

本次澳洲Python辅导的主要内容是完成 Introduction to Software Engineering I 的Assignment3,主要是使用Python GUI编程完成一个图形界面游戏程序的开发.

This assignment provides you the opportunity to apply concepts taught throughout the course to extend the functionality of a basic 2d sandbox game, in the style of Minecraft & Terraria.

The main concepts involved are Graphical User Interfaces (GUIs) and object-oriented programming. The assignment tasks are to add features to the game, as described in the requirements below.

You are encouraged to review some similar games, to better understand how this type of game is played, and for inspiration on advanced features. It is better to do this after reading through this document in its entirety.

Because this assignment deals with multiple files, while not required, you may wish to investigate a more sophisticated IDE. A popular option is PyCharm, which is free for students. VS Code, which is also free, is another common option. Please note that these tools have significantly more complex user-interfaces than IDLE, so you may find them a little overwhelming if you are only familiar with IDLE.

2.1. Getting Started

The archive a3_files.zip contains all the necessary files to start this assignment. A significant amount of support code has been supplied so that you begin with a simple application that is almost working.

The main assignment file is app.py , which contains an incomplete implementation of Ninedraft , the top-level GUI application class. The other files are support code which must not be edited. crafting.py is an exception to this rule, as it must be edited for some tasks.

Initially, you do not need to understand much of the provided code, but as you progress through the tasks, you will need to understand more of this code. You should add code to app.py and modify Ninedraft to implement the necessary functionality.

You are permitted to create additional files to simplify the separation of tasks (i.e. task1.py, task2.py, etc.), although this is not required. If you do this, app.py must be the entry point to your application (i.e. running it will run your assignment).

2.2. Pymunk Library

Physics is implemented in the game using the Pymunk library. You will need to install this library in order to implement your tasks for this assignment. Pymunk can be installed by running the included setup.py .

3.1. Task Overview

This assignment is broken down into three main tasks:

1. The first task involves adding lines of code to clearly marked sections within the main assignment file.
2. The second task involves extending the design to add more interesting functionality to the game.
3. And the third task involves adding sophisticated functionality to further improve the gameplay experience.

For CSSE7030 students only, there is an extra task that involves doing independent research. In general, as the tasks progress, they are less clearly prescribed and increase in difficulty.

3.2. Task Breakdown

CSSE1001 students will be marked out of 20 and CSSE7030 students will be marked out of 26 based on the following
breakdown. Tasks may be attempted in any order, but it is recommended to follow this breakdown, much as possible of each task before moving on to the next.
top-down.

3.3. Mark Breakdown

Sub–Task
App Class
Mouse Controls StatusView Class Basic Items Keyboard Controls File Menu & Dialogs
More Items Crafting CraftingTableBlock
Mobs Furnace
Arrow Movement Interaction with Blocks
For each task, marks will scaled according to the following breakdown.
localhost:8080
2/10

18/05/2019

Code is readable. Appropriate and meaningful identifier names have been used. Simple and clear code structure. Repeated code has been avoided.
Marks 15%
Code Quality
Functionality
Code has been simplified where appropriate and is not overly convoluted. 10%
Documented clearly and concisely, without excessive or extraneous comments. 15%
Components are functional, without major bugs or unhandled exceptions. 60% Assessed through user testing/playing, not automated testing.

Basic GUI Example

There are a significant number of comments in app.py intended to help you complete this task. 4.1. App Class
Write a main function that launches the Ninedraft GUI. Call this main function inside an if __name__ == … block. Modify Ninedraft so that the title of the window is set to something appropriate (i.e. Ninedraft, etc.).
localhost:8080 3/10

Ninedraft.redraw
redraw
GameView
_mouse_move, redraw
Ninedraft
show_target, hide_target
game.py
_left_click
Ninedraft
_right_click
Item.place
Ninedraft
__init__, _left_click, _right_click, redraw
Ninedraft
hide_target
GameView
game.py
show_target,
StatusView
tk.Frame
set_health(health)
18/05/2019 Assignment 3 — CSSE1001 Semester 1, 2019

4.2. Mouse Controls 4.2.1. Moving: Target

When the player’s mouse is moving over the game world, show the target cursor over the block position they have moused over. If the mouse leaves the game world (either to another widget or out of the window), or moves out of range, hide the preview.
Due to the program structure, once the appropriate tkinter event is bound, the only code that needs be added to achieve this is in (Mouse coordinates for the target cursor are saved when the mouse moves so that can use them when it is drawing).
See the methods on and the methods on in

4.2.2. Left Click: Attacking (Mining)

Allow the player to attack (mine) their target block by clicking the left mouse button. Code already exists to call the attack method of the player’s effective item and to mine the block.
Most of this logic is already implemented. You need to bind the mouse button & retrieve what the block drops when it is fully mined.
Further, when a block is successfully mined, reduce the player’s health/food by an amount of your choosing according to the following rules:
If the player has food (> 0), decrease their food Otherwise, decrease their health
See the method on .

4.2.3. Right Click: Using or Placing

When the player right clicks, one of two things should happen:

1. If the player is targeting a block, use that block
2. Otherwise, place the active item (see )
Code has been provided that implements this functionality. You only need to bind the appropriate mouse event.
See the method on See the
methods on in

4.3. StatusView Class

This class is used to display information to the player
Define a class named which inherits from
about their status in the game. The StatusView ‘s widgets must:

1. be updated whenever necessary (i.e. when gaining or losing health or food)

2. be laid out approximately according to Basic GUI Example

3. contain the following widgets:

Health (first row; left) A label to display the amount of health the player has remaining, with an image of a heart to the left. The health must be rounded to the nearest 0.5 (i.e. half or whole).
Food (first row; right) A label to display the amount of food the player has remaining, with an image of a drumstick to the left. The food must be rounded to the nearest 0.5 (i.e. half or whole).
Note: For convenience, you should have a setter method for each of the relevant widgets. i.e. , etc. localhost:8080

The StatusView class should be added to the application in a frame below the GameView . 4.4. Basic Items
One basic type of item is that which drops a block form of itself when placed. For example:

1. When the stone item is placed, it is logical that a stone block should appear
2. Further, when a stone block is mined, it also is logical that it should drop a stone item(s)
The class BlockItem , found in item.py , represents an item as per #1 above.
For this task, modify the create_item function so that it can generate wood & stone items. These should be instances of the
BlockItem class. E.g.

You do not need to modify create_block , as the relevant code to create wood & stone blocks already exists. 4.5. Keyboard Controls

4.5.1. Movement

When the player presses the space bar, they should jump into the air. This can be achieved by modifying their velocity. Set the velocity to something reasonable, that meets the following requirements:
The y-component must be negative (because computer graphics are drawn with the positive y-axis facing down)
The x-component must not be zero, but should be different to what it was (i.e. if the player was moving left, when they jump, they should also keep moving left, but at a different speed)
Double/triple/etc. jumping is allowed, so you do not need to check that the player is on the ground before jumping

4.5.2. Hotbar

When the player presses a number key (1-9, 0), the corresponding item in the hotbar should be selected. If the corresponding item is already selected, it should instead be deselected. Note that 1 corresponds to the first (leftmost) item in the hotbar, and 0 to the last (rightmost), etc.

4.6. File Menu & Dialogs

Implement a menu bar, with a File menu. The File menu should have the following entries: New Game : Restarts the game
Exit : Exits the application
When the player attempts to exit the application, either by the file menu or otherwise, they should first be prompted with a dialog to confirm that they indeed want to quit the application. Further, if the player dies, they should be informed of this with a dialog, and asked if they want to restart the game.
Note: On Mac OS X (and similar), the file menu should appear in the global menu bar (top of the screen).
>>> wood = create_item(‘wood’)
>>> wood
BlockItem(‘wood’)
>>> wood.place()
[(‘block’, (‘wood,))]
>>> block_id = wood.place()[0][1] >>> create_block(*block_id) ResourceBlock(‘wood’)
5. Task 2 – Intermediate Features
localhost:8080 5/10