Python: How to make drawn elements snap to grid in pygame - python

I am experimenting with a Pathfinding project and got stuck while creating a working GUI. I'm using pygame and already created a grid and a feature, which draws cubes when you press (or keep pressing) the mouse-button. However, these cubes just go wherever you click, and do not snap to the grid. I thought about using modulo somehow but I cannot seem to get it to work. Please find the code attached below. The Cube class is what I use for the squares drawn on the screen. Moreover, the drawgrid() function is how I set up my grid. I'd love some help on this, as I've been stuck on this roadblock for three days now.
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 20, 20)
def draw(self):
click = pygame.mouse.get_pressed()
if click[0]: # evaluate left button
pygame.draw.rect(screen, (255, 255, 255), self.square)
Other drawgrid() function:
def drawgrid(w, rows, surface):
sizebtwn = w // rows # Distance between Lines
x = 0
y = 0
for i in range(rows):
x = x + sizebtwn
y = y + sizebtwn
pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, w))
pygame.draw.line(surface, (255, 255, 255), (0, y), (w, y))

You have to align the position to the grids size. Use the floor division operator (//) to divide the coordinates by the size of a cell and compute the integral index in the grid:
x, y = pygame.mouse.get_pos()
ix = x // sizebtwn
iy = y // sizebtwn
Multiply the result by the size of a cell to compute the coordinate:
self.cx, self.cy = ix * sizebtwn, iy * sizebtwn
Minimal example:
import pygame
pygame.init()
screen = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
def drawgrid(w, rows, surface):
sizebtwn = w // rows
for i in range(0, w, sizebtwn):
x, y = i, i
pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, w))
pygame.draw.line(surface, (255, 255, 255), (0, y), (w, y))
class Cube:
def update(self, sizebtwn):
x, y = pygame.mouse.get_pos()
ix = x // sizebtwn
iy = y // sizebtwn
self.cx, self.cy = ix * sizebtwn, iy * sizebtwn
self.square = pygame.Rect(self.cx, self.cy, sizebtwn, sizebtwn)
def draw(self, surface):
click = pygame.mouse.get_pressed()
if click[0]:
pygame.draw.rect(surface, (255, 255, 255), self.square)
cube = Cube()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
cube.update(screen.get_width() // 10)
screen.fill(0)
drawgrid(screen.get_width(), 10, screen)
cube.draw(screen)
pygame.display.flip()

Related

How to subtract geometry in pygame?

I am trying to make a logic in which i can subtract the geometrical shapes. Lets say i have to subtract a circle from a rectangle.
The idea is to draw each point of the rectangle as a single individual point using win.set_at(). And then when we need to subtract the circle from the rectangle we an simply calculate that the point on which we are drawing and completing our square lies or not in the circle. If it lies then don't set pixels else draw the pixel. This is my code:
import pygame
WIDTH, HEIGHT = 700, 500
win = pygame.display.set_mode((WIDTH, HEIGHT))
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
def drawer(window, x, y, radius, s_l, s_w, s_x, s_y):
for i in range(0, HEIGHT):
for j in range(0, WIDTH):
distance = (x - j)**2 + (y - i)**2
if not distance <= radius**2 and i >= s_y and j >= s_x and i <= s_w + s_x and j <= s_l + s_y:
window.set_at((j, i), BLACK)
c_x, c_y = 130, 150
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill(WHITE)
drawer(win, c_x, c_y, 30, 100, 100, 100, 100)
pygame.display.flip()
pygame.quit()
But is it is quite slow. Can you suggest a better way (more optimized algorithm) of doing it.
For a good performance I suggest to use pygame.mask. Draw the shapes on pygame.Surface objects and create masks from the surfaces:
mask1 = pygame.mask.from_surface(shape1)
mask2 = pygame.mask.from_surface(shape2)
Invert the mask of the second shape and create the overlapping mask. Finally, create a new _Surface: from the overlapping mask:
mask2.invert()
subtract_maks = mask1.overlap_mask(mask2, (0, 0))
subtract_shape = subtract_maks.to_surface(setcolor = "red", unsetcolor=(0, 0, 0, 0))
Minimal example
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
shape1 = pygame.Surface((200, 200), pygame.SRCALPHA)
pygame.draw.circle(shape1, "white", (100, 100), 100)
shape2 = pygame.Surface((200, 200), pygame.SRCALPHA)
pygame.draw.rect(shape2, "white", (25, 50, 150, 100))
mask1 = pygame.mask.from_surface(shape1)
mask2 = pygame.mask.from_surface(shape2)
mask2.invert()
subtract_maks = mask1.overlap_mask(mask2, (0, 0))
subtract_shape = subtract_maks.to_surface(setcolor = "red", unsetcolor=(0, 0, 0, 0))
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (128, 128, 128), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles]
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))
window.blit(background, (0, 0))
window.blit(subtract_shape, (50, 50))
pygame.display.flip()
pygame.quit()
exit()

drawing lines but i don't see them on the screen

When i run this code, to try to draw a grid for a snake game:
import pygame
pygame.init()
pygame.display.set_caption('Snake')
class grid(object):
def __init__(self, rows, width, x, y):
self.rows = rows
self.width = width
self.x = x
self.y = y
self.sizeBetween = width // rows
def draw(self, surface):
for l in range(self.rows):
self.x += self.sizeBetween
self.y += self.sizeBetween
pygame.draw.line(win, (255, 255, 255), (self.x, 0), (self.x, self.width))
pygame.draw.line(win, (255, 255, 255), (0, self.y), (self.width, self.y))
def window(surface):
grid.draw(surface)
pygame.display.update()
grid = grid(20, 500, 0, 0)
win = pygame.display.set_mode((grid.width, grid.width))
run = True
while run:
win.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window(win)
It doesn't draw any lines. I'm new to pygame and don't really know how to fix problems like this, since it will just open a black box without any errors.
The grid is only drawn for a short moment in the 1st frame, because the attributes x and y are incremented continuously, but never reset.
def draw(self, surface):
for l in range(self.rows):
self.x += self.sizeBetween
self.y += self.sizeBetween
In the 2nd frame, the display is cleared, but the current value of the attributes x and y is self.rows * self.sizeBetween. Hence all the lines are drawn out of the window.
Change the drawing of the grid:
class Grid(object):
# [...]
def draw(self, surface):
x, y = self.x, self.y
for l in range(self.rows-1):
x += self.sizeBetween
y += self.sizeBetween
pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, self.width))
pygame.draw.line(surface, (255, 255, 255), (0, y), (self.width, y))
Side note, the name of the class grid and the instance gird are identical. This causes that the instance covers the class. Python is case sensitive and class names should normally use the CapWords convention. See Style Guide for Python Code - Class Names.
I recommend to change the name of the class:
class Grid(object):
# [...]
# [...]
grid = Grid(20, 500, 0, 0)

How to move the location of a grid in pygame

import pygame
pygame.init()
width = 800
height = 600
running = True
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Dijkstra's Path-Finding Algorithm Solver")
icon = pygame.image.load('icon.jpg')
pygame.display.set_icon(icon)
def title():
button_font = pygame.font.Font('TrajanPro-Regular.otf', 40)
rect_display = button_font.render('Dijkstra Path-Finding Algorithm', True, (255, 255, 255))
# display global total deaths
screen.blit(rect_display, (12, 10))
def title_underline():
# create the button
rect = pygame.Rect(0, 60, 800, 3)
rect_display = pygame.draw.rect(screen, [255, 255, 255], rect)
button_font = pygame.font.Font('TrajanPro-Regular.otf', 100)
rect_display = button_font.render('', True, (255, 255, 255))
# display global total deaths
screen.blit(rect_display, (270, 198))
def grid():
blockSize = 20 #Set the size of the grid block
for x in range(width):
for y in range(height):
rect = pygame.Rect(x*blockSize, y*blockSize, blockSize, blockSize)
pygame.draw.rect(screen, (200,200,200), rect, 1)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
title()
title_underline()
grid()
pygame.display.update()
I have created a title for my project and I created a grid for which I want to run my application, but the grid is being made in every single x, y location on my screen. But I want the grid to only be made under the title and not over it.
Define the top left coordinates of the grid (grid_x, grid_y) and add the coordinates when constructing the rectangle of a cell. For instance:
def grid():
grid_x = 12
grid_y = 30
blockSize = 20 #Set the size of the grid block
for x in range(width):
for y in range(height):
rect = pygame.Rect(
grid_x + x*blockSize, grid_y + y*blockSize,
blockSize, blockSize)
pygame.draw.rect(screen, (200,200,200), rect, 1)

Python/Pygame - Importing spritesheet as an array of indexed rects [duplicate]

So I've been testing out this code, I found a tutorial on how I can add spritesheets in pygame and decided to try this one:
https://www.spriters-resource.com/3ds/dragonballzextremebutoden/sheet/67257/
I did as the video said and counted the columns and rows, this is my code:
pygame.init()
CLOCK = pygame.time.Clock()
DS = pygame.display.set_mode((W, H))
FPS = 60
class spritesheet:
def __init__(self, filename, cols, rows):
self.sheet = pygame.image.load(filename).convert_alpha()
self.cols = cols
self.rows = rows
self.totalCellCount = cols * rows
self.rect = self.sheet.get_rect()
w = self.cellWidth = self.rect.width / cols
h = self.cellHeight = self.rect.height / rows
hw, hh = self.cellCenter = (w / 2, h / 2)
self.cells = list([(index % cols * w, index / cols * h, w, h) for index in range(self.totalCellCount)])
self.handle = list([
(0,0), (-hw, 0), (-w, 0),
(0, -hh), (-hw, -hh), (-w, -hh),
(0, -h), (-hw, -h), (-w, -h),])
def draw(self, surface, cellIndex, x, y, handle = 0):
surface.blit(self.sheet,
(x + self.handle[handle][0], y + self.handle[handle][1],
self.cells[cellIndex][2], self.cells[cellIndex][3]))
s = spritesheet('Number18.png', 58, 6)
CENTER_HANDLE = 6
Index = 0
#mainloop
run = True
while run:
s.draw(DS, Index % s.totalCellCount, HW, HH, CENTER_HANDLE)
Index +=1
#pygame.draw.circle(DS, WHITE, (HW, HW), 20, 10)
DS.blit(bg,(0,0))
pygame.display.update()
CLOCK.tick(FPS)
DS.fill(BLACK)
The line s = spritesheet("Number18.png", 58, 6) has the numbers 58, 6 which are basically the number of rows and columns I counted on this spritesheet block, but I'm getting problems such as the pygame window on "not responding", the image does not load up and I can't move the pygame screen.
I'm getting problems such as the pygame window on "not responding", [...]
The fist thing what you've to do is to add en event loop to the main loop of the application.
pygame.event removes a pending event message from the queue and returns it.
At least you should handle the QUIT event. Set the value of control variable for the main loop False:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
The tiles in the sprite sheet don't have equal size. Limit the cells list to some parts of the sheet which have equal size.
Try the following:
class spritesheet:
def __init__(self, filename, py, tw, th, tiles):
self.sheet = pygame.image.load(filename).convert_alpha()
self.py = py
self.tw = tw
self.th = th
self.totalCellCount = tiles
self.rect = self.sheet.get_rect()
w, h = tw, th
hw, hh = self.cellCenter = (w / 2, h / 2)
self.cells = [(1+i*tw, self.py, tw-1, th-1) for i in range(tiles)]
self.handle = list([
(0,0), (-hw, 0), (-w, 0),
(0, -hh), (-hw, -hh), (-w, -hh),
(0, -h), (-hw, -h), (-w, -h),])
s = spritesheet('Number18.png', 1085, 80, 134, 8)
[...] the image does not load up [...]
Make sure that the image is located int the working directory of the application.
If you want to draw a sub-image of a spritsheet then you've to set the area parameter (3rd parameter) of pygame.Surface.blit to the rectangle area of the sub-image:
def draw(self, surface, cellIndex, x, y, handle = 0):
hdl = self.handle[handle]
surface.blit(self.sheet, (x + hdl[0], y + hdl[1]), area=self.cells[cellIndex])
[...] I can't move [...]
You have to change the position of the sprite. Handle the KEYDOWN event. Store the position of the sprite (px, py). Change the position when the K_UP, K_DOWN, K_LEFT or K_RIGHT key is pressed:
run = True
px, py, speed = HW, HH, 10
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
py -= speed
elif event.key == pygame.K_DOWN:
py += speed
elif event.key == pygame.K_LEFT:
px -= speed
elif event.key == pygame.K_RIGHT:
px += speed
Minimal Sprite Sheet example:
import os
import pygame
class SpriteSheet:
def __init__(self, filename, px, py, tw, th, m, tiles, color_key = None):
self.sheet = pygame.image.load(filename)
if color_key:
self.sheet = self.sheet.convert()
self.sheet.set_colorkey(color_key)
else:
self.sheet = self.sheet.convert_alpha()
self.cells = [(px + tw * i, py, tw-m, th) for i in range(tiles)]
self.index = 0
def update(self):
self.tile_rect = self.cells[self.index % len(self.cells)]
self.index += 1
def draw(self, surface, x, y):
rect = pygame.Rect(self.tile_rect)
rect.center = (x, y)
surface.blit(self.sheet, rect, self.tile_rect)
pygame.init()
window = pygame.display.set_mode((400, 300))
clock = pygame.time.Clock()
sprite_sheet = SpriteSheet('awesomepossum sheet.bmp', 18, 580, 64, 66, 0, 6, (0, 128, 0))
run = True
while run:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
sprite_sheet.update()
window.fill(0)
sprite_sheet.draw(window, *window.get_rect().center)
pygame.display.update()
pygame.quit()
exit()
Sprite sheet form OpenGameArt.org:

Tile-based movement pygame [duplicate]

I am experimenting with a Pathfinding project and got stuck while creating a working GUI. I'm using pygame and already created a grid and a feature, which draws cubes when you press (or keep pressing) the mouse-button. However, these cubes just go wherever you click, and do not snap to the grid. I thought about using modulo somehow but I cannot seem to get it to work. Please find the code attached below. The Cube class is what I use for the squares drawn on the screen. Moreover, the drawgrid() function is how I set up my grid. I'd love some help on this, as I've been stuck on this roadblock for three days now.
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 20, 20)
def draw(self):
click = pygame.mouse.get_pressed()
if click[0]: # evaluate left button
pygame.draw.rect(screen, (255, 255, 255), self.square)
Other drawgrid() function:
def drawgrid(w, rows, surface):
sizebtwn = w // rows # Distance between Lines
x = 0
y = 0
for i in range(rows):
x = x + sizebtwn
y = y + sizebtwn
pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, w))
pygame.draw.line(surface, (255, 255, 255), (0, y), (w, y))
You have to align the position to the grids size. Use the floor division operator (//) to divide the coordinates by the size of a cell and compute the integral index in the grid:
x, y = pygame.mouse.get_pos()
ix = x // sizebtwn
iy = y // sizebtwn
Multiply the result by the size of a cell to compute the coordinate:
self.cx, self.cy = ix * sizebtwn, iy * sizebtwn
Minimal example:
import pygame
pygame.init()
screen = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
def drawgrid(w, rows, surface):
sizebtwn = w // rows
for i in range(0, w, sizebtwn):
x, y = i, i
pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, w))
pygame.draw.line(surface, (255, 255, 255), (0, y), (w, y))
class Cube:
def update(self, sizebtwn):
x, y = pygame.mouse.get_pos()
ix = x // sizebtwn
iy = y // sizebtwn
self.cx, self.cy = ix * sizebtwn, iy * sizebtwn
self.square = pygame.Rect(self.cx, self.cy, sizebtwn, sizebtwn)
def draw(self, surface):
click = pygame.mouse.get_pressed()
if click[0]:
pygame.draw.rect(surface, (255, 255, 255), self.square)
cube = Cube()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
cube.update(screen.get_width() // 10)
screen.fill(0)
drawgrid(screen.get_width(), 10, screen)
cube.draw(screen)
pygame.display.flip()

Categories