classic game: pong

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

classic game: pong

Postby jelseg » Mon Jun 17, 2013 11:41 am

I'm new to python and I just finished recreating the classic game pong using pygame.
You can play it using the mouse.

Code: Select all
import pygame
import time

pygame.init()
pygame.display.set_caption('pong')

class ball:
    def __init__(self,vx,vy,r,font,height,width,planksize,startspeed):
        self.startspeed=(vx,vy)
        self.X=width/2
        self.Y=height/2
        self.startpos=(width/2,height/2)
        self.vx=vx
        self.vy=vy
        self.r=r
        self.scoreplayer=0
        self.scorepc=0
        self.font=font
        self.height=height
        self.width=width
        self.planksize=planksize
        self.player=height/2
        self.pc=height/2
        self.pcspeed=float(startspeed)
        self.screen=pygame.display.set_mode((width,height))
        self.nhit=True

    def draw(self):
        self.screen.fill((0,0,0))
        pygame.draw.circle(self.screen,(255,255,255),(int(self.X),int(self.Y)),self.r)
        self.text= 'Your score: %d        computers score:%d' %(self.scoreplayer , self.scorepc )
        self.label=self.font.render(self.text,0,(255,255,255))
        self.screen.blit(self.label,(3,3))
        pygame.draw.rect(self.screen,(255,255,255),(1,self.player - self.planksize /2,5,self.planksize))
        pygame.draw.rect(self.screen,(255,255,255),(self.width-6,self.pc-self.planksize /2,5,self.planksize))
        pygame.display.flip()

    def move(self):
        if self.Y<=self.r or self.Y>=(self.height-self.r):
            self.vy=-self.vy
        if self.X<=(6+self.r) and self.nhit:
            self.nhit=False
            if self.Y<=(self.player+self.planksize /2 +self.r) and self.Y>=(self.player-self.planksize /2 -self.r):
                if self.Y<=(self.player+self.planksize/8) and self.Y>=(self.player-self.planksize/8):
                    self.vx=-1.2*self.vx
                elif self.Y<=(self.player+self.planksize/4) and self.Y>=(self.player):
                    self.vy+=1
                    self.vx=-self.vx
                elif self.Y<=(self.player) and self.Y>=(self.player-self.planksize/4):
                    self.vy-=1
                    self.vx=-self.vx
                elif self.Y>self.player:
                    self.vy+=1.5
                    self.vx=-0.9*self.vx
                else :
                    self.vx=-0.9*self.vx
                    self.vy-=1.5
            else:
                self.scorepc +=1
                self.pcspeed = 0.75*self.pcspeed
                self.X , self.Y = self.startpos
                self.vx , self.vy = self.startspeed

        elif self.X>=(self.width-6-self.r) and self.nhit:
            self.nhit=False
            if (self.Y<self.pc+self.planksize /2 +self.r) and (self.Y>self.pc-self.planksize /2 -self.r):
                if self.Y<=(self.pc+self.planksize/8) and self.Y>=(self.pc-self.planksize/8):
                    self.vx=-1.2*self.vx
                elif self.Y<=(self.pc+self.planksize/4) and self.Y>=(self.pc):
                    self.vy+=1
                    self.vx=-self.vx
                elif self.Y<=(self.pc) and self.Y>=(self.pc-self.planksize/4):
                    self.vy-=1
                    self.vx=-self.vx
                elif self.Y>self.pc:
                    self.vy+=1.5
                    self.vx=-0.9*self.vx
                else:
                    self.vy-=1.5
                    self.vx=-0.9*self.vx
            else:
                self.scoreplayer +=1
                if self.pcspeed*1.25<1:
                    self.pcspeed = 1.25*self.pcspeed
                self.X , self.Y = self.startpos
                self.vx , self.vy = -self.startspeed[0], -self.startspeed[1]
        else :
            self.nhit=True


        if self.Y<>self.pc:
            self.pc -= int(self.pcspeed*(self.pc-self.Y))

        self.X = (self.X + self.vx)
        self.Y = ( self.Y + self.vy)


width=550
height=400
planksize=20
font=pygame.font.Font(None,25)
Field=ball(-5,0,3,font,height,width,planksize,0.5)

running=True
while running:
    Field.draw()
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            running=False
        elif event.type == pygame.MOUSEMOTION:
            x, y = event.pos
            if y<height and y>0:
                Field.player=y
    Field.move()
    time.sleep(0.1)


I hope I can have some comments and tips.
jelseg
 
Posts: 3
Joined: Mon Jun 17, 2013 11:25 am

Re: classic game: pong

Postby Mekire » Tue Jun 18, 2013 1:32 am

It is very nice to see that you are using classes and functions in your first few games.

Let's start off with few simple things. Firstly you need to learn to use Rects. Any time you would represent an object that has both a coordinate location as well as a width and a height, you should probably be using one. See the documentation here.

Next; you don't need the time module. Pygame already contains pygame.time. Also, whether it is time or pygame.time, you really shouldn't use sleep/wait/delay. It is hard to see the disadvantage of these for people in their first few games but this method makes it impossible to update multiple actors at different speeds. You should use a combination of
Code: Select all
Clock.tick(desired_fps) #where Clock is an instance of pygame.time.Clock
to constrain the overall frame rate of your game and
Code: Select all
pygame.time.get_ticks()
to limit specific events.

-Mek
User avatar
Mekire
 
Posts: 818
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: classic game: pong

Postby Hissing Cockroach » Sun Sep 22, 2013 5:51 pm

This is REALLY nice for a beginner.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests