如何让我的乒乓球拍在比赛中期成长?

时间:2021-06-24 16:59:14

标签: python pygame pong

我正在制作一个乒乓球游戏项目,我正在尝试制作可选择的道具。我想要它,当我按下“r”时,左拨片的尺寸会增加。我不知道如何做到这一点,请帮忙。我将桨定义为一个类,并为它们设置了尺寸。我希望它们在按下“r”键时立即将矩形 (paddleA) 的高度从 200 更改为 400。我希望能够保持我为游戏编写的动作。

#Liam Real
#Final Project
#Pong with Power ups

import pygame
from random import randint

pygame.init()

clrBlack = pygame.Color(0, 0, 0)
clrWhite = pygame.Color(255,255,255)
clrRed = pygame.Color(255, 0, 0)
Minsize1 = (200)
Minsize2 = (200)
Maxsize1 = (400)

class Player(pygame.sprite.Sprite):

    
    def __init__(self, color, width, height):

        super().__init__()
        


        self.image = pygame.Surface([width, height])
        self.image.fill(clrBlack)
        self.image.set_colorkey(clrBlack)
 

        pygame.draw.rect(self.image, color, [0, 0, width, height])
        

        self.rect = self.image.get_rect()
    def moveUp(self, pixels):
        self.rect.y -= pixels

        if self.rect.y < 0:
          self.rect.y = 0
          
    def moveDown(self, pixels):
        self.rect.y += pixels

        if self.rect.y > 564:
          self.rect.y = 564
          
class Ball(pygame.sprite.Sprite):

    
    def __init__(self, color, width, height):
        super().__init__()
        

        self.image = pygame.Surface([width, height])
        self.image.fill(clrBlack)
        self.image.set_colorkey(clrBlack)
 

        pygame.draw.rect(self.image, color, [0, 0, width, height])
        
        self.velocity = [randint(4,8),randint(-8,8)]
        

        self.rect = self.image.get_rect()
        
    def update(self):
        self.rect.x += self.velocity[0]
        self.rect.y += self.velocity[1]

    def bounce(self):
        self.velocity[0] = -self.velocity[0]
        self.velocity[1] = randint(-8,8)
        

size = (1024, 768)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong and Power ups")

paddleA = Player(clrWhite, 10,Minsize1)
paddleA.rect.x = 20
paddleA.rect.y = 200

paddleB = Player(clrWhite, 10, Minsize2)
paddleB.rect.x = 994
paddleB.rect.y = 200



ball = Ball(clrWhite,10,10)
ball.rect.x = 512
ball.rect.y = 195

all_sprites_list = pygame.sprite.Group()

all_sprites_list.add(paddleA)
all_sprites_list.add(paddleB)
all_sprites_list.add(ball)

RunGame = True

clock = pygame.time.Clock()

scoreA = 0
scoreB = 0


while RunGame:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            RunGame = False
        elif event.type==pygame.KEYDOWN:
                if event.key==pygame.K_x: 
                     RunGame=False 


    keys = pygame.key.get_pressed()

    if keys[pygame.K_r]:
        paddleA = Player(clrWhite, 10,400)



    if keys[pygame.K_w]:
        paddleA.moveUp(5)
    if keys[pygame.K_s]:
        paddleA.moveDown(5)
    if keys[pygame.K_UP]:
        paddleB.moveUp(5)
    if keys[pygame.K_DOWN]:
        paddleB.moveDown(5)
        



    all_sprites_list.update()
    
    if ball.rect.x>=1014:
        scoreA+=1
        ball.velocity[0] = -ball.velocity[0]
    if ball.rect.x<=0:
        scoreB+=1
        ball.velocity[0] = -ball.velocity[0]
    if ball.rect.y>758:
        ball.velocity[1] = -ball.velocity[1]
    if ball.rect.y<0:
        ball.velocity[1] = -ball.velocity[1]  
        
    if pygame.sprite.collide_mask(ball, paddleA) or pygame.sprite.collide_mask(ball, paddleB):
      ball.bounce()

    screen.fill(clrBlack)

    pygame.draw.line(screen, clrWhite, [512, 0], [512, 800 ], 5)

    all_sprites_list.draw(screen)
    
    
    font = pygame.font.Font(None, 74)
    text = font.render(str(scoreA), 1, clrWhite)
    screen.blit(text, (250,10))
    text = font.render(str(scoreB), 1, clrWhite)
    screen.blit(text, (760,10))

    pygame.display.flip()

    clock.tick(60)
    
pygame.quit()

1 个答案:

答案 0 :(得分:0)

grow 类添加 Player 方法。该方法创建了一个更大尺寸的新 image,但保留了桨的中心位置:

class Player(pygame.sprite.Sprite):

    def __init__(self, color, width, height):
        super().__init__()
        self.color = color
        self.width = width
        self.height = height
        self.image = pygame.Surface([self.width, self.height])
        self.image.fill(clrBlack)
        self.image.set_colorkey(clrBlack)
        self.image.fill(self.color)
        self.rect = self.image.get_rect()

    def grow(self):
        self.image = pygame.Surface([self.width, self.height*2])
        self.image.fill(self.color)
        self.rect = self.image.get_rect(center = self.rect.center)

    def moveUp(self, pixels):
        self.rect.y -= pixels
        if self.rect.top < 0:
            self.rect.top = 0
          
    def moveDown(self, pixels):
        self.rect.y += pixels
        if self.rect.bottom > 768:
          self.rect.bottom = 768

当按下 r 时,使用 KEYDOWN 事件增加桨:

while RunGame:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            RunGame = False
        elif event.type==pygame.KEYDOWN:
            if event.key==pygame.K_x: 
                RunGame=False 

            if event.key==pygame.K_r: 
                paddleA.grow()

    # [...]

相关问题