I already tried to google it but I couldn't find anything ...
I created tkinter canvas with width 800, height 600
apparently, the left upside will be (0,0)
I want to change left downside to (0,0)
How can I do this???
You cannot change the coordinate system of the canvas. You can scroll the canvas so that 0,0 is in the bottom left, but the y coordinates will be negative going up.
In order to do that, you have to define your own function. Such:
def conv( coordinatePair ):
return { "x":coordinatePair["x"], "y":canvas.height-coordinatePair["y"]}
Where coordinatePair is a dictionary. That function will take your coordinate, flip the y, and return a new coordinate dictionary.
Related
I am trying to draw a button on a bitmap object. Depending on the y position, it should draw on bitmap1 if the y position is within bmp1's height value, and bitmap2 if it isn't.
For some reason this does not work:
wx.Button(bitmap1 if ypos <= bmp1.GetHeight() else bitmap2, label='Run', id=i, pos=(xpos, ypos))
I can only draw the button on one wx.StaticBitmap image or the panel. The images parents are the panel.
This works fine if I want to switch between the bitmap or onto the panel directly.
What gives?
NOTE:
I managed to work around this using PIL to create a dynamic image large enough to accomodate my generated buttons (a continuous y-size, according to their count and placement), however this idea/code should still be valid.
If I substitute the 'bitmap2' value for the panel, and shift the bitmap2 image drawn on the panel by a bit, then I see that the program draws underneath bitmap2. Why? The image is placed exactly like bitmap1, and bitmap1 has no problems being drawn on it by buttons? :O
I figured out the problem:
The button's parent object should get the ypos according to the parent's dimensions, not on where it is drawn on the frame, like so:
wx.Button(bitmap1 if ypos <= bmp1.GetHeight() else bitmap2, label='Run {i}', id=i, pos=(80, ypos if ypos <= bmp1.GetHeight() else ypos-img_height))
ypos if ypos <= bmp1.GetHeight() else ypos-img_height
Finally!
I was going through a tutorial here - www.raywenderlich.com/24252/beginning-game-programming-for-teens-with-python. In Step 4, he used the following statement :
# 6.1 - Set player position and rotation
position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26))
playerrot = pygame.transform.rotate(player, 360-angle*57.29)
playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
screen.blit(playerrot, playerpos1)
what exactly did he do? and why did he do so?
The coordinates that you pass to pygame.Surface.blit are the coords of the top left corner. I think the playerpos in Step 4 should be the center position of the image/surface, so you need to subtract the half width and the half height of the surface to get the top left coords (where you want to blit it). The author uses the pygame.Surface.get_rect method to get a rect with the width and height of the rotated surface, but he could use the pygame.Surface.get_width and .get_height methods as well.
After each rotation, the playerrot surface will have a different size, so you need to call the get_rect method again to get a new rect with the updated width and height which you need to adjust the coordinates.
By the way, it looks like adding 32 and 26 in this line causes problems with the rotation.
angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26))
This should work correctly:
angle = math.atan2(position[1]-playerpos[1], position[0]-playerpos[0])
It's also possible to use a pygame.Rect as the blit position, so you could do this to create a rect with the size of the rotated surface and the playerpos as the center coordinates:
playerpos1 = playerrot.get_rect(center=playerpos)
I think get_rect() is a method to get the rectangle object and then he's getting its width property to calculate the position on next player. I guess this method is defined somewhere in the code. Maybe see rest of the code too.
He's using it as a means to calculate the movement of the bunny that occurs from being rotated, by getting the rectangle for the bunny image and splitting it in half to get the (x, y) changes in position for the bunny:
------ ---------
|X | ----> Rotate 90 degrees ----> | X|
| | | |
------ ---------
I have a 10 x 10 GridLayout with 100 Image widgets, and on_touch_down, I want the picture that was touched to change to a different picture. Since the touch signal will bubble through GridLayout and all its 100 Image children, I want to do a check in on_touch_down to see if the touch coordinates are within the area occupied by the Image that was touched. How can I find the vertices of the Image, or is there an alternative to my approach? Calculating each Image's four vertices as they are added would be rather difficult since I am stretching these Images.
Many thanks in advance. :)
self.pos and self.size would have sufficed. How silly of me.
The collide_point method looks to be well suited for this.
For example:
def on_touch_down(self, touch):
x, y = touch
for child in self.children():
if child.collide_point(x, y):
do_something
From the docs:
Check if a point (x, y) is inside the widget's axis aligned bounding
box.
:Parameters:
`x`: numeric
X position of the point (in window coordinates)
`y`: numeric
Y position of the point (in window coordinates)
:Returns:
bool, True if the point is inside the bounding box.
..
>>> Widget(pos=(10, 10), size=(50, 50)).collide_point(40, 40)
True
I want to move the coordinates on a tkinter canvas so that the bottom left corner is (0, 0), meaning that the top right corner is (height, width).
How would I do this?
Simplest way is to write a function that inverts your vertical coordinate, given the canvas height and desired element height passed in.
You can't. You'll have to do math on all of your coordinates to translate them from one coordinate system to another.
I'm working on a breakout clone and I've been trying to figure out how to get the intersection rect of two colliding rects so I can measure how deep the ball entered the block in both x and y axis and decide which component of the velocity I'll reverse.
I figured I could calculate the depth for each case like this:
But if I had the intersection rect than I woudn't have to worry if the ball hits the block from the left/right or top/bottom (since I would be only reversing the x and y axis respectively), thus saving me a lot of typing.
I've looked on Pygame's docs but seems it doesn't have a function for that. How would I go about solving this problem?
Assuming you have rectangles r1 and r2, with .left, .right, .top, and .bottom edges, then
left = max(r1.left, r2.left);
right = min(r1.right, r2.right);
top = max(r1.top, r2.top);
bottom = min(r1.bottom, r2.bottom);
(with the usual convention that coordinates increase top to bottom and left to right). Finally, check that left<right and top<bottom, and compute the area:
Area = (right - left) * (top - bottom);
Alternatively, you can use the clip() function. From the docs you linked in your question:
clip(Rect) -> Rect Returns a new rectangle that is cropped to be
completely inside the argument Rect. If the two rectangles do not
overlap to begin with, a Rect with 0 size is returned.