python pygame mask collision [closed]

2024/10/6 4:09:22

when I try to use this piece of code, it only works for 1 object, not for every. I'm trying to modify code from this video (https://www.youtube.com/watch?v=Idu8XfwKUao). Is there is a simpler way to get a result ?.If there is,please let me know

#part of code that doesn't matter
randomx = [100,400,300]
randomy = [100,0,300]
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_color = green_blob
obstacle = []
obstacle_mask = []
oy = []
ox = []
for i in range(4):obstacle.append(pygame.image.load("obstacle-400x399.png").convert_alpha())obstacle_mask.append(pygame.mask.from_surface(obstacle[i]))ox.append(random.choice(randomx))oy.append(random.choice(randomy))
# main loop
while True:events()for i in range(4):mx, my = pygame.mouse.get_pos()offset = ((mx - int(ox[i]))), ((my - int(oy[i])))result = obstacle_mask[i].overlap(blob_mask, offset)if result:blob_color = orange_blobelse:blob_color = green_blobscreen.blit(obstacle[i], (ox[i], oy[i]))screen.blit(blob_color, (mx, my))pygame.display.update()CLOCK.tick(FPS)screen.fill(BLACK)
Answer

Your application works fine. The issue is, that result of each evaluation of overlap cause a change of blob_color. So the last obstacle in the loop "wins". If the last obstacle overlaps the blob, then the color is orange_blob, else it is green_blob.
Set the green color before the loop. If any obstacle overlaps the blob, then change it to orange. The blob has to be drawn once after the loop. e.g.:

while True:events()blob_color = green_blobfor i in range(4):mx, my = pygame.mouse.get_pos()offset = (int(mx - ox[i]), int(my - oy[i]))if obstacle_mask[i].overlap(blob_mask, offset):blob_color = orange_blobscreen.blit(obstacle[i], (ox[i], oy[i]))screen.blit(blob_color, (mx, my))


To find obstacle at random positions, which are not overlapping, you have to evaluate if a new obstacle hits any of the former obstacles.
Create a random position:

x, y = random.randint(100, 400), random.randint(100, 400)

And evaluate if there is any obstacle that overlaps the new position.

isect = any(obstacle_mask[j].overlap(obstacle_mask[i], (x-ox[j], y-oy[j])) for j in range(i))

If that is the case, then repeat the process. Create a new random position and test for overlapping:

obstacle = []
obstacle_mask = []
oy = []
ox = []
for i in range(4):obstacle.append(pygame.image.load("obstacle-400x399.png").convert_alpha())obstacle_mask.append(pygame.mask.from_surface(obstacle[i]))isect = Truewhile isect:x, y = random.randint(100, 400), random.randint(100, 400)isect = any(obstacle_mask[j].overlap(obstacle_mask[i], (x-ox[j], y-oy[j])) for j in range(i))ox.append(x)oy.append(y)
https://en.xdnf.cn/q/118991.html

Related Q&A

How to find max average of values by converting list of tuples to dictionary?

I want to take average for all players with same name. I wrote following code. its showing index error whats the issue?Input: l = [(Kohli, 73), (Ashwin, 33), (Kohli, 7), (Pujara, 122),(Ashwin, 90)]Out…

Cannot pass returned values from function to another function in python

My goal is to have a small program which checks if a customer is approved for a bank loan. It requires the customer to earn > 30k per year and to have atleast 2 years of experience on his/her curren…

What is the difference here that prevents this from working?

Im reading a list of customer names and using each to find an element. Before reading the list, I make can confirm this works when I hard-code the name,datarow = driver.find_element_by_xpath("//sp…

How can I extract numbers based on context of the sentence in python?

I tried using regular expressions but it doesnt do it with any context Examples:: "250 kg Oranges for Sale" "I want to sell 100kg of Onions at 100 per kg"

Chart with secondary y-axis and x-axis as dates

Im trying to create a chart in openpyxl with a secondary y-axis and an DateAxis for the x-values.For this MWE, Ive adapted the secondary axis example with the DateAxis example.from datetime import date…

Env var is defined on docker but returns None

I am working on a docker image I created using firesh/nginx-lua (The Linux distribution is Alpine): FROM firesh/nginx-luaCOPY ./nginx.conf /etc/nginx COPY ./handler.lua /etc/nginx/ COPY ./env_var_echo.…

No Browser is Open issue is coming when running the Robot framework script

I have created Test.py file which has some function in it and using those function names as Keywords in sample.robot file. Ex: - Test.pydef Launch_URL(url):driver.get(url)def article(publication): do…

How to run python file in Tkinter

Im a beginner in Python, hence the question. i would like to run a python file (smileA.py) in Tkinter. How would i start? I do not wish for it to run when clicking a button, but the file to run automa…

Discord.py Cogs “Command [ ] is not found”

I am recoding my discord.py bot using cogs as it wasnt very nice before. I tried this code: import discord import os import keep_alive from discord.ext import commands from discord.ext.commands import …

Binomial distribution CDF using scipy.stats.binom.cdf [duplicate]

This question already has answers here:Alternatives for returning multiple values from a Python function [closed](14 answers)Closed 2 years ago.I wrote below code to use binomial distribution CDF (by u…