Reverse Pong Game Snippet

Tags: , ,

In reverse pong, you score points by allowing the ‘ball’ to pass into your own goal, rather than defending your goal as in traditional pong. The score value of the ball is doubled each time it is reflected by a player, and the scores of both players diminish by a percentage each time at each reflection.

Rather than focusing on traditional game skills such as reflex and dexterity, Reverse Pong is instead a bluffing game of mental misdirection and brinksmanship which attempts to pit the personalities, rather than the skills, of the two players into conflict.

import random
import pygame

#revpong
#pong clone
#players score points by allowing the pong ball into their own goal
#every paddle deflection doubles the current ball value

pygame.init()
pygame.display.set_caption("Revpong")
screen = pygame.display.set_mode((800, 480), pygame.FULLSCREEN)
pygame.mouse.set_visible(False)
#screen = pygame.display.set_mode((800, 480))
windowsize = screen.get_size()

gameContinue = True

game_colour = (255, 255, 255)
clock = pygame.time.Clock()

font = pygame.font.SysFont(pygame.font.get_default_font(), 36)

paddle_dimensions = (24, 96)
paddle = pygame.surface.Surface(paddle_dimensions)
paddle.fill(game_colour)

ball = pygame.surface.Surface((24, 24))
ball.fill(game_colour)

ball_position = (windowsize[0]/2, windowsize[1]/2)
ball_velocity = (-4, 4)

#x positions of paddles
paddle_x_1 = paddle_dimensions[0] * 3
paddle_x_2 = windowsize[0] - paddle_dimensions[0] * 4

#y positions of paddles
paddle_y_1 = windowsize[1] / 2 - paddle_dimensions[1] / 2
paddle_y_2 = windowsize[1] / 2 - paddle_dimensions[1] / 2

#deltas of paddles
paddle_d_1 = 0
paddle_d_2 = 0

#scores
score_ball = 1
score_1 = 0
score_2 = 0

def reset_ball(ball_position, ball_velocity):
	random_y = int(random.random() * 5) - int(random.random() * 5)
	random_x = 2 + int(random.random() * 2)
	ball_position = windowsize[0]/2, windowsize[1]/2
	if score_1 > score_2:
		ball_velocity = random_x, random_y
	elif score_2 > score_1:
		ball_velocity = -random_x, random_y
	else:
		if random.random() > 0.5:
			ball_velocity = random_x, random_y
		else:
			ball_velocity = -random_x, random_y
	return ball_position, ball_velocity

ball_position, ball_velocity = reset_ball(ball_position, ball_velocity)

while gameContinue:
	clock.tick(50)
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			gameContinue = False

	#input
	keys = pygame.key.get_pressed()

	paddle_d_1 = 0
	paddle_d_2 = 0

	if keys[pygame.K_ESCAPE]:
		gameContinue = False

	if keys[pygame.K_w]:
		paddle_y_1 -= 4
		paddle_d_1 -= 1
	if keys[pygame.K_s]:
		paddle_y_1 += 4
		paddle_d_1 += 1
	if keys[pygame.K_o]:
		paddle_y_2 -= 4
		paddle_d_2 -= 1
	if keys[pygame.K_l]:
		paddle_y_2 += 4
		paddle_d_1 += 1

	ball_old_position = ball_position
	ball_position = ball_position[0] + ball_velocity[0], ball_position[1] + ball_velocity[1]

	#check new ball_position for collision with top and bottom edges
	if ball_position[1] < 12 or ball_position[1] > windowsize[1] - 12:
		#bounce off top or bottom edge
		ball_position = ball_old_position
		ball_velocity = (ball_velocity[0], -ball_velocity[1])

	#check new ball_position for collision with score zones
	if ball_position[0] < paddle_dimensions[0]:
		#player 1 scores
		ball_position, ball_velocity = reset_ball(ball_position, ball_velocity)
		score_1 += score_ball
		score_ball = 1
	if ball_position[0] > windowsize[0] - paddle_dimensions[0]:
		#player 2 scores
		score_2 += score_ball
		score_ball = 1
		ball_position, ball_velocity = reset_ball(ball_position, ball_velocity)

	ball_rect = pygame.Rect((ball_position[0] - 12, ball_position[1] - 12), (24, 24))
	#check new ball_position for collision with paddle_1
	paddle_rect = pygame.Rect((paddle_x_1, paddle_y_1), paddle_dimensions)
	if ball_rect.colliderect(paddle_rect):
		#if the ball has passed the paddle x middle
		if ball_position[0] < paddle_x_1 + paddle_dimensions[0]/2:
			#test if ball is up or down
			if ball_position[1] < paddle_y_1 + paddle_dimensions[1]:
				#ball is up
				ball_velocity = ball_velocity[0], ball_velocity[1] - 4
			else:
				#ball is down
				ball_velocity = ball_velocity[0], ball_velocity[1] + 4
		else:
			ball_velocity = ball_velocity[0] * -1 + 0.1, ball_velocity[1] + paddle_d_1
			ball_position = paddle_x_1 + paddle_dimensions[0] + 12 + ball_velocity[0], ball_position[1] + ball_velocity[1]
			score_ball = score_ball * 2
			score_1 = int(score_1 * 0.90)
			score_2 = int(score_2 * 0.90)

	paddle_rect = pygame.Rect((paddle_x_2, paddle_y_2), paddle_dimensions)
	if ball_rect.colliderect(paddle_rect):
		#if the ball has passed the paddle x middle
		if ball_position[0] > paddle_x_2 + paddle_dimensions[0]:
			#test if ball is up or down
			if ball_position[1] < paddle_y_2 + paddle_dimensions[1] / 2:
				#ball is up
				ball_velocity = ball_velocity[0], ball_velocity[1] - 4
			else:
				#ball is down
				ball_velocity = ball_velocity[0], ball_velocity[1] + 4
		else:
			ball_velocity = ball_velocity[0] * -1 - 0.1, ball_velocity[1] + paddle_d_2
			ball_position = paddle_x_2 - 12 + ball_velocity[0], ball_position[1] + ball_velocity[1]

			score_ball = score_ball * 2
			score_1 = int(score_1 * 0.90)
			score_2 = int(score_2 * 0.90)

	#draw
	screen.fill((0,0,0))

	screen.blit(paddle, (paddle_x_1, paddle_y_1))
	screen.blit(paddle, (paddle_x_2, paddle_y_2))
	screen.blit(ball, (ball_position[0] - 12, ball_position[1] - 12))

	screen.blit(font.render(str(score_1), False, game_colour), (0,0))
	offset = font.size(str(score_2))
	screen.blit(font.render(str(score_2), False, game_colour), (windowsize[0]-offset[0],0))
	offset = font.size(str(score_ball))
	screen.blit(font.render(str(score_ball), False, game_colour), (windowsize[0]/2 - offset[0]/2, windowsize[1]-offset[1]))
	pygame.display.flip()

Leave a Reply