简体   繁体   English

pygame中的Rect似乎在那里,但没有blitting

[英]Rect in pygame appears to be there but not blitting

I got pygame recently and am having problems with blitting a certain rect. 我最近得到了pygame,并且遇到了一些问题。 The program itself is an (not finished) undertale battle. 该计划本身是一个(未完成)的长期战斗。 When I run the program and press FIGHT (entering FIGHT mode), I should be getting the attackBarRect with the attackLineRect on top of it moving to the left, but it does not show up. 当我运行程序并按下FIGHT(进入FIGHT模式)时,我应该将attackBarRect与attackLineRect一起移到左侧,但它不会显示。 However, the FIGHT mode is exited after some time, so the rect is there, but does not show up. 但是,一段时间后退出FIGHT模式,因此矩形在那里,但没有出现。 I researched on problems blitting a rect but only found topics with error messages (I don't have any) and the fact that the backround is blitted over the item (Tested for that did not work). 我研究了blitting rect的问题,但只找到了带有错误信息的主题(我没有任何问题),以及backround对项目进行了blitted(为此测试不起作用)的事实。 Please help me. 请帮我。 Full Code: 完整代码:

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("") 

Not working part: 不工作的部分:

    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()

You're moving the attackLineRect downwards and to the right with big steps. 你正在通过大步骤向下和向右移动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)

Try to print the attackLineRect and you'll see that it leaves the screen very quickly. 尝试打印attackLineRect ,你会发现它很快离开了屏幕。

Change it to, for example, move(-1, 0) to move it slowly to the left. 将其更改为,例如, move(-1, 0)以将其缓慢向左移动。

attackLineRect = attackLineRect.move(-1, 0)

Also, your program is structured in a strange way and I'd recommend to post it on Code Review or Reddit to get some tips. 此外,您的程序是以一种奇怪的方式构建的,我建议将其发布在Code ReviewReddit上以获取一些提示。 But the code should work correctly, so fix the issues first and post the complete code with the images. 但代码应该正常工作,因此首先解决问题并将完整的代码与图像一起发布。

One thing you should already use is a pygame.time.Clock to limit the frame rate, eg: 您应该已经使用的一件事是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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM