地图移动时为什么人物会卡进地图,地图不移动人物移动就不会卡进地图(请求讲解)
import pygame
import sys
import find
pygame.init()
file = find.File()
data = file.read()
photo = find.data
level = file.read('code/data/level.dat')
class Game:
def __init__(self, x, y):
self.screen = pygame.display.set_mode((x, y))
pygame.display.set_caption('Pixel Level Game')
self.font = pygame.font.Font(pygame.font.match_font('comicsansms'), 36)
self.hero = pygame.image.load('code/images/hero.png')
self.hero_x = 0
self.hero_y = 550
self.mask_map = []
self.mask_map_pos = []
self.venom_mask = []
self.venom_mask_pos = []
self.level = level
self.speed = 0
self.map_x = 0
self.map_move = False
self.down = 0
self.up = False
self.over = False
def map_draw(self):
self.mask_map = []
self.venom_mask = []
self.mask_map_pos = []
self.venom_mask_pos = []
for p in photo:
for pos in data[self.level][p]:
image = pygame.image.load('code/images/Tiles/' + p)
self.screen.blit(image, (pos[0] - self.map_x, pos[1]))
if 'brick' in p or 'ConveyorBelt' in p or 'IronBlock' in p or 'IronPlate' in p or 'pipe' in p:
self.mask_map.append(pygame.mask.from_surface(image))
self.mask_map_pos.append((pos[0] - self.map_x, pos[1]))
elif 'venom' in p:
self.venom_mask.append(pygame.mask.from_surface(image))
self.venom_mask_pos.append((pos[0] - self.map_x, pos[1]))
def level_draw(self):
w, h = self.font.size('Level: ' + str(self.level + 1))
self.screen.blit(self.font.render('Level: ' + str(self.level + 1), True, (240, 190, 0)), (405 - w / 2, 0))
def over_draw(self):
w, h = self.font.size('Game Over!')
self.screen.blit(self.font.render('Game Over!', True, 'red'), (405 - w / 2, 315 - h / 2))
def Vic(self):
global level
win = True
for p in photo:
for pos in data[self.level][p]:
if pos[0] > self.hero_x:
win = False
if win:
level += 1
if len(data) < self.level:
file.into(self.level, 'code/data/level.dat')
sys.exit()
self.__init__(810, 630)
def draw(self):
self.screen.fill((80, 190, 220))
self.map_draw()
self.screen.blit(self.hero, (self.hero_x, self.hero_y))
if self.over:
self.over_draw()
self.level_draw()
def keys(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
file.into(self.level, 'code/data/level.dat')
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and not self.over:
self.speed = -3
elif event.key == pygame.K_RIGHT and not self.over:
self.speed = 3
elif event.key == pygame.K_UP and not self.up and not self.over:
self.down = -3
self.up = True
elif event.key == pygame.K_SPACE and self.over:
self.__init__(810, 630)
elif event.type == pygame.KEYUP:
self.speed = 0
number = 0
if not self.map_move:
self.hero_x += self.speed
for m in self.mask_map:
if m.overlap(pygame.mask.from_surface(self.hero), (self.hero_x - self.mask_map_pos[number][0], self.hero_y - self.mask_map_pos[number][1])):
self.hero_x -= self.speed
number += 1
if self.hero_x >= 398:
self.map_move = True
elif self.map_move:
number = 0
self.map_x += self.speed
for m in self.mask_map:
if m.overlap(pygame.mask.from_surface(self.hero), (self.hero_x - self.mask_map_pos[number][0], self.hero_y - self.mask_map_pos[number][1])):
self.map_x -= (self.speed * 1.1)
self.speed = 0
number += 1
self.hero_y += self.down
number = 0
for m in self.mask_map:
if m.overlap(pygame.mask.from_surface(self.hero), (self.hero_x - self.mask_map_pos[number][0], self.hero_y - self.mask_map_pos[number][1])):
self.hero_y -= self.down
if self.up and self.down > 0:
self.up = False
number += 1
number = 0
for v in self.venom_mask:
if v.overlap(pygame.mask.from_surface(self.hero), (self.hero_x - self.venom_mask_pos[number][0], self.hero_y - self.venom_mask_pos[number][1])):
self.over = True
number += 1
self.down += 0.1
@staticmethod
def update():
Clock = pygame.time.Clock()
Clock.tick(60)
pygame.display.update()
game = Game(810, 630)
def main():
while True:
game.draw()
game.keys()
game.update()
if __name__ == '__main__':
main()