微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用pygame碰撞后无法移动我的精灵

如何解决使用pygame碰撞后无法移动我的精灵

因此,我是使用python pygame制作游戏的初学者,我正在尝试编写一个检测碰撞的函数。这是我目前拥有的功能

def collision_detection(rect,h2w1_1,h2w1_2,h2w1_3,h2w1_4,h2w2_1,h1w2_1,h1w1_1,h1w1_2,h1w1_3,h1w1_4):
    rect_list = [h2w1_1,h1w1_4]
    for rectangle in rect_list:
        if rect.rect.colliderect(rectangle.rect):
            return True
    return False

这是我game_functions.py的完整代码

import pygame,sys,random

def collision_detection(rect,h1w1_4]
    for rectangle in rect_list:
        if rect.rect.colliderect(rectangle.rect):
            return True
    return False



def rect_movement(settings,event,rect,name,h1w1_4):
    if settings.selected_block == str(name):
        if event.key == pygame.K_RIGHT and not collision_detection(rect,h1w1_4):
            if rect.img_x < 300:
                rect.img_x += 75
        elif event.key == pygame.K_LEFT and not collision_detection(rect,h1w1_4):
            if rect.img_x > 0:
                rect.img_x -= 75
                if collision_detection(rect,h1w1_4):
                    rect.img_x += 75
        elif event.key == pygame.K_UP and not collision_detection(rect,h1w1_4):
            if rect.img_y > 0:
                rect.img_y -= 75
                if collision_detection(rect,h1w1_4):
                    rect.img_y += 75
        elif event.key == pygame.K_DOWN and not collision_detection(rect,h1w1_4):
            if rect.img_y < 200:
                rect.img_y += 75
                if collision_detection(rect,h1w1_4):
                    rect.img_y -= 75


def square_movement(settings,h1w1_4):
            if rect.img_x < 100:
                rect.img_x += 75
                if collision_detection(rect,h1w1_4):
                    rect.img_x -= 75
        elif event.key == pygame.K_LEFT and not collision_detection(rect,h1w1_4):
            if rect.img_x > 20:
                rect.img_x -= 75
                if collision_detection(rect,h1w1_4):
            if rect.img_y > 50:
                rect.img_y -= 75
                if collision_detection(rect,h1w1_4):
            if rect.img_y < 300:
                rect.img_y += 75
                if collision_detection(rect,h1w1_4):
                    rect.img_y -= 75
            else:
                print('You win!')


def sml_square_movement(settings,h1w1_4):
    if settings.selected_block == str(name):
        if event.key == pygame.K_RIGHT:
            if rect.img_x < 200:
                rect.img_x += 75
                if collision_detection(rect,h1w1_4):
                    rect.img_x -= 75

        elif event.key == pygame.K_LEFT:
            if rect.img_x > 20:
                rect.img_x -= 75
                if collision_detection(rect,h1w1_4):
                    rect.img_x += 75

        elif event.key == pygame.K_UP:
            if rect.img_y > 50:
                rect.img_y -= 75
                if collision_detection(rect,h1w1_4):
                    rect.img_y += 75

        elif event.key == pygame.K_DOWN:
            if rect.img_y < 300:
                rect.img_y += 75
                if collision_detection(rect,h1w1_4):
                    rect.img_y -= 75


def rect_h1_movement(settings,h1w1_4):
            if rect.img_x < 150:
                rect.img_x += 75
        elif event.key == pygame.K_LEFT and not collision_detection(rect,h1w1_4):
            if rect.img_x > 0:
                rect.img_x -= 75
        elif event.key == pygame.K_UP and not collision_detection(rect,h1w1_4):
            if rect.img_y > 0:
                rect.img_y -= 75
        elif event.key == pygame.K_DOWN and not collision_detection(rect,h1w1_4):
            if rect.img_y < 300:
                rect.img_y += 75


def check_events(settings,h1w1_4):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.MOUSEBUTTONUP:
            settings.selected_block = random.choice(['h1w1_1','h1w1_2','h1w1_3','h1w1_4','h1w2_1','h2w1_1','h2w1_2','h2w1_3','h2w1_4','h2w2_1'])

        elif event.type == pygame.KEYDOWN:
            # h2w1_1 movement
            rect_movement(settings,h1w1_4)

            # h2w1_2 movement
            rect_movement(settings,h1w1_4)

            # h2w1_3 movement
            rect_movement(settings,h1w1_4)

            # h2w2 movement
            square_movement(settings,'h2w2_1',h1w1_4)

            # h1w2 movement
            rect_h1_movement(settings,h1w1_4)

            # h1w1_1 movement
            sml_square_movement(settings,'h1w1_1',h1w1_4)

            # h1w1_2 movement
            sml_square_movement(settings,h1w1_4)

            # h1w1_3 movement
            sml_square_movement(settings,h1w1_4)

            # h1w1_4 movement
            sml_square_movement(settings,h1w1_4,h1w1_4)


def update_screen():
    pygame.display.flip()

我的主程序:

import pygame

from py_files.settings import Settings

# Shape imports
from py_files.h2w1_1 import rect2hw1_1
from py_files.h2w1_2 import rect2hw1_2
from py_files.h2w1_3 import rect2hw1_3
from py_files.h2w1_4 import rect2hw1_4
from py_files.h2w2_1 import rect2hw2_1
from py_files.h1w2_1 import rect1hw2_1
from py_files.h1w1_1 import rect1hw1_1
from py_files.h1w1_2 import rect1hw1_2
from py_files.h1w1_3 import rect1hw1_3
from py_files.h1w1_4 import rect1hw1_4

import py_files.game_functions as gf


def run_game():
    pygame.init()

    # Start classes
    settings = Settings()

    display = pygame.display.set_mode((settings.screen_width,settings.screen_height))
    h2w1_1 = rect2hw1_1(display)
    h2w1_2 = rect2hw1_2(display)
    h2w1_3 = rect2hw1_3(display)
    h2w1_4 = rect2hw1_4(display)
    h2w2_1 = rect2hw2_1(display)
    h1w2_1 = rect1hw2_1(display)
    h1w1_1 = rect1hw1_1(display)
    h1w1_2 = rect1hw1_2(display)
    h1w1_3 = rect1hw1_3(display)
    h1w1_4 = rect1hw1_4(display)

    while True:
        # Blit images
        h2w1_1.blitme()
        h2w1_2.blitme()
        h2w1_3.blitme()
        h2w1_4.blitme()
        h2w2_1.blitme()
        h1w2_1.blitme()
        h1w1_1.blitme()
        h1w1_2.blitme()
        h1w1_3.blitme()
        h1w1_4.blitme()

        # Updating
        gf.check_events(settings,h1w1_4)
        gf.update_screen()
        display.fill(settings.bg_color)


run_game()

对于所有矩形,我都有10个单独的程序。这是其中一个的样子(对于其他9个,我只是更改了图像和self.x,self.y变量):

h1w1.py:

import pygame

class rect1hw1_1():
    def __init__(self,screen):
        self.screen = screen
        self.img_x = 75
        self.img_y = 225

    def blitme(self):
        img = pygame.image.load('py_files/images/h1w1.png')
        self.screen.blit(img,(self.img_x,self.img_y))
        self.rect = img.get_rect()

我还有一个图像显示游戏的认状态:

The Game Image

这应该做的是检查选定的子画面(“矩形”变量)是否与其他任何可变矩形(h2w1_1,h2w1_2,h2w1_3等)相撞。但是现在有了这段代码,我的精灵都无法移动。这是为什么?我该如何解决

提前谢谢!

解决方法

使用pygame.Rect.collidelist测试矩形是否与矩形列表之一冲突:

rect_list = [h2w1_1,h2w1_2,h2w1_3,h2w1_4,h2w2_1,h1w2_1,h1w1_1,h1w1_2,h1w1_3,h1w1_4]
if rect.rect.colliderect(tile_rects) >= 0:
    # [...]

pygame.Surface.get_rect.get_rect()返回一个具有 Surface 对象大小的矩形,该矩形始终以(0,0)开始,因为 Surface 对象没有位置。矩形的位置可以通过关键字参数指定。例如,可以使用关键字参数center指定矩形的中心。这些关键字参数在返回之前将应用于pygame.Rect的属性(有关关键字参数的完整列表,请参见pygame.Rect)。

使用新位置创建一个矩形并进行碰撞测试。仅在未检测到碰撞的情况下更改位置:

def rect_movement(settings,event,rect,name,h2w1_1,h1w1_4):
    if settings.selected_block == str(name):
        
        testrect = rect.get_rect(topleft = (rect.img_x,rect.img_y))
        if event.key == pygame.K_LEF and testrect.x > 0:
            testrect.x -= 75
        elif event.key == pygame.K_RIGH and testrect.x < 300:
            testrect.x += 75
        elif event.key == pygame.K_UP and testrect.y > 0:
            testrect.y -= 75
        elif event.key == pygame.K_DOWN  and testrect.y < 200:
            testrect.y += 75

        rect_list = [h2w1_1,h1w1_4]
        if not testrect.collidelist(rect_list):
            rect.img_x,rect.img_y = testrect.topleft

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。