当检测到碰撞时,如何阻止播放器图标移动

2024-04-25 18:02:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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)

Tags: imageselfeventifpngdefdisplayblock

热门问题