用pygame移动球

时间:2014-07-01 05:00:54

标签: python pygame

嘿伙计们正在开发一款像pygame一样的弹跳球。 ..我已经下载了一个图像ball.jpg,它像一个球,而bg.jpg就是背景......我试图移动球。但它没有用。我的代码是

import pygame, sys
from pygame.locals import *
pygame.init()
clocks = pygame.time.Clock()
surfaceObject = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Bounce')

mousey,mousex = 0,0
imgx = 10
imgy = 10
pixmove = 5
movement = 'down'


background = pygame.image.load('bg.jpg').convert()
ball = pygame.image.load('ball.jpg').convert_alpha()

sound = pygame.mixer.Sound('yeah.mp3')


while True:

if movement == 'down':
    imgx += pixmove
if imgy < 200:
    movement = 'right'

elif movement  == 'right':
    imgx += pixmove
if imgx < 200:
    movement = 'up'


elif movement == 'up':
    imgy -= pixmove
if imgy < 30:
    movement = 'left'


elif movement == 'left':
    imgx -= pixmove
if imgx < 30:
    movement = 'down'




for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()


        surfaceObject.blit(background,(mousex,mousey))
        surfaceObject.blit(ball,(imgx,imgy))
        sound.Play()


    pygame.display.update()
    clocks.tick(50)

当我运行此代码时,图像不会加载到pygame窗口..我已将bounce.py保存在文件夹中,同时保存了我已下载的两张图像..

我想移动球并在pygame窗口上显示球图像和背景图像。希望你们能帮助我...提前谢谢

1 个答案:

答案 0 :(得分:0)

修复了代码中的缩进问题:

import time                                              
import pygame, sys                                      
from pygame.locals import *                              
pygame.init()                                            
clocks = pygame.time.Clock()                            
surfaceObject = pygame.display.set_mode((640, 480))      
pygame.display.set_caption('Bounce')                    

mousey,mousex = 0,0                                      
imgx = 10                                                
imgy = 10                                                
pixmove = 5                                              
movement = 'down'                                        

background = pygame.image.load('bg.jpg').convert()      
ball = pygame.image.load('ball.jpg').convert_alpha()    
pygame.mixer.music.load('yeah.mp3')

while True:                                              
    time.sleep(1)                                        
    if movement == 'down':                              
        imgx += pixmove                                  
    if imgy < 200:                                      
        movement = 'right'                              

    elif movement  == 'right':                          
        imgx += pixmove                                  
    if imgx < 200:                                      
        movement = 'up'                                  


    elif movement == 'up':                              
        imgy -= pixmove                                  
    if imgy < 30:                                        
        movement = 'left'                                


    elif movement == 'left':                            
        imgx -= pixmove                                  
    if imgx < 30:                                        
        movement = 'down'                                
    print movement                                      
    print imgx                                          
    print imgy                                          

    for event in pygame.event.get():                    
        if event.type == QUIT:                          
            pygame.quit()                                
            sys.exit()                                  

    surfaceObject.blit(background,(mousex,mousey))      
    surfaceObject.blit(ball,(imgx,imgy))                
    pygame.mixer.music.play()
    pygame.display.update()                              
    clocks.tick(50)