PyGame polygon, using a tuple as a parameter - python

I want to draw a polygon on screen, like so:
coords = (653,333),(680,444),(680,444),(653,445)
pygame.draw.polygon(screen, (0, 0, 255), coords, 0)
however this does not draw to the screen. Any help appreciated.
ANSWER: coords should be a list of tuples, so for my example the correct answer would be:
coords = [(653,333), (680,444), (680,444), (653,445)]
pygame.draw.polygon(screen, (0, 0, 255), coords, 0)

The code that you gave seems to be correct. I've tested it and it displays a triangle. As suggested by skrx in the comment your screen size is too small and you are not able to see it.
import pygame
pygame.init()
BLUE = (0, 0, 255)
size = [1920, 1080]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
while not done:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
temp = (653, 333), (680, 444), (680, 444), (653, 445)
pygame.draw.polygon(screen, BLUE, temp, 0)
pygame.display.flip()
pygame.quit()

Related

Pygame colliderect returning true way too soon [duplicate]

I have created an image's rectangle of a sprite using the command, pygame.image.get_rect(). When locating coordinates of key points on the rectangle such as bottomleft by drawing dots, I see that the rectangle's dimensions are completely different to the image's dimensions. In this case, the image is just the arrow head. The top right coordinate of the rectangle is correct, however the bottom left coordinate isn't.
Test to see rectangle
code to draw dots:
pygame.draw.circle(simulation_screen, red, velocity_arrow.rect.topright, 2)
pygame.draw.circle(simulation_screen,red,velocity_arrow.rect.bottomleft,2)
How can I resize the rectangle so that it fits the image?
pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object. This function does not consider the drawing area in the image. You can find the bounding rectangle of the painted area in the surface, with pygame.Surface.get_bounding_rect:
bounding_rect = image.get_bounding_rect()
bounding_rect.move_ip(target_rect.topleft)
See the example. The black rectangle is the Surface rectangle and the red rectangle is the union of the mask component rectangles:
repl.it/#Rabbid76/ImageHitbox
import pygame
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
try:
my_image = pygame.image.load('icon/Bomb-256.png')
except:
my_image = pygame.Surface((200, 200), pygame.SRCALPHA)
pygame.draw.circle(my_image, (0, 128, 0), (60, 60), 40)
pygame.draw.circle(my_image, (0, 0, 128), (100, 150), 40)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pos = window.get_rect().center
my_image_rect = my_image.get_rect(center = pos)
bounding_rect = my_image.get_bounding_rect()
bounding_rect.move_ip(my_image_rect.topleft)
window.fill((255, 255, 255))
window.blit(my_image, my_image_rect)
pygame.draw.rect(window, (0, 0, 0), my_image_rect, 3)
pygame.draw.rect(window, (255, 0, 0), bounding_rect, 3)
pygame.display.flip()
pygame.quit()
exit()
I have the same problem, and I think because of this line:
image = pygame.transform.scale(image, (x, y))
After scaling the image in gimp, everything is working as intended.

How can I change the hitbox of my character if I'm using self.image.get_rect()? [duplicate]

I have created an image's rectangle of a sprite using the command, pygame.image.get_rect(). When locating coordinates of key points on the rectangle such as bottomleft by drawing dots, I see that the rectangle's dimensions are completely different to the image's dimensions. In this case, the image is just the arrow head. The top right coordinate of the rectangle is correct, however the bottom left coordinate isn't.
Test to see rectangle
code to draw dots:
pygame.draw.circle(simulation_screen, red, velocity_arrow.rect.topright, 2)
pygame.draw.circle(simulation_screen,red,velocity_arrow.rect.bottomleft,2)
How can I resize the rectangle so that it fits the image?
pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object. This function does not consider the drawing area in the image. You can find the bounding rectangle of the painted area in the surface, with pygame.Surface.get_bounding_rect:
bounding_rect = image.get_bounding_rect()
bounding_rect.move_ip(target_rect.topleft)
See the example. The black rectangle is the Surface rectangle and the red rectangle is the union of the mask component rectangles:
repl.it/#Rabbid76/ImageHitbox
import pygame
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
try:
my_image = pygame.image.load('icon/Bomb-256.png')
except:
my_image = pygame.Surface((200, 200), pygame.SRCALPHA)
pygame.draw.circle(my_image, (0, 128, 0), (60, 60), 40)
pygame.draw.circle(my_image, (0, 0, 128), (100, 150), 40)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pos = window.get_rect().center
my_image_rect = my_image.get_rect(center = pos)
bounding_rect = my_image.get_bounding_rect()
bounding_rect.move_ip(my_image_rect.topleft)
window.fill((255, 255, 255))
window.blit(my_image, my_image_rect)
pygame.draw.rect(window, (0, 0, 0), my_image_rect, 3)
pygame.draw.rect(window, (255, 0, 0), bounding_rect, 3)
pygame.display.flip()
pygame.quit()
exit()
I have the same problem, and I think because of this line:
image = pygame.transform.scale(image, (x, y))
After scaling the image in gimp, everything is working as intended.

Why my rectrangle does not appere(python 3.8 windoes 7) [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
I tried to make a clone of a game but the rectangle (which is the basic shape that I will need)does not appear. What did I do wrong or its just pygame going crazy?
code:
# importing modules
import pygame
import sys
import random
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.display.quit()
pygame.quit()
exit()
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# cube
if running == True:
pygame.draw.rect(screen, blue, (395, 0, 10, 10)),
pygame.draw.rect(screen, blue, (395, 10, 10, 10)),
pygame.draw.rect(screen, blue, (395, 20, 10, 10)),
clock.tick(60)
and also how am I going to make it empty and 3d. I know I am asking for a lot but I believe someone can explain
You have to draw the scene in the application loop:
# importing modules
import pygame
import sys
import random
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.draw.rect(screen, blue, (395, 0, 10, 10))
pygame.draw.rect(screen, blue, (395, 10, 10, 10))
pygame.draw.rect(screen, blue, (395, 20, 10, 10))
pygame.display.update()
clock.tick(60)
pygame.display.quit()
pygame.quit()
exit()
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick

Questions regarding the pygame.mask function

So I read the documentation of pygame but I could not understand it clearly. I recently asked a question about bitmap fonts and I got some code as my answer; here is the code:
import pygame
pygame.init()
win = pygame.display.set_mode((800, 600))
font = pygame.font.Font("freesansbold.ttf", 32)
i = 0
text = "hello how are you?"
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
letter = text[i]
text_1 = font.render(letter, True, (255, 255, 255))
bw, bh = font.size(letter)
glyph_rect = pygame.mask.from_surface(text_1).get_bounding_rects()
# print(glyph_rect)
if glyph_rect:
gh = glyph_rect[0].height
print(f'letter {letter} bitmap height: {bh} glyph height: {gh}')
win.fill((0, 0, 0))
win.blit(text_1, (0, 0))
pygame.display.update()
i += 1
run = i < len(text)
pygame.quit()
So, my questions are on the line glyph_rect = pygame.mask.from_surface(text_1).get_bounding_rects().
What does the pygame.mask.from_surface() function do?
What does the line glyph_rect = pygame.mask.from_surface(text_1).get_bounding_rects() do?
What arguments does the variable glyph_rect return, and what is the meaning of those arguments?
pygame.mask.from_surface creates a pygame.mask.Mask object form a pygame.Surface.
A Surface is bitmap. A Mask is an 2 dimensional array with Boolean values. The Mask created is the size of the _Surface. A field is True if the corresponding pixel in the surface is not transparent, and False if it is transparent.
pygame.mask.Mask.get_bounding_rects creates a list of pygame.Rect objects. Each rectangle describes a bounding area of connected pixles.
If the Surface contains exactly 1 connected image, you will get exactly 1 rectangle surrounding the image.
See the example. The black rectangle is the Surface rectangle and the red rectangle is the bound rectangle of the connected component:
repl.it/#Rabbid76/ImageHitbox
import pygame
def getMaskRect(surf, top = 0, left = 0):
surf_mask = pygame.mask.from_surface(surf)
rect_list = surf_mask.get_bounding_rects()
surf_mask_rect = rect_list[0].unionall(rect_list)
surf_mask_rect.move_ip(top, left)
return surf_mask_rect
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
try:
my_image = pygame.image.load('Bomb-256.png')
except:
my_image = pygame.Surface((200, 200), pygame.SRCALPHA)
pygame.draw.circle(my_image, (0, 128, 0), (60, 60), 40)
pygame.draw.circle(my_image, (0, 0, 128), (100, 150), 40)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pos = window.get_rect().center
my_image_rect = my_image.get_rect(center = pos)
my_image_mask_rect = getMaskRect(my_image, *my_image_rect.topleft)
window.fill((255, 255, 255))
window.blit(my_image, my_image_rect)
pygame.draw.rect(window, (0, 0, 0), my_image_rect, 3)
pygame.draw.rect(window, (255, 0, 0), my_image_mask_rect, 3)
pygame.display.flip()
pygame.quit()
exit()

Line drawing in pygame

I am trying to draw a line in pygame that has a rounded cap unlike the image below. Is there a way I can do this. Ideally I would want a smooth line with no breaks and a rounded edge as opposed to the flat edge it currently has. My current code for drawing a line is as follows
pygame.draw.line(window, (0, 0, 0), start, end, 50)
The best I can think of is to use pygame.draw.lines() and draw additional circles (pygame.draw.circle) at the joints:
points = [(100, 100), (300, 150), (250, 300)]
pygame.draw.lines(window, (0, 0, 0), False, points, 50)
for p in points:
pygame.draw.circle(window, (0, 0, 0), p, 25)
See also Paint
Minimal example:
repl.it/#Rabbid76/PyGame-PaintFreeThickLine
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
lines = []
draw = False
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
draw = not draw
if draw:
lines.append([event.pos])
if event.type == pygame.MOUSEMOTION and draw:
lines[-1].append(event.pos)
window.fill((255, 255, 255))
for points in lines:
if len(points) > 1:
pygame.draw.lines(window, (0, 0, 0), False, points, 50)
for p in points:
pygame.draw.circle(window, (0, 0, 0), p, 25)
pygame.display.flip()
pygame.quit()
exit()

Categories