简体   繁体   English

更改pygame中的背景

[英]change background in pygame

For some reason, my background does not change and my game does not start when the space bar is pressed. 由于某些原因,按下空格键时我的背景不会改变,并且我的游戏也不会开始。 I am not receiving any errors, the program just isn't working correctly. 我没有收到任何错误,该程序无法正常运行。 Please help. 请帮忙。

def main():
    title = games.load_image("contratitle.bmp",transparent = False)
    games.screen.background = title
    cursor = Cursor()
    games.screen.add(cursor)
    xd = 0
    if games.keyboard.is_pressed(games.K_q):
        xd = 1        
        while xd == 1:
            screen = games.load_image("jungle.bmp", transparent = False)
            games.screen.background = screen
            file2 = 'contra.mp3'
            pygame.mixer.music.load(file2)
            pygame.mixer.music.play()
            rizer = Rizer()
            games.screen.add(rizer)
            runner = Runner()
            games.screen.add(runner)
            shooter = Shooter()
            games.screen.add(shooter)
            spreadpower = Spreadp()
            #games.screen.add(spreadpower)
            rizerlives = Rizerlives()
            games.screen.add(rizerlives)
            rizerlivesa = Rizerlivesa()
            games.screen.add(rizerlivesa)
            #bean = Bean()
            #games.screen.add(bean)
    games.screen.mainloop()
main()

Then, I tried to use pygame.display.update(), but that didn't work either. 然后,我尝试使用pygame.display.update(),但这也不起作用。 Am I putting it in the wrong place? 我把它放错了地方吗?

def main():
    screen = games.load_image("contratitle.bmp",transparent = False)
    games.screen.background = screen
    cursor = Cursor()
    games.screen.add(cursor)
    xd = 0
    if games.keyboard.is_pressed(games.K_SPACE):
        xd = 1
        while xd == 1:
            screen = games.load_image("jungle.bmp", transparent = False)
            games.screen.background = screen
            file2 = 'contra.mp3'
            pygame.mixer.music.load(file2)
            pygame.mixer.music.play()
            rizer = Rizer()
            games.screen.add(rizer)
            pygame.display.update()
    games.screen.mainloop()
main()

For your 7th line of code: 对于您的第七行代码:

if games.keyboard.is_pressed(games.K_q):

It appears you need to change it to: 看来您需要将其更改为:

if games.keyboard.is_pressed(games.K_SPACE):

For more documentation on the PyGame gamekeys, see this link: 有关PyGame游戏键的更多文档,请参见以下链接:

http://www.pygame.org/docs/ref/key.html http://www.pygame.org/docs/ref/key.html

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

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