QuestionQuestion

Objectives
- Understand basic elements of pygame
- Understand how to use algebra and geometry to simulate basic physics
- Understand how to accept user input in pygame
- Implement a simple game of air hockey

Tasks
1. Get the player names for Player One and Player Two.
2. Print instructions for the players.
a. Player One controls the blue mallet on the left with WASD.
b. Player Two controls the red mallet on the right with the arrow keys.
c. At the start of the game, the puck is placed in the center circle on the board. After a five second countdown, play begins and either player can push the puck.
d. Neither player can push any part of the mallet past the centerline.
e. If the puck goes in the goal, a point is scored by the player on the opposite side of the game table. This is true regardless of which player knocks the puck in the goal.
f. The player who was scored against can move the mallet and puck to any legal position before hitting the puck. SPACE is used to push the puck after a goal.
g. The first player to score ten goals wins the game.
3. After the instructions are printed on the screen, wait for ENTER to begin play.
4. The game table is 640 X 480, with a red center circle, a vertical red center line, a goal on the left, and a goal on the right. The goals should be slightly larger than the puck.
5. The puck is a black circle with radius 25.
6. The mallet for Player One is a blue rectangle, no part of which may leave the left half of the game table. The mallet for Player Two is a red rectangle, no part of which may leave the right half of the table.
7. The name and score for each player is on the upper part of the table in the color of the player's mallet.
8. Create a countdown screen for 2c.
9. The puck must always remain on the table unless it goes into the goal. Otherwise, it must bounce off the top, sides, or bottom.
10. If the puck enters a goal, a point is scored by the player on the opposite side of the table.
11. When a goal is scored, the puck reappears on the screen and can be moved using the mouse by the player who lost the point. The player can move the puck to any legal position before hitting it with the mallet to resume play.
12. (Bonus) If the puck enters at an angle that does not permit it to go all the way into the goal, then it can rattle and either bounce out or stop. If it stops partially in the goal, then the player on that side of the table can move the puck as if a goal had been scored.
13. (Bonus) Allow players to control the velocity with which the puck is hit in 10.
14. (Bonus) Allow players to control the velocity with which the puck is hit at any time.
15. When a player wins, a screen with the winner's name and the final score should be shown.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import pygame
import time
from pygame.locals import *

pygame.init()

white = (255,255,255)
black = (0,0,0)
green = (0,150,0)
red = (255,0,0)
blue = (0,0,255)
light_blue = (147,251,253)

#Clock initialized
clock= pygame.time.Clock()
#Board Size
screen= pygame.display.set_mode((680,480))
#dividing line
divline1 = screen.get_width()/2, 0
divline2 = screen.get_width()/2 ,screen.get_height()
#Caption
pygame.display.set_caption('Air Hockey!')
#Font Sizes
smallfont = pygame.font.SysFont("comicsansms" , 25)
medfont = pygame.font.SysFont("comicsansms" , 45)
largefont = pygame.font.SysFont("comicsansms" , 65)

#Create Game Objects
goalheight = 50
goalwidth = 20
goal1 = pygame.Rect(0,screen.get_height()/2 - 50,10,100)
goal2 = pygame.Rect(screen.get_width()-10,screen.get_height()/2 - goalheight,10,100)
paddle1= pygame.Rect(screen.get_width()/2-200,screen.get_height()/2,20,20)
paddle2= pygame.Rect(screen.get_width()/2+200,screen.get_height()/2,20,20)
paddleVelocity= 4
disc= pygame.Rect(screen.get_width()/2,screen.get_height()/2,20,20)
#pygame.draw.rect(gameDisplay,red,[screen.get_width()/2,screen.get_height()/2,20,20])
#divider= pygame.Rect(screen.get_width()/2,0,3,screen.get_height())
discVelocity= [5,5]
img = pygame.image.load('./disc.png')
#background = pygame.image.load('./background.jpg')
bluepadimg = pygame.image.load('./bluepad.png')
redpadimg = pygame.image.load('./redpad.png')

#Score
score1,score2 = 0,0
serveDirection=1

def resetPuck():
    discVelocity[0]=5*serveDirection
    discVelocity[1]=5*serveDirection
    print(score1,score2)
    disc.x= screen.get_width()/2
    disc.y= screen.get_height()/2

def text_objects(text,color,size):
    if size == "small":
       textSurface = smallfont.render(text , True , color)
    elif size == "medium":
       textSurface = medfont.render(text , True , color)   
    elif size == "large":
       textSurface = largefont.render(text , True , color)
    return textSurface , textSurface.get_rect()

def pause():
    paused = True
    message_to_screen("Paused",black,-100,size="large")
    message_to_screen("Press c to continue , q to quit",black,25)
    pygame.display.update()
    while paused:
       for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                   paused = False

                elif event.key == pygame.K_q:
                   pygame.quit()
                   quit()
       #gameDisplay.fill(white)
       clock.tick(5)   

def message_to_screen(msg,color,y_displace=0,x_displace=0,size = "small"):
    textSurf, textRect = text_objects(msg,color,size)
    textRect.center = (screen.get_width()/2+x_displace) , ((screen.get_height()/2) + y_displace)
    screen.blit(textSurf,text...

By purchasing this solution you'll be able to access the following files:
Solution.docx and Solution.py.

$20.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats