Complete generations 0 and 1 of Trux Falsy.
Challenge
Complete additional generations of Trux Falsy.
Collision detection has the reputation of being one of the most difficult parts of creating a good video game. There are many different ways to determine whether two entities are colliding. One of them involves circles (similar to the point in circle program from last class). Another, very trivial type of collision detection is simply determining if a point is located within a given rectangle.
Details
In a Python file point_in_rectangle.py, define a function
is_point_in_rectangle(point_x, point_y,
rectangle_center_x, rectangle_center_y,
rectangle_width, rectangle_height)
. This function
takes 6 parameters. The first two parameters are
integers x and y, a point in 2 dimensional space. The next two
parameters (rectangle_center_x
and rectangle_center_y
) are integers representing the
point in the very center of a rectangle in 2 dimensional space.
The last two parameters (rectangle_width
and rectangle_height
) are integers representing the
width and height of the rectangle.
Your function should return True if the specified point is inside the specified rectangle. Consider a point exacty on the border of the rectangle as inside the rectangle. Your function should return False if the point is not inside the rectangle. Make sure your function always returns True or False! A None value should never be returned, and printing "True" or "False" does not count.
Test Cases
Function Call | Expected Output |
---|---|
is_point_in_rectangle(0, 0, 0, 0, 2, 2) |
True |
is_point_in_rectangle(1, 0, 0, 0, 2, 2) |
True |
is_point_in_rectangle(-1, 0, 0, 0, 2, 2) |
True |
is_point_in_rectangle(0, 1, 0, 0, 2, 2) |
True |
is_point_in_rectangle(0, -1, 0, 0, 2, 2) |
True |
is_point_in_rectangle(1, 1, 0, 0, 2, 2) |
True |
is_point_in_rectangle(-1, -1, 0, 0, 2, 2) |
True |
is_point_in_rectangle(-1, 1, 0, 0, 2, 2) |
True |
is_point_in_rectangle(1, -1, 0, 0, 2, 2) |
True |
is_point_in_rectangle(2, 0, 0, 0, 2, 2) |
False |
is_point_in_rectangle(-2, 0, 0, 0, 2, 2) |
False |
is_point_in_rectangle(0, 2, 0, 0, 2, 2) |
False |
is_point_in_rectangle(0,-2, 0, 0, 2, 2) |
False |
is_point_in_rectangle(-3, 2, -3, 2, 1, 1) |
True |
is_point_in_rectangle(0, 1, -3, 2, 1, 1) |
False |
is_point_in_rectangle(2, 0, 0, 0, 4, 2) |
True |
is_point_in_rectangle(0, 2, 0, 0, 4, 2) |
False |
Hint
Challenge
Modify your program to be visual like our Point in Circle program from last class. Your program should draw a rectangle in the center of the screen that takes up about half of the window. Allow the user to click on the screen for up to 10 points. If the point the user clicked was inside the rectangle, it should be colored green. Otherwise it should be colored red.
One of the more interesting things that conditionals give us is the ability to interact with a graphical program in different weays. We can tell the difference between a mouse button being down vs. up, we can tell when a keyboard key is pressed, etc. This can also allow us to create programs which allows a user to express their...creativity!
Details
Create a python program in a file called paint.py. Your program should detect when the left mouse button is pressed, and the point that the mouse button was previously at to draw lines when the user drags the mouse.
Example
Hint
Challenge
Modify your program so that you can chose between different line colors, based on whether the user clicked on a rectangle defining that color.
Example
Submission
Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.