1

I'm newb on pygame and I need help manipulating blitted images on my surface.

First, I have 2 functions that generate a 2d list with values from 1 to 4 :

def realNumber(proba):
randomNumber = random.random()

x1, x2, x3 = proba[0], proba[1], proba[2]

if randomNumber < x1:
    return 4
if x1 < randomNumber and randomNumber < x2:
    return 3
if x2 < randomNumber and randomNumber < x3:
    return 2
else:
    return 1

def gameBoard(n, proba):
matrix = [[realNumber(proba) for x in range(n)] for y in range(n)] 
return matrix

I then use that list and display it with pygame :

import sys, pygame, bases, possible, merge
pygame.init()

# Init vars
mapSize = 5
xPos = 288
yPos = 265
proba=(0.05,0.30,0.6)
size = width, height = 1088, 607
gameBoard = gameBoard(mapSize, proba)
screen = pygame.display.set_mode(size)
surface = pygame.image.load("images/surface-5x5.png")
numOne = pygame.image.load("images/1.png")
numTwo = pygame.image.load("images/2.png")
numThree = pygame.image.load("images/3.png")
numFour = pygame.image.load("images/4.png")
cells = {
    1: numOne,
    2: numTwo,
    3: numThree,
    4: numFour
}
cellSize = 32

I then created a function to draw that gameBoard grid :

def drawGrid():
for col in range(mapSize):
    for row in range(mapSize):
        surface.blit(cells[gameBoard[col][row]], (yPos + row*cellSize, xPos + col*cellSize, cellSize, cellSize))

Finally I load the result in my main loop :

while 1:
for event in pygame.event.get():
    if event.type == pygame.QUIT: sys.exit()
    elif event.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()

screen.blit(surface, (0, 0))
pygame.display.update()

drawGrid()

Result look like this :

enter image description here

Now what I want to achieve is to be able to When I mouse click on one of these cells, apply an image selection effect (shadow, color, ... anything)

Problem is I don't know How to get / point to the displayed cells and How to apply a cool effect on it when mouse clicking.

If someone could give some help here, I would be a lot grateful.

Thanks anyway!

NoX
  • 575
  • 5
  • 16
  • to add effect you can (1) first create in "Image Editor" images already with effect and then you can replace clicked image. or (2) create transparent images only with effect so you could blit one image on another. – furas Jan 16 '17 at 21:57
  • `pygame.MOUSEBUTTONDOWN` gives you mouse position as `event.pos` so you don't need `pygame.mouse.get_pos()` – furas Jan 16 '17 at 21:57
  • You doesn't use `pygame.Rect()` to keep objects size and position so you can't use `rect.collidepoint(mouse_position)` but you can recalculate mouse position `(x,y)` to `(column, row)` to find clicked object. – furas Jan 16 '17 at 22:00
  • The idea is sexy, Any code example to achieve this ? – NoX Jan 16 '17 at 22:02
  • which idea - images with efects, or `event.pos` or `rect.collidepoint(mouse_position)` or `recalculation (x,y) to (column, row)` ? – furas Jan 16 '17 at 22:06
  • recalculation. Others ideas is taken also too :) – NoX Jan 16 '17 at 22:19
  • 1
    you calculate image position with `yPos + row*cellSize, xPos + col*cellSize` so you can use it to calculate `row` , `col` - something like `col = round((mouse_x-xPos)/cellSize)`, `row = round((mouse_y-yPos)/cellSize)` – furas Jan 16 '17 at 22:26
  • you could create images with effects in some Painter and then keep as `cells = { 11: numOne_with_shadow, 21: numOne_with_color, ...}` and then you have to add `10` to selected number (ie. 1) to get image with shadow (11) or add `20` to get image with color. – furas Jan 16 '17 at 22:35
  • it might be not good concrete help, but I'd start looking into numpy arrays first. Then I'd try to implement rendering and store the cell values in these arrays. Might be a big time investment, but finally all the things you describe here would be way easier. So you will be able to use a lot of built-in sugar of numpy and end up with elegant code. Just my opinion – Mikhail V Jan 17 '17 at 01:43
  • Thanks for answer. It's for a school test, the only lib I can use is pygame. – NoX Jan 17 '17 at 13:52

0 Answers0