#python #python-3.x #pygame
#python #python-3.x #pygame
Вопрос:
Я хотел бы отобразить изображение открытой двери в координатах x и y закрытой двери (двери), но я не знаю, как работает функция отображения в этих случаях. Извините, что код немного запутанный, он представляет собой смесь нескольких разных стилей кода.Часть моего кода взята из: https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125
код:
from pygame.locals import *
import pygame
import sys
import glob
map2 = """wwwwwwd wwwww
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
w w
wwwwwwpwwwwww"""
pygame.display.set_icon(pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Icon.png"))
pygame.display.set_caption("Knock Knight")
screen = pygame.display.set_mode((226, 318))
moving_right = False
moving_left = False
moving_up = False
moving_down = False
player_location = [0,0]#remember its a fucking list
door_list = []
door_location = [100,-20]
#-----------------------------
open_door = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Open door.png").convert()
floor_tile = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/floor.png").convert()
door = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Door.png").convert()#if you dont convert it colorkey wont work
door_rect = pygame.Rect(door_location[0], door_location[1], door.get_width(), door.get_height())
door.set_colorkey((255, 255, 255))
wall = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Wall.png").convert()
wall_rect = wall.get_rect(center=(100, 256))
player = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Player.png").convert()
player_rect = pygame.Rect(player_location[0], player_location[1], player.get_width(), player.get_height())
player.set_colorkey((255, 255, 255))
enemy = pygame.image.load("C:/Users/cuerv/Downloads/My code/Knock Knight/Sprites/Enemy.png").convert()
enemy_rect = enemy.get_rect(center=(100, 250))
enemy.set_colorkey((255, 255, 255))
def check_collision(door, player):
for player in door:
#for pipr in pipes = checks forall the rects inside pipe list
if player_rect.colliderect(door_rect):
#colliderect = checks for collision
pygame.display(open_door)
def init_display():
global screen, wall, door, player, enemy, floor_tile, player_rect
def tiles(map2):
global wall, door, player, enemy, floor_tile, player_rect
door_list.clear()
for y, line in enumerate(map2):
#counts lines
for x, c in enumerate(line):
#counts caracters
if c == "p":
player_rect = screen.blit(player, player_location)
if c == "w":
#caracter is w
screen.blit(wall, (x * 16.18, y * 15))
if c == "d":
rect = screen.blit(door, (x * 16.2, y * 15))
door_list.append(rect)
if c == "e":
screen.blit(enemy, (x * 16, y * 15))
if c == "f":
screen.blit(floor_tile, (x * 16, y * 15))
map2 = map2.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
screen.fill((0,0,0))
tiles(map2)
if moving_right == True:
player_location[0] = 4
if moving_left == True:
player_location[0] -= 4
if moving_up == True:
player_location[1] -=4
if moving_down == True:
player_location[1] =4
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
moving_right = True
if event.key == K_LEFT:
moving_left = True
if event.key == K_UP:
moving_up = True
if event.key == K_DOWN:
moving_down = True
if event.type == KEYUP:
if event.key == K_RIGHT:
moving_right = False
if event.key == K_LEFT:
moving_left = False
if event.key == K_UP:
moving_up = False
if event.key == K_DOWN:
moving_down = False
check_collision(door_rect, player_rect)
pygame.display.update()
clock.tick(60)
Ответ №1:
Добавьте переменную player_at_door
и инициализируйте ее с False
помощью . Когда игрок находится у двери, установите переменную True
:
player_at_door = False
def check_collision(door, player):
global player_at_door
player_at_door = False
for door_rect in door:
if player_rect.colliderect(door_rect):
player_at_door = True
Нарисуйте либо door
или open_door
, в зависимости от состояния player_at_door
:
def tiles(map1):
global tile, door, player, enemy, player_rect
door_list.clear()
for y, line in enumerate(map1):
#counts lines
for x, c in enumerate(line):
#counts caracters
if c == "w":
#caracter is w
screen.blit(tile, (x * 16.18, y * 15))
if c == "d":
door_image = open_door if player_at_door else door # <---
rect = screen.blit(door_image, (x * 16.2, y * 15)) # <---
door_list.append(rect)
if c == "p":
player_rect = screen.blit(player, player_location)
if c == "e":
screen.blit(enemy, (x * 16, y * 15))