2014-07-01 90 views
-1

嘿,你們正在開發一個像pygame遊戲一樣的彈跳球。 ..我已經下載了它的作用就像一個球,bg.jpg圖像0​​這是background..I試圖移動它沒有工作。我的代碼是用pygame移動球

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) 

當我運行這段代碼的ball.But圖像沒有加載pygame窗口..我已經保存bounce.py在文件夾以及我已經下載的兩個圖像..

我想移動球並在pygame上顯示球圖像和背景圖像窗口..希望你們能幫助我...提前預感

+0

我測試了代碼,pygame窗口正在加載。我認爲你面臨的縮進問題 –

+0

@AshokaLella,但它沒有顯示任何錯誤.. – user3783784

+0

我得到了'sound.Play()'的錯誤,因爲我沒有'yeah.mp3'。所以我刪除了這些行。除了我有一個球和背景,球似乎正在向右移動 –

回答

0

固定indenta在代碼中的問題:

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) 
+0

我可能知道爲什麼聲音沒有工作?? ...我已經添加line.2聲音.play()在surfaceObject.blit(球,(imgx,imgy))。但它沒有工作..你有任何想法如何aceheive這? – user3783784

+0

@ user3783784,增加了聲音 –

+0

..當添加行時,顯示沒有錯誤..但是sou nd不能被聽到。 – user3783784