Hi guys i have 3 question for the pygame pong game I made:
1. I keep getting an error when i run this that has to do iwth the wx window
I made for the user to enter a speed. this is the error code:
Traceback (most recent call last):
File "/home/armin/Documents/School/Programming/Pong/Pong Test.py", line 26, in <module>
frame = MenuBar(parent=None,id=-1)
File "/home/armin/Documents/School/Programming/Pong/Pong Test.py", line 22, in __init__
speed=SpeedBox.Value()
TypeError: 'unicode' object is not callable
2. Second question is is it possible to make a menu bar in a pygame window like you can make in a wxpython window? I looked it it up and found something about a PGU but couldn't find any examples or tutorials about how to actually do it.
3. What else can I do to improve my game? what can I add? Also I wanted to make a menu screen, do i have to do this in wxpython or can i do it in the pygame screen?
You will need a sound file called "pong.wav" and "score.wav" to run this code.
Thank you for your time guys
#Various Declarations
import pygame,math,sys,os,random,pygame.mixer, wx
from pygame.locals import *
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((800,600),0,32)
Font = pygame.font.SysFont("Comic Sans MS", 20)
class MenuBar(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Window',size = (300,200))
panel = wx.Panel(self)
SpeedBox = wx.TextEntryDialog(None,"Pick a speed (1-3)","Speed", "1")
if SpeedBox.ShowModal()==wx.ID_OK:
speed=SpeedBox.Value()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MenuBar(parent=None,id=-1)
frame.Show()
app.MainLoop()
ball_x = 400
ball_y = 280
ball_move_x = speed
ball_move_y = speed
computer_y = 250
playerscore = 0
computerscore = 0
sound = pygame.mixer.Sound('pong.wav')
score = pygame.mixer.Sound('score.wav')
Pause = Font.render("Click p to Pause", 1, (225, 25, 0))
Resume = Font.render("Click r to Resume", 1, (225, 25, 0))
KeepPlaying = True
def playercollide(ball_y,ball_x,paddle_y):
counter = 0
collided = False
if ball_x == 730:
print (ball_y - 25) - (paddle_y + counter)
while counter < 105:
if ball_y - (paddle_y + counter) == 0 or (ball_y - 25) - (paddle_y + counter) > -50 and (ball_y - 25) - (paddle_y + counter) < -10:
PlaySound(sound)
collided = True
break
counter += 1
return collided
def computercollide(ball_y,ball_x,paddle_y):
counter = 0
collided = False
if ball_x == 50:
print (ball_y - 25) - (paddle_y + counter)
while counter < 105:
if ball_y - (paddle_y + counter) == 0 or (ball_y - 25) - (paddle_y + counter) > -50 and (ball_y - 25) - (paddle_y + counter) < -10:
PlaySound(sound)
collided = True
break
counter += 1
return collided
def PlaySound(name):
name.play()
while KeepPlaying:
screen.fill((0,0,0))
screen.blit(Pause, (100,550))
screen.blit(Resume, (600,550))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_p:
KeepPlaying = False
while KeepPlaying == False:
pygame.time.wait(1000)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_r:
KeepPlaying = True
#Gets the mouse position for the players paddles y coordianate
player_x,player_y = pygame.mouse.get_pos()
player_y -= 50
#This moves the ball
ball_x += ball_move_x
ball_y += ball_move_y
#Detects ball collion with the paddles and walls
if ball_y > 600:
PlaySound(sound)
ball_move_y *= -1
if ball_y < 2:
PlaySound(sound)
ball_move_y *= -1
if playercollide(ball_y,ball_x,player_y):
ball_x = 730
ball_move_x *= -1
if computercollide(ball_y,ball_x,computer_y):
ball_x = 60
ball_move_x *= -1
#Detects if the players paddle trys to go outside the screen
if player_y > 500:
player_y = 499
elif player_y < 2:
player_y = 3
#Detects if the computers paddle trys to go outside the screen
if computer_y > 500:
computer_y = 499
elif computer_y < 2:
computer_y = 3
#If the ball is on the computers half the computers paddle will move towards the ball
if ball_x < 400:
if computer_y < ball_y:
computer_y += 2
elif computer_y > ball_y:
computer_y -= 2
#Adds to the score
if ball_x > 780:
PlaySound(score)
computerscore += 1
ball_x = 400
ball_y = 280
elif ball_x < 30:
PlaySound(score)
playerscore += 1
ball_x = 400
ball_y = 280
PlayerScore = Font.render("Player Score: " + str(playerscore), 1, (225, 25, 0))
ComputerScore = Font.render("Computer Score: " + str(computerscore), 1, (225, 25, 0))
#Draws all the objects
screen.lock()
pygame.draw.rect(screen, (0,255,0), Rect((ball_x,ball_y), (25,25)))
pygame.draw.rect(screen, (0,255,0), Rect((740,player_y), (15,100)))
pygame.draw.rect(screen, (0,255,0), Rect((50,computer_y), (15,100)))
screen.unlock()
screen.blit(PlayerScore, (400,50))
screen.blit(ComputerScore, (50,50))
pygame.display.update()