2016-08-20 6 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.jpg)파이 게임 - 파이 게임은 탁구 충돌 게임 여기

나는 그것을 실행할 때마다의 파이 게임 창 사진은 실행할 수 없습니다, 내가 제어 할 수있는 시간이 없어 노를 젓으면 게임이 끝났음을 알 수 있습니다. 나는 오랫동안 수색을 해왔지만 그 이유를 알 수 없다. 블랙 볼이 너무 빨리 움직여 약 1 초가 지나면 게임이 끝난 것 같습니다. 그러나 나는 이미 속도를 합리적인 범위로 설정했다. 나는 혼란 스럽다. 누구든지 도와 줄 수 있습니까?

+0

인생을 처음 잃어버린 경우'done'을 설정하고 false로 설정하지 마십시오. 상태 머신을 약간 개선해야합니다. while 루프에서'myball.rect.top'와'screen.get_rect(). bottom'의 값을 출력하면 놀랄 수 있습니다. –

+0

내'if lives == 0' 문에서 done을 True로 설정했습니다. 그리고 my while 루프에'myball.rect.top'와'screen.get_rect(). bottom'의 값을 출력하는 코드는 어디에 추가해야합니까? –

+0

어디서나 인쇄하십시오. 잠시 후 OK입니다. –

답변

1

게임을 다시 빌드하고 문제를 해결할 수 있습니다.

여기

시도 역 조건

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

하고 잘 작동 : 너무 날 데려 갔어 왜, 그것은 반송하기 위해선, 방망이로 공을 칠 수 있습니다 ...

나도 몰라 오랫동안 알아낼 수 있습니다 : 공이 화면 아래로 떨어지면 게임을 잃게됩니다. 그 때문에 상단 y는 창 하단보다 커야합니다 (컴퓨터 화면 좌표는 왼쪽 상단에서 0,0입니다)