I am making a pong game project, and I'm trying to make selectable powerups. I want it when I press "r" the left paddle will increase in size. I cannot figure out how to make this happen pls help. I have the paddles defined as a class, with a set size for them both. I want them to instantly change from 200 to 400 for the height of the rectangle (paddleA
), when pressing the key "r". I want to be able to maintain the movement I have coded in for my game.
#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()
Add a grow
method to Player
class. The method creates a new image
with a larger size but keeps the center position of the paddle:
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
Use the KEYDOWN
event to increase the paddle, when r is pressed:
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()
# [...]