• Aucun résultat trouvé

Introduction to Programming

N/A
N/A
Protected

Academic year: 2022

Partager "Introduction to Programming"

Copied!
3
0
0

Texte intégral

(1)

Introduction to Programming

Practical #8

Figure1 – Game of Tron.

1 Tron

In this practical, we are going to program the game of TRON. It is game with 2 players in which each controls a mobile that moves with constant speed and leaves behind a trace. The first player that crashes on its trace or the adversary’s trace (or exits the window) loses the game. This practical is rather ambitious and is a mini-project. It will take us several sessions.

1.1 Snake

We will proceed in two steps. First program a game of Snake with a single player. In this game, the single player controls a snake that grows little by little (of one element eachxround, with the convention that the total length is bounded by a constantnmaxelements).

The starting project has two files,utils.hand utils.cpp, that contain a structure point (that we may enrich with useful methods later) and a function meant to get the keys pressed by the players.

We will build an objectSnakewith adequate methods, and a functiongame_1pexploiting the capacities of the Snake to reproduce the desired behavior. First, we won’t care about collisions (with the border or with itself), and only do that in a second time. Your work is split into 6 steps.

1. (on paper) Define an interface for classSnake (list all necessary functionalities).

2. (on paper) Think about implementation of the class Snake : how to store the data ? How to program the different methods ? (read first some remarks from next paragraph).

3. In a file snake.h, write the declaration of the class Snake : its members, its methods, what is public, what is not.

4. Submit the result of your thoughts to the instructor to validate your choices.

5. Implement the classSnake(that is, program the methods that you declared).

6. Program the functiongame_1pusing aSnake.

Remark : the fileutils.hdefines :

1. 4 integersleft,down,up,rightsuch that :

(a) the functionx→(x+ 1)%4transforms left to down, down to right, right to up and up to left ; it corresponds to a quarter turn in trigonometric sense.

1

(2)

(b) the functionx→(x+ 3)%4makes the same but in clockwise direction.1

2. an array of 4 pointsdirsuch that, after definition of a function computing the sum of two points, the functionp→p+dir[d] returns :

(a) ford=leftthe point corresponding to a shift of pof 1 unit left.

(b) ford=upthe point corresponding to a shift ofpof 1 unit up.

(c) ford=rightthe point corresponding to a shift of pof 1 unit right.

(d) ford=downthe point corresponding to a shift of pof 1 unit down.

1.2 Tron

From the game of Snake, it will be easy to implement the game of Tron.

1. Two players.

Inspired by function game_1p, create a function game_2p with two players. We will use for this player the keysS,X,DandF.2The functionkeyboard()will return the integersint(’S’), int(’X’), int(’D’) and int(’F’). Remark : we treat only one key per round, hence a single call to function keyboard() per round, otherwise the snakes are in concurrency for the keyboard.

2. Ultimate tuning.

(a) Handle the collision of the snakes.

(b) The principle of Tron is that the trace of mobiles remains. To implement that, we just need to extend the snake at each round.

1.3 Graphics

Small bonus to make our game more attractive : we will see how to handle graphics instead of the uniform rectangles we had until now. The objective is to replace the heading square by an image that we will move at each step.

We are going to use the NativeBitmap of Imagine++, that are images that are faster to draw on screen then regular images. To put an image in aNativeBitmapwe proceed like follows :

// I n t e g e r s p a s s e d by r e f e r e n c e when l o a d i n g t h e image // s o a s t o s t o r e t h e w i d t h and h e i g h t o f t h e image i n t w, h ;

// Image l o a d i n g b y t e∗ rgb ;

l o a d C o l o r I m a g e ( s r c P a t h ("image_name . bmp") , rgb , w, h ) ; // D e c l a r a t i o n o f a N a t i v e B i t m a p

NativeBitmap my_native_bitmap (w, h ) ; // Put t h e image i n t h e N a t i v e B i t m a p

my_native_bitmap . s e t C o l o r I m a g e ( 0 , 0 , rgb , w, h ) ; d e l e t e [ ] r gb ; // We don ’ t need t h e image anymore

The display of aNativeBitmapon screen can be done using the function of Imagine++ : v o i d putNativeBitmap (i n t x , i n t y , NativeBitmap nb )

1. Replace in the snake the display of the head by a bitmap. You can use the imagesmoto_blue.bmp andmoto_red.bmpin the archive.

2. Use the imageexplosion.bmpto show the death of a player.

1.4 Make the code great

To have a clean code of which you may be proud, heed the following guidelines :

— The classSnakehas one/several constructor(s).

— The classSnakehas a destructor only if necessary. Hint : did some dynamic allocation occur ?

— No useless copy of aSnake when passing as argument of a function. Hint : are objects of type Snakepassed by reference ? See the course about copy constructor to understand why it matters.

1. In mathematics,(x+ 3)%4 = (x1)%4, but for C++−1%4 =−1, which is wrong for our purpose.

2. The keys A, Z, Q and W are not in the right conguration for a Qwerty keyboard, thus avoid them.

2

(3)

— Only methods used by the exterior hare public. All others must be private.

— The methods that do not change the Snake areconst.

— The indentation is correct. Do we really have to repeat this obvious guideline ?

— The bitmaps are read withsrcPathand the program stops if one image file is not found.

3

Références

Documents relatifs

For example, the ED should be able to receive a standard Abstract Syntax representation for an expression, plus an editing command in Abstract Syntax (e.g., Edit[expr,

The optional javaCodebase attribute is used to store the locations of the object factory’s and the object’s class definitions.. A directory entry containing a JNDI reference

To communicate the completion of an output response (4), the user process could issue an explicit system call; or, the Terminal Handler could assume completion when the

Once this constraint is lifted by the deployment of IPv6, and in the absence of a scalable routing strategy, the rapid DFZ RIB size growth problem today can potentially

This document describes URN namespaces that will identify various objects within the EPCglobal system for identifying products within ecommerce and supply chain

Potential action items for the W3C included investigating the formation of a privacy interest group and formulating guidance about fingerprinting, referrer headers,

5 Following the administration of CD8 + T cells activated in vitro, the help tumor infiltration by effector cells is essential for the efficacy of t cell-based

The resolution of a plane elasticity problem comes down to the search for a stress function, called the Airy function A, which is bi- harmonic, that is to say ∆(∆A)=0. The expression