如何在检测到碰撞时停止播放器图标移动

时间:2018-01-12 13:54:24

标签: python class pygame python-imaging-library collision

我正在尝试使用pygame制作游戏并遇到两个问题。我必须以某种方式从块中制作一个迷宫,并且当它与它交互时让玩家图标被该块阻止。我知道如何处理块以及如何在它们交互时进行检测,而不知道如何阻止播放器图标在其中移动。此外,我已经设法将播放器图标blit到屏幕上,并且屏幕上只有一个,但我无法弄清楚如何使用箭头键移动图标。我不是要求任何人重写我的代码,而是要指出我正确的方向。

import pygame, sys
from pygame.locals import *
from PIL import Image



pygame.init()
FPS = 60
fps_clock = pygame.time.Clock()
surface = pygame.display.set_mode((640, 480))
mouse = pygame.mouse.get_pos()
playericon = pygame.image.load("Player_Icon.png")
white = 255, 255, 255
cover = pygame.image.load("Player_Icon_cover.png")
s = 0
DISPLAY_SURF = pygame.display.set_mode((640, 720))
DISPLAY_SURF.fill(white)
coord=[]

class Blocks:
    def __init__(self, x, y):
        self.x = x
        self.y = y


    def set_position():
        x = self.x
        y = self.y

    def get_position():
        return set_position

    class Visible_Blocks:

        def image():
            block = pygame.image.load("Plain block.png")
            DISPLAY_SURF.blit(block, (self.x, self.y))

        def detect_collision():
            block_image = Image.open("Plain block.png")
            width, height = block_image.size
            if self.x >= coord[1][0] > self.x + width:
                if self.y >= coord[1][1] < self.y + height:

while True:
    pygame.display.update()
    fps_clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN:
            x, y = event.pos
            coord.append(event.pos)
            if s == 0:
                DISPLAY_SURF.blit(playericon, (x, y))
            else:
                DISPLAY_SURF.blit(cover, coord[-2])
                DISPLAY_SURF.blit(playericon, (x, y))
                pygame.display.flip()
            s += 1
            if s >= 2:
                coord_length = len(coord)
                for i in range(coord_length - 2):
                    del coord[i]
                    print(coord)

0 个答案:

没有答案