首页 > 解决方案 > The pygame code and the error is mentioned below:

问题描述

Here is a source code of my space-arcade pygame. It's a quite simple one and doesn't shoots bullets. Only that we can move the player with the obstacles being moved as well. However, the limit for the spaceship (player) to move in the screen has been set from x = 0 to x = 765 and from y = 200 to y = 480. But the spaceship somehow cross the limit for y-axis i.e it can move beyond y = 0 and y = 600, but only at some specific x-axis like when x is 10 and 590 something. How can i fix this gap in the following python code:

import random

pygame.init()

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

pygame.display.set_caption("Space Invader")    
icon = pygame.image.load("space-invaders.png")  
pygame.display.set_icon(icon)  

background = pygame.image.load("background.png") 

player_image = pygame.image.load("space-invaders.png")    
playerX = 370  
playerY = 480  
playerX_change = 0
playerY_change = 0

def player(x, y):
    screen.blit(player_image, (x, y))  

obs_image = pygame.image.load("cave-painting (1).png")    
obsX = random.randint(0, 800)       
obsY = random.randint(0, 100)        
obsX_change = 2      
obsY_change = 0.3      

def obstacle(x, y):
    screen.blit(obs_image, (x, y))  

running = True
while running:
    screen.fill( (0, 0,0) )  
    screen.blit(background, (0, 0))  

    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            running = False  
        
        if event.type == pygame.KEYDOWN: 
            if event.key == pygame.K_LEFT:  
                playerX_change = -4  
            if event.key == pygame.K_RIGHT:  
                playerX_change = 4 
            if event.key == pygame.K_UP:  
                playerY_change = -4
            if event.key == pygame.K_DOWN:  
                playerY_change = 4

        if event.type == pygame.KEYUP:  
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0  
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                playerY_change = 0

    playerX += playerX_change  
    playerY += playerY_change 

    # Stopping our player beyond screen:
    if playerX <= 0:
        playerX = 0
    elif playerX >= 765:  
        playerX = 765
    elif playerY <= 200:
        playerY = 200
    elif playerY >= 480:
        playerY = 480

    obsX += obsX_change
    obsY += obsY_change

    # Movement mechanics for obstacles:
    if obsX <= 0:
        obsX_change = 3
        obsY += obsY_change
    elif obsX >= 736:
        obsX_change = -3
        obsY += obsY_change
  
    player(playerX, playerY)  
    obstacle(obsX, obsY)  

    pygame.display.update()```

Here are the source pictures used in the aforementioned code:

[background][1]
[cave-painting (1)][2]
[space-invaders][3]

  [1]: https://i.stack.imgur.com/EZFLs.png

  [2]: https://i.stack.imgur.com/CxNs4.png

  [3]: https://i.stack.imgur.com/Z97n7.png  

标签: pythonpygame

解决方案


不要使用elif. 你的代码说(伪代码):

If x is beyond or equal to 0 then go back to zero
If x is not beyond or equal to 0 but x is beyond or equal to 765 then go back to 765
If none of the above are true but y is beyond or equal to 200 then go back to 200

你看到问题了吗?如果 x 等于 0,则永远不会发生 y 检查。将第三个更改elifif解决此问题。这是固定代码:

if playerX <= 0:
    playerX = 0

elif playerX >= 765:  
    playerX = 765

if playerY <= 200:
    playerY = 200

elif playerY >= 480:
    playerY = 480

推荐阅读