EECS 12 Assignment 5, Fall 2018 Due: 5pm, Dec 7, Friday
Description:
In this final assignment, you are required to use all the knowledge you’ve acquired over this
quarter to construct a brick-breaker game shown below:
Before the game starts, your program must read from a text file and use the words to decide
the length, color and position of each brick based on each word’s string size.
When the game starts, the ball sits in the middle of the paddle. At the bottom of the screen the
player needs to enter the number of lives, and the program shows the message, the current
score, and a start button. When the START button is clicked, the ball will shoot upward with
random speed and direction. The START button should then be hidden until the user loses a
life, and then it should show up again. The life input should be replaced by a simple message
when the game is over.
1
The ball moves at constant speed unless it hits something. When the ball hits any brick,
paddle, or top/side wall, the direction of the ball bounces reversely either horizontally or
vertically. For example, if the ball moves to the right and downward and it hits left side of the
brick, the ball bounces to the left but still downward. If the ball moves up and leftward, and it
hits the top wall, the ball should bounce back to be downward but still leftward.
When the ball hits a brick, its score will be increased based on the word size in the brick. If
the moving paddle is unable to catch the ball and it goes down to the bottom, one life will be
lost. The ball and the paddle will be again placed in the middle of the screen as in the
beginning. If the user uses up all lives, the program shows a QUIT button to allow the user to
stop the program.
Requirements:
YOu are given three files: hw5_main.py, an unfinished hw5_lib.py, and document.txt. You
need to complete what’s missing in hw5_lib.py to make the game program, hw5_main.py,
work. Three classes are defined in the library, as specified below.
1. Paddle: A class for the pad rectangle with several methods to respond to the key by
moving it, to return its upper center point, and to place the rectangle back to the
horizontal center of the window. The paddle will bounce the ball by reversing the
direction of the ball horizontally and vertically.
1.1. Member of the class:
1.1.1. rectangle
1.1.1.1. Initial location: space_from_bottom pixels from the window
bottom, centering horizontally
1.1.1.2. Size: paddle_width pixels in width, paddle_height pixels in
height
1.1.1.3. Fill color: light green
1.1.1.4. Outline color: white
1.2. Method of the class:
1.2.1. moveByKey(self, key, win_width, offset): the paddle rectangle
moves for offset pixel based on your keyboard left/right key press. It
cannot move out of the left edge of the window if a user presses left key
and also not out of the right edge if right key is pressed.
1.2.2. getSurfaceCenter(self): it returns a Point representing the upper
center point of the paddle rectangle for the ball to move back.
1.2.3. resetToCenter(self, window): the paddle rectangle should move back
to horizontal center of the window
2. Brick: A class contains a rectangle which is a single brick with various colors and
lengths, and also contains a word extracted from a text file of which the length decides
the length and the color of a brick, respectively. All the brick rectangles are generated
in the beginning and one of them disappears when the ball hit any of its four edges.
2
2.1. Member of the class:
2.1.1. rectangle
2.1.1.1. Initial location: x, y , two pixel input parameters
2.1.1.2. Size: width, height , two pixel input parameters
2.1.1.3. Color: color, a string input parameter
2.1.2. text: a string parameter
2.2. Method of the class:
2.2.1. getScore(self): a score calculated by each lowercase alphabet character,
e.g. if a word “bad” then its score is 1 + 0 + 3 = 4 (hint: use ASCII code
distance from that of “a”)
3. Ball: A class contains the yellow ball
3.1. Member of the class:
3.1.1. circle
3.1.1.1. Initial center location: radius pixels up of the paddle surface center
point given the radius and the paddle input parameters
3.1.1.2. Color: yellow
3.1.2. direction: a list with 2 numbers which are the pixels to move in x
direction in the first place and the pixels to move in y direction in the
second.
3.2. Methods of the class:
3.2.1. moveIt(self): move the ball circle using the direction two-item list
exactly once.
3.2.2. resetToPaddle(self, paddle): Given the paddle parameter, move the
ball circle back to the initial center location as in requirement 3.1.1.1
3.2.3. setRandomDirectionSpeed(self, min_speed = 0.85, max_speed =
3.0): Given the min_speed and max_speed parameters, randomly
generate the member direction as a two-item list where the first item in
either the range of min_speed and max_speed for rightward initial
movement or the range of the negative -min_speed and -max_speed for
leftward initial movement, and the second item in the range of negative
-min_speed and -max_speed for upward initial movement. If the
min_speed and max_speed is not given, use the default value 0.85 and 3.0,
respectively.
3.2.4. getDirectionSpeed(self): This method returns the member direction
list.
3.2.5. setDirectionSpeed(self, d): This method replaces the member
direction list with the input new list d.
3.2.6. reverseX(self): It changes the ball’s horizontal moving direction by
reversing the number sign of the first item of the direction list to reverse
the ball from rightward to leftward or vice versa.
3
3.2.7. reverseY(self): It changes the ball’s vertical moving direction by
reversing the number sign of the second item of the direction list to reverse
the ball from upward to downward or vice versa.
3.2.8. checkHitWindow(self, window): It checks whether the ball circle goes
outside the upper and side window walls. It returns True if part of the ball
is outside or False otherwise the whole ball is still inside the window.
3.2.9. checkHit(self, rectangle): As the following diagram, this method of
the ball first checks whether the ball circle center point resides in the green
area which is either half of the width of the given blue rectangle plus its
circle radius (w/2 + r) away or half of the height of the given rectangle
plus the circle radius (h/2 + r) away. If so, this method returns False to
indicate the ball circle will not hit the rectangle. After the first test, it
next checks whether the ball falls in the yellow area which means either
the horizontal absolute distance between the ball circle center point and
the rectangle center point is not more than half of the width of the
rectangle (< w/2), or the vertical absolute distance between the ball center
point and the rectangle center point is no more than the half of the
height of the rectangle (< h/2). If either of the two conditions hold, return
True. Last but not the least, it decides whether the ball touches the corner
point at the white sector area instead of red area by comparing the square
distance between ball center point and the corner point of the rectangle
with the square radius of the ball (r ** 2).
The following functions also must be implemented.
4. setupMessageScoreAndLifeInput: function to generate several graphic widgets, the
message text, score number text, life label text, and life entry input.
4.1. Input parameter:
4
4.1.1. window: The window created in hw5_main.py
4.1.2. offset_from_center_x: The horizontal offset of a graphic text/entry from
the center of the window.
4.1.3. offset_from_bottom: The vertical offset of a graphic text/entry from the
bottom of the window.
4.2. Action:
4.2.1. Create the white Text of score label, “SCORE: ”, which is
offset_from_center_x pixels to the right of the center of the window
and offset_from_bottom pixels up of the bottom of the window.
4.2.2. Create the white Text of score number “0” which is two times of the
offset_from_center_x pixels to the right of the center of the window
and offset_from_bottom pixels up of the bottom of the window.
4.2.3. Create the white Text of life label, “LIFE: ”, which is two times of the
offset_from_center_x pixels to the left of the center of the window and
offset_from_bottom pixels up of the bottom of the window.
4.2.4. Create the white Entry input of life for user to enter the number of lives in
total which locates at offset_from_center_x pixels to the left of the
center of the window and offset_from_bottom pixels up of the bottom
of the window.
4.2.5. Create the red empty Text for the message display which is at the center of
the window and two times of the offset_from_bottom pixels up of the
bottom of the window.
4.2.6. Returns the message text, score number text, life label text, and life entry
input in order.
5. getLinesOfWords function:
5.1. Input parameter:
5.1.1. filename: the name of the file to read
5.2. Action:
5.2.1. Open the file and read it line by line.
5.2.2. For each line, clean it by first replacing all the non-alphabet and the
non-number characters with space characters. After splitting the words out
by space, generate a list of long words of which the length is at least 2
characters and at most 8 characters.
5.2.3. Output the two dimensional list containing lines of valid words
6. makeLifeStatic function:
6.1. Input parameter:
6.1.1. window: The window created in hw5_main.py
6.1.2. life_input: the life entry input created in requirement 4.2.4
6.2. Action:
6.2.1. Hide the life entry input and then create and display the life number text in
the same place occupied by the liffe entry input.
5
7. updateScore function:
7.1. Input parameter:
7.1.1. score_offset: a number which is the score offset to be added into the
score.
7.1.2. score_num_text: the string text of score number created in requirement
4.2.2
7.2. Action:
7.2.1. Update the score number by extracting it from the graphic
score_num_text, adding it with the score_offset, and putting it back to
the text.
Part 1. First you complete getSurfaceCenter(self) in requirement 1.2.2 to return the
center point of the upper edge of the paddle rectangle. Then you fulfill the function
setupMessageScoreAndLifeInput in requirment 4. With only the variable PART1 at the top
of hw5_main.py set as True, and others as False, you should see the following game window
when running it.
6
Part 2. Implement the function getLinesOfWords as in requirement 5 and turn only the
variable PART2 to be True. In addition to the game main window, you should see the result of
the 2D lists printed on your shell IDLE terminal window like the following graph.
Part 3. With the completed function makeLifeStatic, when you set only the PART3 as True,
you should see the full start game window as following graph.
After entering the life number in the entry box and pressing the START button, it should turn
the input into the read-only text, clean the message text, and hide the START button at the
same time as the following two graphs shown.
7
Part 4. In this part, you should implement the method moveByKey of the Paddle class in
requirement 1.2.1. In consequence, you allow the paddle to be controllable by left or right key.
It should be similar to the following graph.
Part 5. You should write the methods moveIt, setRandomDirectionSpeed,
getDirectionSpeed, setDirectionSpeed, reverseX, reverseY in the Ball class
(requirement 3.2). Set only the PART5 to True, and the ball should be launched and it will goes
out of the window.
8
Further complete checkHit, checkHitWindow in the Ball class, the ball should be bounced
back when hitting either the window or any brick with the PART5 being True. It’s normal
when the ball touches and stays at the bottom of the window and the paddle stays at where it
is without moving back to the initial position as the following graph before you finish writing
the two methods, resetToCenter in the Paddle class and resetToPaddle in the Ball class.
With the two methods implemented and PART5 still being True, you should be able to see the
paddle and the ball reset back to the initial place but the score is always 0 even though the ball
has hit several bricks.
9
Part 6. The final part asks you to complete the method getScore of the Brick class and the
function updateScore, and at last you should be able to play the game with the score
calculated under all PART1~5 variables to be FALSE. The following graph shows an example
of the game over window with the score added and the QUIT button is revealed.
10
Grading Criteria (100 points):
● Program can run without error and comments on the top of the program include your
name, ID (5 pts).
● Part 1 (10 pts)
● Part 2 (20 pts)
● Part 3 (10 pts)
● Part 4 (5 pts)
● Part 5 (40 pts)
● Part 6 (10 pts)
Submission:
Submit your homework before 5pm, Dec 7 (Friday) to the canvas website. Submit only the
Python source file with filename “hw5_lib.py”.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。