Man, I just find strings very confusing in Python.
But uhh, I made a new program which is pretty much a pong game. Well, the idea of making it pong wasn't my own. The book I'm reading instructed me to create a pong game.
But anyways, here's what I got [final product]:
from gasp import *
PLAYER1_WINS = 0
PLAYER2_WINS = 1
QUIT = -1
def hit(bx, by, r, px, py, h): #calculates if ball hits paddle
if abs(py - by) < h and abs(px - bx) <= r:
return True
def play_round():
ball_x = 400 #creates the pong ball's properties
ball_y = 300
ball = Circle((ball_x, ball_y), 10, filled=True)
dx = 4 #creates the original ball movement direction
dy = random_between(-5, 5)
paddle_width = 15 #overall paddle properties
paddle_height = 55
paddle_x = 20 #player 1's paddle properties
paddle_y = 300
paddle = Box((paddle_x, paddle_y), paddle_width, paddle_height, filled = True, color = color.BLUE)
paddle2_x = 780 #player 2's paddle properties
paddle2_y = 300
paddle2 = Box((paddle2_x, paddle2_y), paddle_width, paddle_height, filled = True, color = color.BLUE)
while True:
if ball_y >= 590 or ball_y <= 10: #bounce ball off of top and bottom walls
dy *= -1
ball_x += dx
ball_y += dy
#moves the ball with the updated x and y coordinates from previous 2 lines
move_to(ball, (ball_x, ball_y))
#user input section to move paddles; will not advance past the screen limit
if key_pressed('a') and paddle_y <= 580:
paddle_y += 5
elif key_pressed('s') and paddle_y >= 20:
paddle_y -= 5
if key_pressed('j') and paddle2_y <= 580:
paddle2_y += 5
elif key_pressed('k') and paddle2_y >= 20:
paddle2_y -= 5
if key_pressed('escape'): #have no idea if this will work until someone tests it <img src='http://www.neocodex.us/forum/public/style_emoticons/<#EMO_DIR#>/sad.gif' class='bbc_emoticon' alt=':(' />
return QUIT
#move paddles according to new values from user input
move_to(paddle, (paddle_x, paddle_y))
move_to(paddle2, (paddle2_x, paddle2_y))
#hit function used to hit pong ball back
if hit(ball_x, ball_y, paddle_width, paddle_x, paddle_y, paddle_height):
dx *= -1
elif hit(ball_x, ball_y, paddle_width, paddle2_x, paddle2_y, paddle_height):
dx *= -1
'''once pong ball advances screen horizontally,
program will return 1[player 2] or 0[player 1] according to
which side the ball had advanced on
'''
if ball_x >= 810:
remove_from_screen(ball)
remove_from_screen(paddle)
remove_from_screen(paddle2)
return PLAYER1_WINS
elif ball_x <= -10:
remove_from_screen(ball)
remove_from_screen(paddle)
remove_from_screen(paddle2)
return PLAYER2_WINS
update_when('next_tick')
def play_game():
player1_score = 0
player2_score = 0
while True:
#show player scores at top
pmsg = Text("Player 1: %d Points" % player1_score, (10, 570), size=24)
cmsg = Text("Player 2: %d Points" % player2_score, (640, 570), size=24)
sleep(3)
remove_from_screen(pmsg)
remove_from_screen(cmsg)
result = play_round() #initiate single round of pong
if result == PLAYER1_WINS:
player1_score += 1
elif result == PLAYER2_WINS:
player2_score += 1
else:
return QUIT
if player1_score == 5:
return PLAYER1_WINS
elif player2_score == 5:
return PLAYER2_WINS
begin_graphics(800, 600, title="Catch", background=color.WHITE) #initiate Gasp
set_speed(120) #frame rate speed
result = play_game() #initiate game of pong
if result == PLAYER1_WINS:
Text("Player 1 Wins!", (340, 290), size=32)
elif result == PLAYER2_WINS:
Text("Player 2 Wins!", (340, 290), size=32)
sleep(4)
end_graphics() #currently doesnt work ;(, stupid PC Gasp issues...
And well most things in the code seem to be OK and the game functions with no bugs [as far as I've seen]. But for some odd reason user input is just not allowed. The coding is fine but when I press any of the paddle movement keys they just wont work. Does anyone have the Gasp module so they could test this out? Since I usually have problems in general with Gasp [I cant close it without my computer freezing >.<].
Thanks. Sorry.
Edit: Holy crap I never realized how incredibly long that was
Well then, I'll add in an extra sorry beside that thanks. :$
Edited by AbrarCat, 25 August 2010 - 03:27 PM.