NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import pygame, sys
from pygame.locals import *

# Number of frames per second
# Change this value to speed up or slow down your game
FPS = 200

#Global Variables to be used through our program

WINDOWWIDTH = 400
WINDOWHEIGHT = 300
LINETHICKNESS = 10
PADDLESIZE = 50
PADDLEOFFSET = 20

# Set up the colours
BLACK = (0 ,0 ,0 )
WHITE = (255,255,255)

#Draws the arena the game will be played in.
def drawArena():
DISPLAYSURF.fill((0,0,0))
#Draw outline of arena
pygame.draw.rect(DISPLAYSURF, WHITE, ((0,0),(WINDOWWIDTH,WINDOWHEIGHT)), LINETHICKNESS*2)
#Draw centre line
pygame.draw.line(DISPLAYSURF, WHITE, ((WINDOWWIDTH/2),0),((WINDOWWIDTH/2),WINDOWHEIGHT), (LINETHICKNESS/4))


#Draws the paddle
def drawPaddle(paddle):
#Stops paddle moving too low
if paddle.bottom > WINDOWHEIGHT - LINETHICKNESS:
paddle.bottom = WINDOWHEIGHT - LINETHICKNESS
#Stops paddle moving too high
elif paddle.top < LINETHICKNESS:
paddle.top = LINETHICKNESS
#Draws paddle
pygame.draw.rect(DISPLAYSURF, WHITE, paddle)


#draws the ball
def drawBall(ball):
pygame.draw.rect(DISPLAYSURF, WHITE, ball)

#moves the ball returns new position
def moveBall(ball, ballDirX, ballDirY):
ball.x += ballDirX
ball.y += ballDirY
return ball

#Checks for a collision with a wall, and 'bounces' ball off it.
#Returns new direction
def checkEdgeCollision(ball, ballDirX, ballDirY):
if ball.top == (LINETHICKNESS) or ball.bottom == (WINDOWHEIGHT - LINETHICKNESS):
ballDirY = ballDirY * -1
if ball.left == (LINETHICKNESS) or ball.right == (WINDOWWIDTH - LINETHICKNESS):
ballDirX = ballDirX * -1
return ballDirX, ballDirY

#Checks is the ball has hit a paddle, and 'bounces' ball off it.
def checkHitBall(ball, paddle1, paddle2, ballDirX):
if ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
return -1
elif ballDirX == 1 and paddle2.left == ball.right and paddle2.top < ball.top and paddle2.bottom > ball.bottom:
return -1
else: return 1

#Checks to see if a point has been scored returns new score
def checkPointScored(paddle1, ball, score, ballDirX):
#reset points if left wall is hit
if ball.left == LINETHICKNESS:
return 0
#1 point for hitting the ball
elif ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
score += 1
return score
#5 points for beating the other paddle
elif ball.right == WINDOWWIDTH - LINETHICKNESS:
score += 5
return score
#if no points scored, return score unchanged
else: return score

#Artificial Intelligence of computer player
def artificialIntelligence(ball, ballDirX, paddle2):
#If ball is moving away from paddle, center bat
if ballDirX == -1:
if paddle2.centery < (WINDOWHEIGHT/2):
paddle2.y += 1
elif paddle2.centery > (WINDOWHEIGHT/2):
paddle2.y -= 1
#if ball moving towards bat, track its movement.
elif ballDirX == 1:
if paddle2.centery < ball.centery:
paddle2.y += 1
else:
paddle2.y -=1
return paddle2

#Displays the current score on the screen
def displayScore(score):
resultSurf = BASICFONT.render('Score = %s' %(score), True, WHITE)
resultRect = resultSurf.get_rect()
resultRect.topleft = (WINDOWWIDTH - 150, 25)
DISPLAYSURF.blit(resultSurf, resultRect)

#Main function
def main():
pygame.init()
global DISPLAYSURF
##Font information
global BASICFONT, BASICFONTSIZE
BASICFONTSIZE = 20
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)

FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
pygame.display.set_caption('Pong')

#Initiate variable and set starting positions
#any future changes made within rectangles
ballX = WINDOWWIDTH/2 - LINETHICKNESS/2
ballY = WINDOWHEIGHT/2 - LINETHICKNESS/2
playerOnePosition = (WINDOWHEIGHT - PADDLESIZE) /2
playerTwoPosition = (WINDOWHEIGHT - PADDLESIZE) /2
score = 0

#Keeps track of ball direction
ballDirX = -1 ## -1 = left 1 = right
ballDirY = -1 ## -1 = up 1 = down

#Creates Rectangles for ball and paddles.
paddle1 = pygame.Rect(PADDLEOFFSET,playerOnePosition, LINETHICKNESS,PADDLESIZE)
paddle2 = pygame.Rect(WINDOWWIDTH - PADDLEOFFSET - LINETHICKNESS, playerTwoPosition, LINETHICKNESS,PADDLESIZE)
ball = pygame.Rect(ballX, ballY, LINETHICKNESS, LINETHICKNESS)

#Draws the starting position of the Arena
drawArena()
drawPaddle(paddle1)
drawPaddle(paddle2)
drawBall(ball)

pygame.mouse.set_visible(0) # make cursor invisible

while True: #main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# mouse movement commands
elif event.type == MOUSEMOTION:
mousex, mousey = event.pos
paddle1.y = mousey

drawArena()
drawPaddle(paddle1)
drawPaddle(paddle2)
drawBall(ball)

ball = moveBall(ball, ballDirX, ballDirY)
ballDirX, ballDirY = checkEdgeCollision(ball, ballDirX, ballDirY)
score = checkPointScored(paddle1, ball, score, ballDirX)
ballDirX = ballDirX * checkHitBall(ball, paddle1, paddle2, ballDirX)
paddle2 = artificialIntelligence (ball, ballDirX, paddle2)

displayScore(score)

pygame.display.update()
FPSCLOCK.tick(FPS)

if __name__=='__main__':
main()
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.