diff options
| author | Ming Chow | 2012-03-04 19:26:51 -0500 | 
|---|---|---|
| committer | Ming Chow | 2012-03-04 19:26:51 -0500 | 
| commit | c9d5eef6c9edf1e4b89bb7edde4ce30ce3c6935a (patch) | |
| tree | b1d4c819edb96e5348d011503e16502e8d0df18e | |
| download | GameLabs-c9d5eef6c9edf1e4b89bb7edde4ce30ce3c6935a.tar.bz2 | |
Finished instructions
| -rw-r--r-- | README.txt | 56 | ||||
| -rw-r--r-- | pong.py | 83 | 
2 files changed, 139 insertions, 0 deletions
| diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..831a197 --- /dev/null +++ b/README.txt @@ -0,0 +1,56 @@ +#Objectives# +In this lab, you will: +* Practice collision detection and response +* Practice using Git and GitHub +* Hack Pong + +#Overview# +I have provided `pong.py`.  Run the program via `python pong.py` +(Pygame required).  Move the paddle with either the UP or DOWN arrow +keys or use the mouse.  If your paddle hits the ball, you score a +point.  The problem is: this isn't Pong as you know it.  Enough said. + +#Instructions# +* (1 point) There is only one score --for you.  Add another score on +  the display for the opponent.  Your score shall be displayed at the +  top middle half of the left side of the board while the opponent's +  score shall be displayed at the top middle half of the right side of +  the board. + +* (1 point) As of right now, if you miss the ball (that is, the ball +  hits your wall on the left), it bounces.  Change the source code so +  that if the ball hits your wall, the opponent scores a point. + +* (1 point) Change the source code so that instead of receiving a +  point if your paddle hits the ball, you receive a point only if the +  ball hits the right wall. + +* (1 point) When the ball collides with a paddle, play a sound (find +  one). + +* (1 point) Implement a second paddle on the right side. :-) Duh! + +* (1 point) Draw a line (e.g., a dotted black line, a solid red line) +  dividing the playing surface, as in the real game of Pong. + +* (1 point) Change the source so if the ball hits a wall (i.e., +  someone scores), reset the ball to the middle of the playing surface +  and move the ball to the player who got scored on. + +* (1 point) Change the source so if a player scores 11 points, the +  game is over.  That is, display who wins and prompt for a rematch. +  If rematch, reset the scores to 0 - 0. + +* Either: (1 point) change your paddle to move using the "W" (up) and +"S" (down) keys and use the UP and DOWN arrows for the opponent +paddle, OR (1 point + 1 bonus point) make the opponent paddle play on +its own. But don't make the opponent impossibly hard! + +* (1 point) Push your work to your GitHub account. + +#Submission# +Push your work to your GitHub account and *send me a message (to +mchow01) via Notifications in GitHub.* Notifications is under the +radio waves icon to the right of your GitHub username (and to the +right of the Account Settings and Log Out icons) at the top navigation +bar. @@ -0,0 +1,83 @@ +import pygame, sys + +# Constants +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 600 +PADDLE_START_X = 10 +PADDLE_START_Y = 20 +PADDLE_WIDTH = 10 +PADDLE_HEIGHT = 100 +BALL_SPEED = 10 +BALL_WIDTH_HEIGHT = 16 + +pygame.init() +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) +pygame.display.set_caption("Pong") + +# This is a rect that contains the ball at the beginning it is set in the center of the screen +ball_rect = pygame.Rect((SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2), (BALL_WIDTH_HEIGHT, BALL_WIDTH_HEIGHT)) + +# Speed of the ball (x, y) +ball_speed = [BALL_SPEED, BALL_SPEED] + +# Your paddle vertically centered on the left side +paddle_rect = pygame.Rect((PADDLE_START_X, PADDLE_START_Y), (PADDLE_WIDTH, PADDLE_HEIGHT)) + +# Scoring: 1 point if you hit the ball, -5 point if you miss the ball +score = 0 + +# Load the font for displaying the score +font = pygame.font.Font(None, 30) + +# Game loop +while True: +	# Event handler +	for event in pygame.event.get(): +		if event.type == pygame.QUIT: +			sys.exit(0) +			pygame.quit() +		# Control the paddle with the mouse +		elif event.type == pygame.MOUSEMOTION: +			paddle_rect.centery = event.pos[1] +			# correct paddle position if it's going out of window +			if paddle_rect.top < 0: +				paddle_rect.top = 0 +			elif paddle_rect.bottom >= SCREEN_HEIGHT: +				paddle_rect.bottom = SCREEN_HEIGHT + +	# This test if up or down keys are pressed; if yes, move the paddle +	if pygame.key.get_pressed()[pygame.K_UP] and paddle_rect.top > 0: +		paddle_rect.top -= BALL_SPEED +	elif pygame.key.get_pressed()[pygame.K_DOWN] and paddle_rect.bottom < SCREEN_HEIGHT: +		paddle_rect.top += BALL_SPEED +	elif pygame.key.get_pressed()[pygame.K_ESCAPE]: +		sys.exit(0) +		pygame.quit() +		 +	# Update ball position +	ball_rect.left += ball_speed[0] +	ball_rect.top += ball_speed[1] + +	# Ball collision with rails +	if ball_rect.top <= 0 or ball_rect.bottom >= SCREEN_HEIGHT: +		ball_speed[1] = -ball_speed[1] +	if ball_rect.right >= SCREEN_WIDTH or ball_rect.left <= 0: +		ball_speed[0] = -ball_speed[0] + +	# Test if the ball is hit by the paddle; if yes reverse speed and add a point +	if paddle_rect.colliderect(ball_rect): +		ball_speed[0] = -ball_speed[0] +		score += 1 +	 +	# Clear screen +	screen.fill((255, 255, 255)) + +	# Render the ball, the paddle, and the score +	pygame.draw.rect(screen, (0, 0, 0), paddle_rect) # Your paddle +	pygame.draw.circle(screen, (0, 0, 0), ball_rect.center, ball_rect.width / 2) # The ball +	score_text = font.render(str(score), True, (0, 0, 0)) +	screen.blit(score_text, ((SCREEN_WIDTH / 2) - font.size(str(score))[0] / 2, 5)) # The score +	 +	# Update screen and wait 20 milliseconds +	pygame.display.flip() +	pygame.time.delay(20) | 
