Pygame - pygame无法进行乒乓球碰撞游戏

时间:2016-08-20 08:11:40

标签: python pygame

#!/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)

以下是我的代码的pygame窗口图片:(http://i.stack.imgur.com/SFyBJ.jpg

每次我跑步时,我都没有时间控制球拍,然后显示比赛已结束。我一直在寻找,但无法找到原因。看起来黑球的移动速度如此之快,以至于游戏在一秒钟左右就结束了。但我已经将速度设定在一个合理的范围内,我很困惑。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我可以重建你的游戏,并修复问题。

在此尝试反向条件

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

它工作正常:我可以用蝙蝠击球,让它反弹,......

我不知道为什么我花了这么长时间才弄明白:如果球从底部掉到屏幕上,你就会输掉比赛。为此,顶部y必须大于窗口底部(计算机屏幕坐标从左上角开始为0,0)