BestTechie Forums: Pong - BestTechie Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Pong


Rate Topic: -----

#1 User is offline   Naming is hard 

  • Techie
  • PipPipPipPipPip
  • Group: Members
  • Posts: 518
  • Joined: 16-August 05

Posted 10 July 2006 - 04:57 AM

Heh, Simplist pong game you could make.
I couldn't get the scoring working for 2players or change the balls direction in mid-game so the ball starts off at an angle.
Still took me about aday though.

EDIT:
Opps i forgot to say, W and S for player one, and Up and Down arrows for player two.

#Pong
#Ryan Luna 7/9/06

import random
from livewires import games, color

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
THE_SCREEN = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT)

class Player1(games.Sprite):

	image = games.load_image("paddle.bmp")
	sound = games.load_sound("paddle.wav")

	def __init__ (self, screen, x, y):
		self.init_sprite(screen = screen, x = x, y = y, image = Player1.image)
	
	def moved(self):
		#Moving the paddle for player1
		x, y = self.get_pos()
		if self.screen.is_pressed(games.K_w):
			y -= 1		   
		if self.screen.is_pressed(games.K_s):
			y += 1
		self.move_to(x, y)
		self.check_for_collide()
		
	def check_for_collide(self):
		""" Check if pan catches a pizza. """
		if self.overlapping_objects():
			ball = self.overlapping_objects()[0]
			ball.handle_collide()
			Player1.sound.play()

class Player2(games.Sprite):
	
	image = games.load_image("paddle.bmp")
	
	def __init__ (self, screen, x, y):
		self.init_sprite(screen = screen, x = x, y = y, image = Player2.image)

	def moved(self):
		#Moving the paddle for player2
		x, y = self.get_pos()
		if self.screen.is_pressed(games.K_UP):
			y -= 1
		if self.screen.is_pressed(games.K_DOWN):
			y += 1
		self.move_to(x, y)
		self.check_for_collide()
	
	def check_for_collide(self):
		""" Check if pan catches a pizza. """
		if self.overlapping_objects():
			ball = self.overlapping_objects()[0]
			ball.handle_collide()
			Player1.sound.play()
		
class Ball(games.Sprite):
	
	image = games.load_image("ball.bmp")
	sound = games.load_sound("lose.wav")

	def  __init__ (self, screen, x, y, speedy, speedx = 2):
		self.init_sprite(screen = screen, x = x, y = y, dx = speedx,
						 dy = speedy, image = Ball.image)
		self.score_value = 0
		self.score_text = games.Text(screen = self.screen, x = 250, y = 20,
									 text = " ", size = 25, color = color.white)
								
	def moved(self):
		if self.get_left() < 0:
			self.ball_destroy()
			self.create_ball()
			Ball.sound.play()
		if self.get_right() > SCREEN_WIDTH:
			self.ball_destroy()
			self.create_ball()
			Ball.sound.play()
			self.scoring()
		if self.get_bottom() > SCREEN_HEIGHT:
			self.bounce()
		if self.get_top() < 0:
			self.bounce()
	
	def scoring(self):
		self.score_value += 10
		self.score_text.set_text("Score: " + str(self.score_value))

	
	def handle_collide(self):
		self.reverse()
		
	def ball_destroy(self):
		self.destroy()
		
	def reverse(self):
		""" Reverse direction. """
		dx, dy = self.get_velocity()
		self.set_velocity((-dx, dy))
		
	def bounce(self):
		""" Bounce off wall """
		dx, dy = self.get_velocity()
		self.set_velocity((dx, -dy))
	
	def create_ball(self):
		y_speed = random.randrange(2) + 1
		Ball(screen = self.screen, x = SCREEN_HEIGHT/2, y = SCREEN_WIDTH/2, speedy = y_speed)
def main():
	my_screen = THE_SCREEN
	
	bg_image = games.load_image("blackscreen.jpg", transparent = False)
	my_screen.set_background(bg_image)
	y_speed = random.randrange(2) + 1
	Player1(screen = my_screen, x = 10, y = SCREEN_HEIGHT/2)
	Player2(screen = my_screen, x = 630, y = SCREEN_HEIGHT/2)
	Ball(screen = my_screen, x = SCREEN_HEIGHT/2, y = SCREEN_WIDTH/2, speedy = y_speed)
	my_screen.mouse_visible(False)

	my_screen.mainloop()

main()


I'd attach it but it wont upload .py

This post has been edited by Naming is hard: 21 July 2006 - 05:13 PM


#2 User is offline   naraku9333 

  • I never sleep, 'cause sleep is the cousin of death.
  • PipPipPipPip
  • Group: Members
  • Posts: 298
  • Joined: 06-November 04
  • Location:Brentwood, New York
  • Operating System:Desktop:Gentoo Linux 2.6.16-gentoo-r9 Laptop: Kubuntu 6.06

Posted 10 July 2006 - 03:18 PM

Here is a link to the livewires module http://www.livewires...veWires-2.0.tgz (direct link) which is needed to run. Also there are several references to images I dont have, maybe you can zip the package and attach.

#3 User is offline   Naming is hard 

  • Techie
  • PipPipPipPipPip
  • Group: Members
  • Posts: 518
  • Joined: 16-August 05

Posted 11 July 2006 - 05:44 AM

Wow, yeah how stupid am'i.
Here they are.
Attached File  Sprites.zip (5.89K)
Number of downloads: 48

This post has been edited by Naming is hard: 11 July 2006 - 05:45 AM


#4 User is offline   Naming is hard 

  • Techie
  • PipPipPipPipPip
  • Group: Members
  • Posts: 518
  • Joined: 16-August 05

Posted 12 July 2006 - 11:26 AM

I wanna try adding sound, anyone know where i can get some good sound effects?

#5 User is offline   naraku9333 

  • I never sleep, 'cause sleep is the cousin of death.
  • PipPipPipPip
  • Group: Members
  • Posts: 298
  • Joined: 06-November 04
  • Location:Brentwood, New York
  • Operating System:Desktop:Gentoo Linux 2.6.16-gentoo-r9 Laptop: Kubuntu 6.06

Posted 21 July 2006 - 01:30 PM

I get this error when running on linux with python-2.4.3, pygame-1.6.2, and livewires-2.0
Traceback (most recent call last):
  File "pong.py", line 119, in ?
	main()
  File "pong.py", line 114, in main
	Ball(screen = my_screen, x = SCREEN_HEIGHT/2, y = SCREEN_WIDTH/2, speedy = y_speed)
  File "pong.py", line 72, in __init__
	dy = speedy, image = Ball.image)
TypeError: init_sprite() got an unexpected keyword argument 'dx'
Exception exceptions.AttributeError: "Ball instance has no attribute '_gone'" in <bound method Ball.__del__ of <__main__.Ball instance at 0xb7cb59ec>> ignored

This post has been edited by naraku9333: 21 July 2006 - 01:31 PM


#6 User is offline   Naming is hard 

  • Techie
  • PipPipPipPipPip
  • Group: Members
  • Posts: 518
  • Joined: 16-August 05

Posted 21 July 2006 - 05:09 PM

Thats odd, ill have to compile it on Linux and see whats wrong.

This post has been edited by Naming is hard: 21 July 2006 - 05:13 PM


#7 User is offline   naraku9333 

  • I never sleep, 'cause sleep is the cousin of death.
  • PipPipPipPip
  • Group: Members
  • Posts: 298
  • Joined: 06-November 04
  • Location:Brentwood, New York
  • Operating System:Desktop:Gentoo Linux 2.6.16-gentoo-r9 Laptop: Kubuntu 6.06

Posted 23 July 2006 - 04:03 PM

I get the same error in windows as well. Are we using different versoins of livewires?

#8 User is offline   Naming is hard 

  • Techie
  • PipPipPipPipPip
  • Group: Members
  • Posts: 518
  • Joined: 16-August 05

Posted 23 July 2006 - 07:38 PM

2.0 Livewire, pygame-1.7, Python 2.4

#9 User is offline   naraku9333 

  • I never sleep, 'cause sleep is the cousin of death.
  • PipPipPipPip
  • Group: Members
  • Posts: 298
  • Joined: 06-November 04
  • Location:Brentwood, New York
  • Operating System:Desktop:Gentoo Linux 2.6.16-gentoo-r9 Laptop: Kubuntu 6.06

Posted 23 July 2006 - 09:22 PM

I still wonder about the livewires packages, I had to change all instances of color to colour with my version.

#10 User is offline   Naming is hard 

  • Techie
  • PipPipPipPipPip
  • Group: Members
  • Posts: 518
  • Joined: 16-August 05

Posted 24 July 2006 - 02:08 AM

View Postnaraku9333, on Jul 23 2006, 08:22 PM, said:

I still wonder about the livewires packages, I had to change all instances of color to colour with my version.



LOL, thats so weird.

#11 User is offline   bobbycan 

  • Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 09-August 10

Posted 09 August 2010 - 09:30 PM

View PostNaming is hard, on 24 July 2006 - 02:08 AM, said:

View Postnaraku9333, on Jul 23 2006, 08:22 PM, said:

I still wonder about the livewires packages, I had to change all instances of color to colour with my version.



LOL, thats so weird.

mine duznt work either

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users