我最近得到了pygame,并且遇到了blitting某个矩阵的问题。 该计划本身是一个(未完成)的长期战斗。当我运行程序并按下FIGHT(进入FIGHT模式)时,我应该将attackBarRect与attackLineRect一起移到左侧,但它不会显示。但是,一段时间后退出FIGHT模式,因此矩形在那里,但没有出现。我研究了blitting rect的问题,但只找到了带有错误信息的主题(我没有任何问题),以及backround对项目进行了blitted(为此测试不起作用)的事实。请帮我。 完整代码:
import sys, pygame, time, os
pygame.init()
size = width, height = 600,448
menu1=[0,1,0,0]
enemyHealth=3000
attackNumber=0
attackPowerSelf=0
attackPower=1
attackLineX=546
attackLineY=246
def pingdef():
pass
def menuSelect():
was=1
rerun=False
pressed = pygame.key.get_pressed()
x=menu1.index(1)
if pressed[pygame.K_RIGHT]!=0:
time.sleep(0.2)
if x==3:
menu1[x]=0
menu1[0]=1
else:
menu1[x]=0
menu1[x+1]=1
elif pressed[pygame.K_LEFT]!=0:
time.sleep(0.2)
if x==0:
menu1[x]=0
menu1[3]=1
else:
menu1[x]=0
menu1[x-1]=1
elif pressed[pygame.K_z]!=0:
return x
else:
rerun=True
x=menu1.index(1)
if x==0:
screen.blit(backround, backroundRect)
if x!=was:
pingdef()
screen.blit(fight,fightRect)
was=x
if x==1:
screen.blit(backround, backroundRect)
if x!=was:
pingdef()
screen.blit(act,actRect)
was=x
if x==2:
screen.blit(backround, backroundRect)
if x!=was:
pingdef()
screen.blit(item,itemRect)
was=x
if x==3:
screen.blit(backround, backroundRect)
if x!=was:
pingdef()
screen.blit(mercy,mercyRect)
was=x
pygame.event.pump()
pygame.display.flip()
if rerun==True:
return None
screen = pygame.display.set_mode(size)
backround = pygame.image.load("images\\backround.png")
backroundRect = backround.get_rect()
fight=pygame.image.load("images\\fightSelected.png")
fightRect = fight.get_rect()
act=pygame.image.load("images\\act.png")
actRect = act.get_rect()
item=pygame.image.load("images\\item.png")
itemRect = item.get_rect()
mercy=pygame.image.load("images\\mercy.png")
mercyRect = mercy.get_rect()
attackBar=pygame.image.load("images\\attackBar.png")
attackBarRect = attackBar.get_rect()
attackLine=pygame.image.load("images\\attackLine.png")
attackLineRect = attackLine.get_rect()
pygame.mixer.music.load("sounds\\Megalovenia.ogg")
pygame.mixer.music.play(loops=-1, start=0.0)
screen.blit(fight,fightRect)
screen.blit(backround, backroundRect)
pygame.display.flip()
fightRect=fightRect.move(42,403)
actRect=actRect.move(179,403)
itemRect=itemRect.move(324,403)
mercyRect=mercyRect.move(463,403)
attackBarRect=attackBarRect.move(47,246)
attackLineRect=attackLineRect.move(attackLineX,attackLineY)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
screen.blit(backround, backroundRect)
while True:
selected=menuSelect()
if selected!=None:
break
time.sleep(0.2)
if selected==0:
screen.blit(backround, backroundRect)
screen.blit(attackBar,attackBarRect)
time.sleep(0.2)
while True:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_RETURN]==1 or attackLineRect.left<47:
attackLineRect=attackLineRect.move(546,246)
break
if attackLineRect.right<304:
attackPowerSelf+=attackPower
else:
attackPowerSelf-=attackPower
attackLineX-=1
attackLineRect=attackLineRect.move(attackLineX,attackLineY)
screen.blit(attackBar,attackBarRect)
screen.blit(attackLine,attackLineRect)
pygame.event.pump()
pygame.display.flip()
if selected==1:
print("ACT")
if selected==2:
print("ITEM")
if selected==3:
print("MERCY")
pygame.display.flip()
wait=input("")
不工作:
if selected==0:
screen.blit(backround, backroundRect)
screen.blit(attackBar,attackBarRect)
time.sleep(0.2)
while True:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_RETURN]==1 or attackLineRect.left<47:
attackLineRect=attackLineRect.move(546,246)
break
if attackLineRect.right<304:
attackPowerSelf+=attackPower
else:
attackPowerSelf-=attackPower
attackLineX-=1
attackLineRect=attackLineRect.move(attackLineX,attackLineY)
screen.blit(attackBar,attackBarRect)
screen.blit(attackLine,attackLineRect)
pygame.event.pump()
pygame.display.flip()
答案 0 :(得分:0)
您需要大步向下移动attackLineRect
向右和向右移动。
attackLineX = 546
attackLineY = 246
# This moves the rect to the right by 546 pixels and down by 246 px.
attackLineRect = attackLineRect.move(attackLineX, attackLineY)
尝试打印attackLineRect
,您会发现它很快离开了屏幕。
将其更改为,例如move(-1, 0)
,将其慢慢向左移动。
attackLineRect = attackLineRect.move(-1, 0)
此外,您的计划结构奇怪,我建议您在Code Review或Reddit发布该计划,以获取一些提示。但代码应该正常工作,因此首先解决问题并将完整的代码与图像一起发布。
您应该已经使用的一件事是pygame.time.Clock
来限制帧速率,例如:
clock = pygame.time.Clock()
while True:
# In each while loop call `clock.tick(30)` to limit the frame rate to 30 fps.
clock.tick(30)