2016-08-20 62 views
3
#!/usr/bin/python 
# -*- coding: utf-8 -*- 
import pygame 
import sys 


class MyBallClass(pygame.sprite.Sprite): 
    def __init__(self, image_file, speed, location): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.image.load(image_file) 
     self.rect = self.image.get_rect() 
     self.rect.left, self.rect.top = location 
     self.speed = speed 

    def move(self): 
     global points, score_text 
     self.rect = self.rect.move(self.speed) 
     if self.rect.left < 0 or self.rect.right > screen.get_width(): 
      self.speed[0] = -self.speed[0] 

     if self.rect.top <= 0: 
      self.speed[1] = -self.speed[1] 
      points += 1 
      score_text = font.render(str(points), 1, (0, 0, 0)) 


class MyPaddleClass(pygame.sprite.Sprite): 
    def __init__(self, location=[0, 0]): 
     pygame.sprite.Sprite.__init__(self) 
     image_surface = pygame.surface.Surface([100, 20]) 
     image_surface.fill([0, 0, 0]) 
     self.image = image_surface.convert() 
     self.rect = self.image.get_rect() 
     self.rect.left, self.rect.top = location 


pygame.init() 
screen = pygame.display.set_mode([640, 480]) 
clock = pygame.time.Clock() 
ball_speed = [3, 4] 
myball = MyBallClass("E:\\python file\\blackball.jpg", ball_speed, [50, 50]) 
ballgroup = pygame.sprite.Group(myball) 
paddle = MyPaddleClass([270, 400]) 
lives = 3 
points = 0 

font = pygame.font.Font(None, 50) 
score_text = font.render(str(points), 1, (0, 0, 0)) 
textpos = [10, 10] 
done = False 

while 1: 
    clock.tick(30) 
    screen.fill([255, 255, 255]) 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 
     elif event.type == pygame.MOUSEMOTION: 
      paddle.rect.centerx = event.pos[0] 

    if pygame.sprite.spritecollide(paddle, ballgroup, False): 
     myball.speed[1] = -myball.speed[1] 
    myball.move() 
    if not done: 
     screen.blit(myball.image, myball.rect) 
     screen.blit(paddle.image, paddle.rect) 
     screen.blit(score_text, textpos) 
     for i in range(lives): 
      width = screen.get_width() 
      screen.blit(myball.image, [width - 40 * i, 20]) 
     pygame.display.flip() 
    if myball.rect.top <= screen.get_rect().bottom: 
     # In get_rect(), you cannot leave out brackets 
     lives -= 1 
     if lives == 0: 
      final_text1 = "Game over!" 
      final_text2 = 'your final score is' + str(points) 
      ft1_font = pygame.font.Font(None, 70) 
      ft2_font = pygame.font.Font(None, 50) 
      ft1_surface = font.render(final_text1, 1, (0, 0, 0)) 
      ft2_surface = font.render(final_text2, 1, (0, 0, 0)) 
      screen.blit(ft1_surface, [screen.get_width()/2, 100]) 
      screen.blit(ft2_surface, [screen.get_width()/2, 200]) 
      pygame.display.flip() 
      done = True 
     else: 
      pygame.time.delay(1000) 
      myball.rect.topleft = [50, 50] 
     frame_rate = clock.get_fps() 
     print(frame_rate) 

這裏是我的代碼:(http://i.stack.imgur.com/SFyBJ.jpgpygame的 - pygame中不能運行的乒乓遊戲碰撞

我每次運行它Pygame的窗口畫面,我沒有控制時間槳,然後它顯示遊戲結束。我一直在尋找很長時間,但找不到原因。看起來黑球運動速度如此之快,以至於比賽結束了大約一秒鐘。但我已經把速度設定在一個合理的範圍內,我很困惑。誰能幫忙?

+0

第一次你失去了生命,你設置了'done'並且從不將它設置爲false。你必須稍微改進你的狀態機。同時在while循環中打印'myball.rect.top'和'screen.get_rect()。bottom'的值,您可能會感到驚訝。 –

+0

在我的'if lives == 0'聲明中,我已將done設置爲True。在我的while循環中,我應該在哪裏添加用於打印'myball.rect.top'和'screen.get_rect()。bottom'的值的代碼? –

+0

將它們打印到任何地方。只是在確定好之後 –

回答

1

我可以重建你的遊戲並解決問題。

嘗試逆這裏條件

if myball.rect.top >= screen.get_rect().bottom: 

工作得很好:我能擊中球與蝙蝠,使反彈,...

我不知道爲什麼我花了這麼很長時間沒有想到:如果球從底部離開屏幕,你就會失去比賽。爲此,頂部y必須大於窗口底部(計算機屏幕座標是從左上角開始的0,0,0)