I keep getting segmentation faults or crashes when I move my sprite to the right side of the screen.
I am attempting to get my "sprite" to "switch" pixels with another image I have at the point of contact with the mask and then change back when it leaves. It's not doing that too well at the moment and the segmentation fault doesn't help either.
This project has the base start from what was shown by code.Pylet at https://www.youtube.com/watch?v=Idu8XfwKUao. Where I'm modifying it is where I am having the issues, starting around the while loop. According to my IDE it has something to do with the setting of the maskOverlap variable and, where I think the error lies more specifically is with the offset2 variable
So my questions are: Can anyone see where and why the segmentation faults are occurring and: What I could do to better the outcome of my sprites changing color and then changing back.
import math, random, sys
import cv2
import pygame
from pygame.locals import *
# exit the program
def events():
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# define display surface
W, H = 900, 600
HW, HH = W / 2, H / 2
AREA = W * H
# initialise display
pygame.init()
CLOCK = pygame.time.Clock()
DS = pygame.display.set_mode((W, H))
pygame.display.set_caption("code.Pylet - Pixel Perfect Collision")
FPS = 120
# define some colors
BLACK = (0, 0, 0, 255)
WHITE = (255, 255, 255, 255)
obstacle = pygame.image.load("obstacle-400x399.png").convert_alpha()
#obstacle = pygame.image.load("mask.jpg").convert_alpha()
obstacle_mask = pygame.mask.from_surface(obstacle)
obstacle_rect = obstacle.get_rect()
ox = HW - obstacle_rect.center[0]
oy = HH - obstacle_rect.center[1]
green_blob = pygame.image.load("greenblob-59x51.png").convert_alpha()
orange_blob = pygame.image.load("orangeblob-59x51.png").convert_alpha()
blob_mask = pygame.mask.from_surface(green_blob)
blob_rect = green_blob.get_rect()
#blob_color = green_blob
# main loop
while True:
events()
mx, my = pygame.mouse.get_pos()
offset = (int(mx - ox), int(my - oy))
offset2 = (int(ox - mx), int(oy - my))
result = obstacle_mask.overlap(blob_mask, offset)
maskOverlap = blob_mask.overlap_mask(obstacle_mask, offset2)
#blob_temp = blob_color
maskSize = maskOverlap.get_size()
a, b = maskSize
if result:
blob_color = green_blob.copy()
for x in range(maskSize[0]):
for y in range(maskSize[1]):
if maskOverlap.get_at((x,y)):
blob_color.set_at((x,y), orange_blob.get_at((x,y)))
else:
blob_color = green_blob
DS.blit(obstacle, (ox, oy))
DS.blit(blob_color, (mx, my))
pygame.display.update()
CLOCK.tick(FPS)
DS.fill(BLACK)