Public feed
#889915
############################################################################
#Module to set the game board and control the game ball ####################
############################################################################
import pygame, os, sys
from pygame.locals import *
import GlobVar, random
class game():
def Start(self):
#set up the game board and load all images, including changing the game board background#
GlobVar.screen.fill ((0,0,0))
GlobVar.screen.blit(GlobVar.GameBall,(GlobVar.GBallX,GlobVar.GBallY))
GlobVar.screen.blit(GlobVar.Pad,(GlobVar.PlayerX,GlobVar.PlayerY))
GlobVar.screen.blit(GlobVar.Pad,(GlobVar.CompX,GlobVar.CompY))
#set the random number for ball to start with#
self.x = 0
self.x = GlobVar.RandChoice
self.Xmove = 1
#draw and control the ball#
def Ball(self):
if GlobVar.BallSpace == "GameBoard":
GlobVar.screen.blit(GlobVar.EBall,(GlobVar.GBallX,GlobVar.GBallY))
GlobVar.GBallX = GlobVar.GBallX + self.Xmove
GlobVar.screen.blit(GlobVar.GameBall,(GlobVar.GBallX,GlobVar.GBallY))
#control the player#
def Player(self):
#input#
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
exit()
if event.key == K_UP and GlobVar.PlayerY > 20:
GlobVar.screen.blit(GlobVar.ErasePad,(GlobVar.PlayerX,GlobVar.PlayerY))
GlobVar.PlayerY = GlobVar.PlayerY - 10
GlobVar.screen.blit(GlobVar.Pad,(GlobVar.PlayerX,GlobVar.PlayerY))
if event.key == K_DOWN and GlobVar.PlayerY < 530:
GlobVar.screen.blit(GlobVar.ErasePad,(GlobVar.PlayerX,GlobVar.PlayerY))
GlobVar.PlayerY = GlobVar.PlayerY + 10
GlobVar.screen.blit(GlobVar.Pad,(GlobVar.PlayerX,GlobVar.PlayerY))
#draw and control the computer#
def Computer(self):
GlobVar.screen.blit(GlobVar.Pad,(GlobVar.CompX,GlobVar.CompY))
def Collide(self):
if GlobVar.GBallY == GlobVar.PlayerY:
self.Xmove = -2
####################################################################################
#Every variable that a module needs to share with another module will be found here#
####################################################################################
import pygame, os, sys, random
from pygame.locals import*
#the screen global#
global screen
screen = pygame.display.set_mode ((800,600))
pygame.display.set_caption ("!!!PONG!!!")
#background image#
global BackGround
BackGround = pygame.image.load (os.path.join ("Data","menubackground.jpeg"))
BackGround = BackGround.convert()
#ball image#
global Ball
Ball = pygame.image.load (os.path.join ("Data", "ball.jpeg"))
Ball = Ball.convert()
#eraseball image#
global EBall
EBall = pygame.image.load (os.path.join ("Data","eraseball.jpeg"))
EBall= EBall.convert()
#Menu Select variable to keep track of choice at main menu#
global MenuSel
MenuSel = "none"
#Game Ball And position variables#
global GameBall
global GBallX
global GBallY
global BallSpace
GameBall = pygame.image.load (os.path.join("Data", "ball.jpeg"))
GBallX = 400
GBallY = 300
BallSpace = "GameBoard"
#set the random number for ball to start with#
global RandChoice
RandChoice = random.choice([0,1,2,3,4,5])
#paddles and possition variables#
global Pad
global ErasePad
global PlayerX
global PlayerY
global CompX
global CompY
global PlayerCspace
ErasePad = pygame.image.load (os.path.join ("Data", "erasepaddle.jpeg"))
Pad = pygame.image.load (os.path.join ("Data","paddle.jpeg"))
PlayerX = 780
PlayerY = 280
CompX = 10
CompY = 280
#player collision space
PlayerCspace = PlayerX - 50
#############################################################################
#The main menu classes for pong #
#############################################################################
import pygame, os, sys
from pygame.locals import *
import GlobVar
class menu():
def DrawMenu(self):
#blit image to screen#
GlobVar.screen.blit(GlobVar.BackGround,(0,0))
def DrawBall(self):
#setting possitional variables#
self.BallX = 140
self.BallY = 140
#blit image#
GlobVar.screen.blit(GlobVar.Ball,(self.BallX,self.BallY))
def BallSel(self):
#checks for input#
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
#lets use use the escape key to exit game#
if event.key == K_ESCAPE:
exit()
#moves ball to bottom choice and checks to see if its there,and exits game
if event.key == K_DOWN and self.BallY != 200:
GlobVar.screen.blit(GlobVar.EBall,(self.BallX,self.BallY))
self.BallY = self.BallY + 60
GlobVar.screen.blit(GlobVar.Ball,(self.BallX,self.BallY))
if event.key == K_RETURN and self.BallY == 200:
GlobVar.MenuSel = "exit"
#moves ball to top choice and checks to see if its there,and exits loop
if event.key == K_UP and self.BallY != 140:
GlobVar.screen.blit(GlobVar.EBall,(self.BallX,self.BallY))
self.BallY = self.BallY - 60
GlobVar.screen.blit(GlobVar.Ball,(self.BallX,self.BallY))
if event.key == K_RETURN and self.BallY == 140:
GlobVar.MenuSel = "enterpong"
if GlobVar.MenuSel == "exit":
exit()
#####################################################################################################
# original author: Derek Mcdonald #
# with help from: R.J. Marsan #
#####################################################################################################
# A recreation of the classic title "Pong" #
#####################################################################################################
import pygame
from pygame.locals import *
import MainMenu, GlobVar, game
#initialize the pygame engine#
pygame.init()
#makes it so you can hold a key#
pygame.key.set_repeat(50,0)
#make our imported classes into callable objects#
Menu = MainMenu.menu()
Game = game.game()
#Call all objects to set up the game before the menu loop begins#
Menu.DrawMenu()
Menu.DrawBall()
#The mainloop variable, like a switch to the world............#
MainLoopVar = 1
GameLoop = 1
#main loop#
while MainLoopVar == 1:
#the menu and its items and functions#
Menu.BallSel()
pygame.display.flip()
#test to see if play the game was selcted and then enters game loop#
if GlobVar.MenuSel == "enterpong":
while GameLoop == 1:
Game.Start()
Game.Ball()
Game.Player()
Game.Computer()
Game.Collide()
pygame.display.flip()