搜索
熱搜: 活動 交友 discuz
查看: 2842|回復: 0
打印 上一主題 下一主題

python設計

[複製鏈接]
跳轉到指定樓層
1#
發表於 2009-2-8 08:30:55 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
各位大大:
part1 我已經做好了
是part 2 跟3我不會~~
就拜託各為我幫想一想要怎麼寫了...

Part 1 - Pool Table
Write a function called table() that draws a pool table in the centre of the turtle graphics window.
You can draw the table by drawing two coloured rectangles (a green one on top of a brown one), and four coloured circles at the corners.  Note that the turtle.circle(r) function draws a circle of radius r starting at the current turtle position and heading.  If you want to centre the circle at a particular point you will have to bear this in mind.
The dimensions of the table should be as follows:
    * pool table (the green part) width: 400
    * pool table height (the green part again): 200
    * pool table border (the brown part): 20 pixels wide
    * pocket radius: 20
The pockets should be centred at the corners of the green rectangle, at these coordinates
    * top left: x = -200, y = 100
    * top right: x = 200, y = 100
    * bottom left: x = -200, y = -100
    * bottom right: x = 200, y = -100
Part 2 - One Pool Ball
Write functions to show the path of a pool ball on the table.  The pool ball should start in the centre of the table, move in the heading specified in a parameter to the function, and should bounce off the walls until it stops moving (i.e. it has moved a specified number of steps) or until it goes into one of the pool table pockets.  The path of the ball should be shown.
Function Requirements
You must write a function called one_ball(heading).  This function should call two other functions:
    *      table() - draws the pool table, the function that you wrote in part 1
    *      bounce(steps, heading) - moves the ball steps times, the ball initially has a heading of heading, passed through from one_ball
These two functions may also call other functions as required
When you call  bounce from within one_ball I would suggest a steps value of 2,000.  The ball should move as follows each step:
    *      Move forward 1 pixel unless it reaches a pocket or hits a wall
    *      If the ball reaches the edge of the (green) table surface it should bounce off the wall as described below
    *      The ball should stop moving if it reaches a pocket and the program should terminate
    *      The ball should stop moving if it has moved steps times and the program should terminate
Implementing The Bounce Function - Make sure you read this section carefully!
It is important to structure the bounce function so that it will be easy to modify in the next part of the project.  Here is the pseudocode for this function:
Create a new turtle pen (let's call it t1)
Change the pen's colour (to a colour of your choice)
Change the pen's shape to a circle
Set current (a variable) to 0
while (current < steps) and (not(in_pocket(t1)))
     move_ball(t1)
Creating a New Pen
You should implement the pool ball using a new turtle pen.  This will make it a lot easier to implement the rest of the project where you will need a second ball (i.e. a turtle pen).  To create and use a turtle pen assign the result of the Pen() function to a variable and then call turtle graphics functions as normal except that the function names are preceded by the variable name and a dot instead of "turtle.".  Here is an example, not related to the project, that draws a pink line:
t1 = turtle.Pen()
t1.color('pink') #a pink pen
t1.shape('circle')#makes the "turtle" circular
t1.forward(100)
#and so on ...
The in_pocket(t) Function
The in_pocket function has a turtle pen parameter (that I called t), and should be passed the name of the pen that was created previously.  The function returns True or False, indicating whether or not the ball has reached any one of the pool table corner pockets.  We will say that the ball is "in" the pocket when the turtle's position is within 20 pixels of the centre of a pocket (and you can see part 1 for where the pockets should be drawn).

You might want to look at the trigonometry section at the end of this part for some assistance.
The move_ball(t) Function
The body of the while loop in one_ball moves the turtle a single pixel forward and, if necessary, changes its heading.  This is all carried out by the move_ball function which has a turtle pen parameter (that I called t).  This function should do the following:
if the ball has reached the edge of the table
     set heading to bounce off the wall
move one pixel forward

Trigonometry Review
Here are a couple of hints that may help you determine if the turtle is within 20 pixels of the centre of a pocket, and calculate the new heading of the turtle once it hits a wall.
Distance Between Two Points
The distance betwen two points (cx, cy) and (x, y) is given by:
math.sqrt(math.fabs(x - cx)**2 + math.fabs(y - cy)**2)
Bouncing
Here is a diagram of what we want to happen if the turtle hits the right wall.
Let's say that the turtle hits the right wall while on a heading of 20 degrees (the heading correspond to the angle a in the diagram).  To make the turtle bounce off the wall we make it turn left by 180 -2a degrees.
Finally, let's briefly review how Turtle Graphics keeps track of direction.  When the turtle first appears (or is reset) it points to the right (or the east).  Turtle Graphics records this heading as 0.  Turning the turtle left by 50 degrees would give it a heading of 50.  If the turtle was facing due north (up) it would have a heading of 90, west (left) 180, south (down) 270 and so on.
To find the current heading of the turtle call:
turtle.heading()

Part 3 - Two Balls
For the last part of the project you will add a second ball to the table.  This ball should be placed in the near the right hand wall, and it should be centred vertically.  This second ball should be stationary initially but should start moving if it is hit by the first ball.  Assume that the balls hit if their positions are within 20 pixels of each other.  The modified behaviour for each step should be as follows:

Note: Your program should give the impression that both balls are moving at the same time.  In fact, your main while loop (in a function called bounce_two) should be moving both balls, one ball at a time, one pixel at a time which gives the appearance that both balls are moving together.  If you used the structure that I set out in part 2 (with a loop that moves the ball one pixel at a time) it shouldn't be too difficult to modify this for part 3.
Initial Behaviour

This occurs if the first ball (Ball1) has not hit the second ball (Ball2) yet
    *      Move Ball1 1 pixel unless it reaches a pocket or hits a wall
    *      If the Ball1 reaches the edge of the (green) table surface it should bounce off the wall
    *      Ball1 should stop moving if it reaches a pocket and the program should terminate
    *      Ball1 should stop moving if it has moved steps times and the program should terminate
Behaviour if Ball1 hits Ball2
    * Make Ball2's heading the same as Ball1's heading + a random amount between -20 and +20
    * Make Ball1 turn left through 180 + a random amount between -20 and +20 (so between 160 and 200 degrees)
    * Move Ball2 forward 5 pixels (to get it out of the way of Ball1)

Behaviour after Ball1 first hits Ball2

    *      Move Ball1 1 pixel unless it reaches a pocket or hits a wall
    *      If the Ball1 reaches the edge of the (green) table surface it should bounce off the wall
    * Ball1 should stop moving if it reaches a pocket
    *      Move Ball2 1 pixel unless it reaches a pocket or hits a wall
    *      If the Ball2 reaches the edge of the (green) table surface it should bounce off the wall
    *      Ball2 should stop moving if it reaches a pocket
    * The program should terminate if both Ball1 and Ball2 have reached a pocket
    *      Ball1 and Ball2 should stop moving after the move process has been run steps times and the program should terminate
Function Requirements
You must write a function called two_balls(heading).  This function should call two other functions:
    *     table() - draws the pool table, the function that you wrote in part 1
    *      bounce_two(steps, heading) - moves the ball steps times, the first ball initially has a heading of heading, passed through from two_balls
Implementing Two Balls
You should be able to use the same basic structure as your bounce function from part 1.  Here are a few hints about what changes you will have to make:
The bounce-two Function
This can be similar to the original bounce function except that
    * You will need to create a second pen that represents the second ball
    * The while loop condition is (considerably) more complex, you should carefully determine when the program should terminate.  I found it very helpful to create two boolean variables called ball1moving and ball2moving that represent whether or not the two balls are moving.  Initially I set ball1moving to True and ball2moving to False.  These two variables can be used in the while condition and in the body of the loop to determine whether or not the balls should move.
    * Inside the while loop you need to determine if a ball should be moved.  If a ball should be moved then you should call a new move_two_balls function and check, using in_pocket, to see if the ball has reached a pocket.

The in_pocket(t) Function
You shouldn't have to change this function at all.  Note that you can use the same function to test either ball, as its turtle pen parameter will refer to whichever turtle pen you pass to it.
The move_two_balls(this_t, other_t) Function
You will need to write a new move_two_balls function.  This will be very similar to the original move_ball function except that it needs two turtle pen parameters that represent the two balls (this_t is the one currently being moved, and other_t is the other one.  As well as moving a ball and changing its heading if it reaches a wall, this function must also detect if the one ball hits the other and change their headings if they do so.

This function is also going to have to inform the rest of your program if Ball2 has been hit, so it should return a boolean value to indicate whether or not the balls have hit.  If so you will need to set ball2moving to True.
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-5-18 13:22 , Processed in 0.016840 second(s), 18 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表