球在 pygame 窗口的边缘反弹并需要一段时间才能出来

时间:2021-08-02 00:22:25

标签: python pygame

球 (TennisBall) 在 pygame 窗口外弹跳 并需要一段时间才能出来

当我的球 (TennisBall) 进入我的球拍时,它会在 pygame 窗口 中弹跳,然后在它外面弹跳并需要一段时间才能出来。如果您在此处运行代码,您可以亲眼看看。 您可以将 code 复制到您的文本编辑器。代码如下:

import pygame, sys, easygui
player1 = easygui.enterbox('Player 1\'s name:', default = 'Mason')
player2 = easygui.enterbox('Player 2\'s name:', default = 'Nolan')
score_display1 = 0
score_display2 = 0
def set_score1(new_score):
    global score_display1, score_surface1
    score_display1 = new_score
    score_surface1 = score_font1.render(str(score_display1), 1, (0, 0, 0))

def set_score2(new_score):
    global score_display2, score_surface2
    score_display2 = new_score
    score_surface2 = score_font2.render(str(score_display2), 1, (0, 0, 0))

def set_rounds(new_round):
    global rounds, round_surface, round_text
    rounds = new_round
    round_text = 'round: ' + str(rounds)
    round_surface = round_font.render(str(round_text), 1, (0, 0, 0))

# Defines the ball class
class Ball(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):
        self.rect = self.rect.move(self.speed)

        # Bounces the ball
        if self.rect.top == 0 :
            self.speed[1] = -self.speed[1]

        if self.rect.bottom == 480:
            self.speed[1] = -self.speed[1]

        if self.rect.left <= 0:
            self.speed[0] = -self.speed[0]
            set_score2(score_display2 + 1)

        if self.rect.right >= 640:
            self.speed[0] = -self.speed[0]
            set_score1(score_display1 + 1)

# Defines the paddle class
class Paddle(pygame.sprite.Sprite):
    def __init__(self, location = [0,0]):
        pygame.sprite.Sprite.__init__(self)
        image_surface = pygame.surface.Surface([20, 100])
        image_surface.fill([255,25,0])
        self.image = image_surface.convert()
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
        self.location = location


pygame.init()

# Initializes everything
screen = pygame.display.set_mode([640,480])
clock = pygame.time.Clock()
TennisBall = Ball('wackyball.bmp', [5, 10], [320, 240])
ballGroup = pygame.sprite.Group(TennisBall)
paddle = Paddle([1, 150])
paddle2 = Paddle([619, 150])
round_font = pygame.font.Font(None, 50)
rounds = 0
round_text = 'round: ' + str(rounds)
round_surface = round_font.render(str(round_text), 1, (0, 0, 0))
player_rounds_1 = 0
player_rounds_2 = 0
name_text1 = player1
name_text2 = player2
name_surface1 = round_font.render(str(name_text1), 1, (0, 0, 0))
name_surface2 = round_font.render(str(name_text2), 1, (0, 0, 0))
name_position1 = [10, 10]
name_position2 = [500, 10]
score_position1 = [130, 10]
score_position2 = [610, 10]

# Creates the font object
score_font1 = pygame.font.Font(None, 50)
score_font2 = pygame.font.Font(None, 50)
score_surface1 = score_font1.render(str(score_display1), 1, (0, 0, 0))
score_surface2 = score_font2.render(str(score_display2), 1, (0, 0, 0))
round_position = [240, 10]
done = False
running = True
delay = 10
interval = 50
pygame.key.set_repeat(delay, interval)

while running:  # The start of the main program (`while` loop)
    clock.tick(30)
    screen.fill([255, 255, 255])
    key_input = pygame.key.get_pressed()  
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:
            running = False
        # Detects mouse motion to move the paddle
        elif event.type == pygame.MOUSEMOTION:
            paddle.rect.centery = event.pos[1]
        # Detects keys being pressed    

        elif key_input[pygame.K_UP]:
            if paddle2.rect.top >= 0:
                paddle2.rect.top -= 50

        elif key_input[pygame.K_DOWN]:
            if paddle2.rect.bottom <= 480:
                paddle2.rect.bottom += 50

    # rounds
    if score_display1 >= 11:
        player_rounds_1 = player_rounds_1 + 1
        score_display1 = 0
        score_display2 = 0
        set_rounds(rounds + 1)
        set_score1(0)
        set_score2(0)
        TennisBall.location = [320, 240]
        # TennisBall.speed[1] + 5
        # TennisBall.speed[0] + 5

    if score_display2 >= 11:
        player_rounds_2 = player_rounds_2 + 1
        score_display1 = 0
        score_display2 = 0
        set_rounds(rounds + 1)
        set_score1(0)
        set_score2(0)
        TennisBall.location = [320, 240]
        # TennisBall.speed[1] + 5
        # TennisBall.speed[0] + 5

    # Detects collisions between the ball and paddle
    if pygame.sprite.spritecollide(paddle, ballGroup, False):
        TennisBall.speed[0] = -TennisBall.speed[0]

    elif pygame.sprite.spritecollide(paddle2, ballGroup, False):
        TennisBall.speed[0] = -TennisBall.speed[0]

    TennisBall.move()  # Moves the ball

    # Redraws everything
    if not done:
        screen.blit(TennisBall.image, TennisBall.rect)
        screen.blit(paddle.image, paddle.rect)
        screen.blit(paddle2.image, paddle2.rect)
        screen.blit(score_surface1, score_position1)
        screen.blit(score_surface2, score_position2)
        screen.blit(name_surface1, name_position1)
        screen.blit(name_surface2, name_position2)
        screen.blit(round_surface, round_position)
        pygame.display.flip()
 

    # Creates and draws the final score text
    if rounds == 5:
        final_text_gameover = "Game Over"
        final_text_winner1 = "The winner is " + player1
        final_text_winner2 = "The winner is " + player2
        final_text_tie = 'No winner. Tie!'

        if player_rounds_2 < player_rounds_1:
            ft1_font = pygame.font.Font(None, 70)
            ft1_surface = ft1_font.render(final_text_gameover, 1, (0, 0, 0))
            ft2_font = pygame.font.Font(None, 50)
            ft2_surface = ft2_font.render(final_text_winner1, 1, (0, 0, 0))
            screen.blit(ft1_surface, [screen.get_width()//2 - \
                        ft1_surface.get_width()//2, 100])
            screen.blit(ft2_surface, [screen.get_width()//2 - \
                        ft2_surface.get_width()//2, 200])
        if player_rounds_2 > player_rounds_1:
            ft3_font = pygame.font.Font(None, 70)
            ft3_surface = ft3_font.render(final_text_gameover, 1, (0, 0, 0))
            ft4_font = pygame.font.Font(None, 50)
            ft4_surface = ft4_font.render(final_text_winner2, 1, (0, 0, 0))
            screen.blit(ft3_surface, [screen.get_width()//2 - \
                        ft3_surface.get_width()//2, 100])
            screen.blit(ft4_surface, [screen.get_width()//2 - \
                        ft4_surface.get_width()//2, 200])

        if player_rounds_2 == player_rounds_1:
            ft5_font = pygame.font.Font(None, 70)
            ft5_surface = ft5_font.render(final_text_gameover, 1, (0, 0, 0))
            ft6_font = pygame.font.Font(None, 50)
            ft6_surface = ft6_font.render(final_text_tie, 1, (0, 0, 0))
            screen.blit(ft5_surface, [screen.get_width()//2 - \
                        ft5_surface.get_width()//2, 100])
            screen.blit(ft6_surface, [screen.get_width()//2 - \
                        ft6_surface.get_width()//2, 200])

        done = True
        pygame.display.flip()
        
pygame.quit()

0 个答案:

没有答案