如何测试区域是否重叠(Python) [英] How to test if areas overlap (Python)

查看:871
本文介绍了如何测试区域是否重叠(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手程序员,正在使用Pygame库编写Python程序来模拟自然选择.

I'm a novice coder writing a Python program to simulate natural selection using the Pygame library.

我要完成的一件事情是使重叠的椭圆形(或者,如果太复杂的话,矩形)对象重叠而产生继承其特征的子对象.

One of the things I'm trying to accomplish is to make moving elliptical (or if this is too complex, rectangular) objects that overlap produce a child object that inherits their traits.

我的问题是我无法创建有效的代码来识别两个对象区域何时重叠.我需要代码来识别两个对象何时穿越路径(并暂时重叠),以便游戏知道生成其他形状.

My problem is that I'm unable to create a working code to identify when any two object areas are overlapped. I need the code to identify when two objects are crossing paths (and momentarily overlapped) so that the game knows to spawn another shape.

有一次,我尝试了一个复杂的嵌套for循环,该循环我认为可以,但是却导致模拟崩溃.

At one point I tried a complex nested for-loop that I thought would work, but that caused the simulation to crash.

这是我的代码,

import pygame, random

# Define some colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

pygame.init()

# Set the width and height of the screen
map_width = 800
map_height = 800
size = [map_width, map_height]
screen = pygame.display.set_mode(size)

# Display name
pygame.display.set_caption("Natural Selection Game")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# -------- Organism -----------
class Organism:

    def __init__(self):
        # Speed and direction
        self.change_x = random.randrange(0,6)
        self.change_y = random.randrange(0,6)

        # Dimensions
        self.width = random.randrange(5,50)
        self.height = random.randrange(5,50)

        # Diet (WIP)
        self.color = random.choice([red, green, blue])

        # Starting position
        self.x = random.randrange(0 + self.width, 800 - self.width)
        self.y = random.randrange(0 + self.height, 800 - self.height)

    # Spawn
    def spawn(self, screen):
        pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height])

    # Initiate movement
    def move(self):
        self.x += self.change_x
        self.y += self.change_y

    # Bounce 
    def bounce(self, map_width, map_height):
        if self.x < 0 or self.x > map_width - self.width:
            self.change_x = self.change_x * -1
        if self.y < 0 or self.y > map_height - self.height:
            self.change_y = self.change_y * -1

# Initial spawning conditions
org_list = []
org_number = 15

for i in range(0,org_number):
    org = Organism()
    org_list.append(org)

# -------- Main Program Loop -----------
while not done:
    # ---- Event Processing ----
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # ---- Logic ----
    # Movement
    for i in org_list:
        i.move()
        i.bounce(map_width, map_height)

    # Reproduction

    # ---- Drawing ----
    # Set the screen background
    screen.fill(white)

    # Spawn organisms
    for i in org_list:
        i.spawn(screen)

    # --- Wrap-up
    # Limit to 60 frames per second
    clock.tick(60)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

# Close everything down
pygame.quit()

任何见识将不胜感激.

推荐答案

您应使用 pygame. 保持位置和大小,然后可以使用Pygame函数检查碰撞.

You should use pygame.Rect to keep position and size and then you can use Pygame functions to check collisions.

one_rect.colliderect(second_rect)

one_rect.coolidepoint(mouse_position)

有关 pygame.Rect

Pygame还具有 pygame.sprite.Group 来保留一组精灵并进行检查所有精灵的碰撞.

Pygame has also pygame.sprite.Group to keep group of sprites and check collisions for all sprites.

这篇关于如何测试区域是否重叠(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆