简体   繁体   English

如果我将整个代码作为 function 放在 pygame 中,为什么某些功能不起作用?

[英]why doesnt some features not work if i put my whole code as a function in pygame?

So, i have bean developing this simple game where there is this slingshot and the ball a ball image is hidden right beneath it.所以,我一直在开发这个简单的游戏,其中有一个弹弓,球的图像就隐藏在它的正下方。 and then i have a bird.然后我有一只鸟。 My games goal is to shot the bird down.我的游戏目标是将鸟击落。 everything worked the bird was being shot down my ball was firing.一切正常,鸟被击落,我的球在开火。 I was in my final stages of this game when i put it all into a function so that after the bird is shot down i can call it all over again and repeat it.当我把它全部放入 function 时,我正处于这场比赛的最后阶段,这样在鸟被击落后我可以再次调用它并重复它。 so when i put my code in the function and tested it out, the sprites where drawn properly but when i clicked the space bar to fire my ball it did not work.因此,当我将我的代码放入 function 并对其进行测试时,精灵绘制正确但当我单击空格键发射我的球时它不起作用。 I am using pygame for this project.我在这个项目中使用 pygame。 you can look at my code below:您可以在下面查看我的代码:

Thanks in advance提前致谢

import random
onlystarting = True
def main():
    global onlystarting
    onlystarting = False

    pygame.init()
    screen = pygame.display.set_mode((800, 600))


    postImg = pygame.image.load('post.png')
    postX = 0
    postY = 150

    lineImg = pygame.image.load('line.png')
    lineImg = pygame.transform.scale(lineImg, (610, lineImg.get_height()))

    slingshotImg = pygame.image.load('slingshot.png')
    slingshotX = 340
    slingshotY = 500
    slingshotX_change =  0


    ballImg = pygame.image.load('ball.png')
    ballX = slingshotX
    ballY = 533
    ballYchange = 2
    ballImg = pygame.transform.scale(ballImg, (20, 20))
    ballState = "ready"

    birdImg = pygame.image.load('bird.png')
    birdX = random.randint(300, 600)
    birdY = 240
    birdState = "alive"

    def post(x, y) :
        screen.blit(postImg, (x, y))

    def line(x, y) :
        screen.blit(lineImg, (x, y))

    def slingshot(x, y) :
        screen.blit(slingshotImg, (x, y))

    def fire_ball(x,y):
        global ballState
        ballState = "fire"
        screen.blit(ballImg, (x + 16, y))
    def bird(x,y):
        screen.blit(birdImg, (x, y))

    running = True
    #Game loop
    while running:
        screen.fill((225, 225, 225))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    slingshotX_change = -0.5

                if event.key == pygame.K_RIGHT:
                    slingshotX_change = 0.5
                
                if event.key == pygame.K_SPACE:
                    if ballState == "ready":
                        ballX = slingshotX
                        fire_ball(ballX, ballY)
            
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                    slingshotX_change = 0

            slingshotX += slingshotX_change

        slingshotX += slingshotX_change


        if ballState == "fire":
            fire_ball(ballX, ballY)
            ballY -= ballYchange

        if slingshotX <= 100:
            slingshotX = 100.1

        if slingshotX >= 640:
            slingshotX = 639.9



        distanceX = ballX - birdX
        distanceY = ballY - birdY
        
        if distanceX > -32:
            if distanceX < 32 :
                if distanceY > -32:
                    if distanceY < 32:
                        birdX = 1000
                        birdY = 1000
                        main()


        post(postX, postY)
        post(650, 150)
        line(95, 270)
        line(95, 370)
        slingshot(slingshotX, slingshotY)
        bird(birdX, birdY)
        
        pygame.display.update()
if onlystarting:
    main()```

You have functions in the function using global variables ballState , screen , postImg , lineImg , slingshotImg .您在 function 中有函数使用全局变量ballStatescreenpostImglineImgslingshotImg ballImg and birdImg . ballImgbirdImg However, these variables are never defined in global scope. These variables are local variables in the main function.然而,这些变量从未在全局scope中定义。这些变量在main function中是局部变量。

Either use the global statement to make this variables global:使用global语句使这个变量成为全局变量:

def main():
    global ballState, screen, onlystarting, postImg, lineImg, slingshotImg, ballImg, birdImg
    # [...]

Or pass the variables to the functions:或者将变量传递给函数:

def draw(screen, surf, x, y) :
    screen.blit(postImg, (x, y))

def fire(screen, surf, x, y):
    global ballState
    ballState = "fire"
    screen.blit(surf, (x + 16, y))
def main():
    global onlystarting, ballState
    # [...]

    for event in pygame.event.get():
            # [...]
        
            if event.type == pygame.KEYDOWN:
                # [...]
           
                if event.key == pygame.K_SPACE:
                    if ballState == "ready":
                        ballX = slingshotX
                        fire(screen, ballImg, ballX, ballY)
            
        # [...]   

        if ballState == "fire":
            fire(screen, ballImg, ballX, ballY)
            ballY -= ballYchange

        # [...]
        
        draw(screen, postImg, postX, postY)
        draw(screen, postImg, 650, 150)
        draw(screen, lineImg, 95, 270)
        draw(screen, lineImg, 95, 370)
        draw(screen, slingshotImg, slingshotX, slingshotY)
        draw(screen, birdImg, birdX, birdY)
        
        pygame.display.update()

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

相关问题 为什么我的代码不起作用? 我需要制作一个密码生成器, - why my code doesnt work? i need to make a password generator, 我有在 pygame 中写入屏幕的功能,但它不起作用 - I have function for writing to the screen in pygame but it doesnt work 为什么我的笛卡尔积函数不起作用? - Why doesnt my cartesian product function work? 当我在 python 中放入一个函数时,我的代码不起作用 - My code doesn’t work when I put in in a function in python python pygame animation 可以在没有 class 的情况下工作,但是一旦我将它放入 class 中,它就不想工作了? - python pygame animation works without class but as soon as i put it into a class it doesnt want to work? 我的Pygame messagetoscreen功能不显示文本 - My Pygame messagetoscreen function doesnt show text 为什么 colorkey 或 setalpha 不能在 pygame 中处理我的代码? - Why won't colorkey or setalpha work on my code in pygame? 为什么我的 Pygame 精灵跳转代码不起作用? - Why won't my Pygame sprite jump code work? PyGame,我希望能够延迟一些代码而不延迟整个程序 - PyGame, I want to be able to delay some code without delaying the whole program 为什么我的 python Armstrong 号码代码不起作用 - Why my python Armstrong number code doesnt work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM