你加上self.playerRect.left , self.playerRect.top = self.playerX ,self.playerY再看吧。这样是不是就对应上了?
其实你没必要去专门定义X,Y。直接用rect.left rect.top代替即可。
import sys
import pygame
from pygame.locals import *
class Player():
def __init__(self):
self.MAXHP = 100 # 生命上限
self.HP = 100 # 初始化当前生命
self.MAXMP = 100 # 魔法值上限
self.MP = 100 # 初始化当前魔法值
self.EXP = 0 # 初始化经验值
self.LV = 1 # 初始化登记
self.PA = 50 # 初始化物理攻击力
self.MA = 50 # 初始化魔法攻击力
self.PD = 10 # 初始化物理防御值
self.MD = 10 # 初始化魔法防御值
self.dead = False
self.playerX = 200 # 初始X坐标
self.playerY = 200 # 初始Y坐标
self.playerwalkforward = False # 向前移动
self.playerwalkback = False # 向后移动
self.playerwalkleft = False # 向左移动
self.playerwalright = False # 向右移动
self.speed = 5 # 移动速度
self.playerStatus = pygame.image.load('code.jpg') # 加载任务图像
self.playerRect = self.playerStatus.get_rect()
self.playerRect.left , self.playerRect.top = self.playerX ,self.playerY
# self.playerRect = pygame.Rect((self.playerX,self.playerY),(50,71)) #获取任务矩形
# self.playerRect = pygame.Rect((200,200),(50,71))
def playerUpdate(self):
if self.playerwalkforward:
self.playerY -= self.speed # 向前移动
self.playerRect = self.playerRect.move([0, -self.speed])
print('Y', self.playerY)
elif self.playerwalkback:
self.playerY += self.speed # 向后移动
self.playerRect = self.playerRect.move([0, self.speed])
print('Y', self.playerY)
elif self.playerwalkleft:
self.playerX -= self.speed # 向左移动
self.playerRect = self.playerRect.move([-self.speed, 0 ])
print('X', self.playerX)
elif self.playerwalright:
self.playerX += self.speed # 向右移动
self.playerRect = self.playerRect.move([self.speed, 0])
print('X', self.playerX)
class Monster():
def __init__(self):
self.MAXHP = 100
self.HP = 100
self.MAXMP = 100
self.MP = 100
self.EXP = 0
self.LV = 1
self.PA = 50
self.MA = 50
self.PD = 10 # 初始化物理防御值
self.MD = 10 # 初始化魔法防御值
# self.GU = GU
self.dead = False
self.monsterX = 100
self.monsterY = 100
self.monsterStatus = pygame.image.load('code.jpg')
self.monsterRect = pygame.Rect((self.monsterX, self.monsterY), (50, 71))
def createMap():
screen.fill((255, 255, 255))
screen.blit(pygame.image.load('背景图.jpg'), (0, 0)) # 加载背景图片
screen.blit(Monster.monsterStatus, (Monster.monsterX, Monster.monsterY)) # 加载怪物
screen.blit(Player.playerStatus, (Player.playerX, Player.playerY)) # 加载任务
Player.playerUpdate()
pygame.display.update()
pygame.display.flip()
def impactCheck():
# Player.playerRect = pygame.Rect((Player.playerX,Player.playerY),(Player.playerStatus.get_width(),Player.playerStatus.get_height()))
# Player.playerRect = Player.playerStatus.get_rect()
# Monster.monsterRect = Monster.monsterStatus.get_rect()
# print (Player.playerRect)
# print (Monster.monsterRect)
if Monster.monsterRect.colliderect(Player.playerRect):
# fighting()
i = True
print(i)
def fighting():
while True:
if Player.dead == False and Monster.dead == False:
if Player.PD >= Monster.PA:
Player.HP = Player.HP
else:
Player.HP -= Monster.PA
if Monster.PD >= Player.PA:
Monster.HP = Monster.PD
else:
Monster.HP -= Player.PA
else:
break
if __name__ == '__main__':
pygame.init()
size = width, height = 800, 800
screen = pygame.display.set_mode(size)
pygame.display.set_caption('魔塔')
clock = pygame.time.Clock()
Player = Player()
Monster = Monster()
impactCheck()
while True:
clock.tick(60)
# impactCheck()
createMap()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 在按下键盘时,设置player的属性为True
elif event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
Player.playerwalkleft = True
elif event.key == K_d or event.key == K_RIGHT:
Player.playerwalright = True
elif event.key == K_w or event.key == K_UP:
Player.playerwalkforward = True
elif event.key == K_s or event.key == K_DOWN:
Player.playerwalkback = True
# elif event.key == K_SPACE:
# hero_temp.shoot() # 这里应该是飞机.显示子弹方法()
# 在松开键盘时,设置player的属性为False
elif event.type == KEYUP:
if event.key == K_a or event.key == K_LEFT:
Player.playerwalkleft = False
print('m', Monster.monsterRect)
print('p', Player.playerRect)
elif event.key == K_d or event.key == K_RIGHT:
Player.playerwalright = False
print('m', Monster.monsterRect)
print('p', Player.playerRect)
elif event.key == K_w or event.key == K_UP:
Player.playerwalkforward = False
print('m', Monster.monsterRect)
print('p', Player.playerRect)
elif event.key == K_s or event.key == K_DOWN:
Player.playerwalkback = False
print('m', Monster.monsterRect)
print('p', Player.playerRect)
# else:
# Player.playerwalkforward = False
# createMap()
# pygame.display.flip()
|