Skip to main content

Python Ping Pong Game

Game: Ping Pong
Libraries used:
1) pygame 
2) Math
3) Random

Version: Python 3.7

Python Code:
--------------------------------------------------------------------------------------------------------------------------
import pygame
import math
import random
pygame.init()
dis=pygame.display.set_mode((600,400))
gameover=False
white=(255,255,255)
black=(0,0,0)
red=(255,0,0)
blue=(0,0,255)
col=(0,255,0)
x1=575
y1=200
dx1=0
dy1=0
x2=25
y2=185
dx2=1
dy2=0
c=0
t=0
font_style = pygame.font.SysFont(None, 50)
def message(msg,color):
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, [300, 200])
font_style1 = pygame.font.SysFont(None, 30)   
def show(msg,color):
    mesg = font_style1.render(msg, True, color)
    dis.blit(mesg, [300, 10])   
clock=pygame.time.Clock()
while not gameover:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            gameover=True
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_UP:
                dy1=-1
                dx1=0
            if event.key==pygame.K_DOWN:
                dy1=1
                dx1=0
    dis.fill(black)
    if y1>=0 and y1<=300:
        x1+=dx1
        y1+=dy1
    else:
        x1=x1
        if y1>300:
            y1=300
        if y1<0:
            y1=0
           
    #Code for bounce
    if c==0:
        x2+=dx2
        y2+=dy2
       
    if x2>=565 and y2<=y1+100 and y2>=y1:
        c=1
      
    if c==1:
        x2-=dx2
        if t==0:
            if y2<=y1+70 and y2>=y1+30:
                dy2=0
            if y2>y1+70:
                dy2=1
            if y2<y1+30:
                dy2=-1
            y2+=dy2
        if t==1:
            y2+=dy2
       
    if x2==25:
        c=0
        t=0
        if y2<=220 and y2>=180:
            dy2=0
        if y2>220:
            dy2=round(random.randrange(-1,1))
        if y2<180:
            dy2=round(random.randrange(-1,1))
    #SET BOUNDARY
    if y2<=0:
        t=1
        dy2=1
    if y2>=400:
        t=1
        dy2=-1
    if x2>600:
        message('You Lost',red)
        gameover=True
    show('MADE BY VISHNU',col) 
    pygame.draw.rect(dis,white,[x1,y1,10,100])
    pygame.draw.rect(dis,white,[10,0,10,400])
    pygame.draw.rect(dis,blue,[0,0,600,5])
    pygame.draw.rect(dis,blue,[0,395,600,5])
    pygame.draw.circle(dis,red,(x2,y2),10)
    pygame.display.update()
    clock.tick(500)
pygame.quit()
quit()
 ----------------------------------------------------------------------------------------------------------------------------

Game Play:
 

Comments

Popular posts from this blog

LED Matrix

Aim: To display a character or letter on a 8x8 LED matrix. Code: #define F_CPU 8000000UL #include<avr/io.h> #include<util/delay.h> int main(){ DDRC=0xFF; DDRD=0xFF; DDRB=0x00; while(1){ if(PINB==0x01){ PORTC=0x08; PORTD=0xEF; _delay_ms(100); PORTC=0x1C; PORTD=0xC7; _delay_ms(100); PORTC=0x3E; PORTD=0x83; _delay_ms(100); PORTC=0x7F; PORTD=0x01; _delay_ms(100); PORTC=0xFF; PORTD=0x00; _delay_ms(100); PORTC=0x7F; PORTD=0x01; _delay_ms(100); PORTC=0x3E; PORTD=0x83; _delay_ms(100); PORTC=0x1C; PORTD=0xC7; _delay_ms(100); PORTC=0x08; PORTD=0xEF; _delay_ms(100); } else if(PINB==0x02){ PORTC=0x02; PORTD=0xF0; _delay_ms(1); PORTC=0x04; PORTD=0xEF; _delay_ms(1); PORTC=0x08; PORTD=0xDF; _delay_ms(1); PORTC=0x10; PORTD=0xBF; _delay_ms(1); PORTC=0x20; PORTD=0xDF; _delay_ms(1); PORTC=0x40; PORTD=0xEF; _delay_ms(1); PORTC=0x80; PORTD=0xF0; _delay_ms(1); }}} Proteus Simulation: LED matrix simulatio...

Digital Timer

Aim: To make a Digital Timer using ATmega16 microcontroller and seven segment displays. Code: #define F_CPU 8000000UL #include<avr/io.h> #include<util/delay.h> int main() { DDRD=0xFF; DDRB=0xFF; DDRC=0xFF; DDRA=0xFF; char ar[10]={0x81,0xF3,0x49,0x61,0x33,0x25,0x05,0xF1,0x01,0x21}; int i,j,k,l,m,n,o,p; for(i=0;i<3;i++) { for(j=0;j<10;j++) { for(k=0;k<6;k++) { for(l=0;l<10;l++) { for(m=0;m<6;m++) { for(n=0;n<10;n++) { for(o=0;o<10;o++){ for(p=0;p<10;p++){ if(j==2 && i==4){ j=0; i=0; } PORTD=0b000101010; PORTB=ar[n]; PORTC=ar[l]; PORTA=ar[j]; _delay_ms(9); PORTD=0b000010101; PORTB=ar[m]; PORTC=ar[k]; PORTA=ar[i]; _delay_ms(1); }}}}}}}}} Hardware information:  PortD= Control seven segments PortB= Seconds PortC= Minutes PortA= Hours  Proteus Simulation: Digital timer simulation  Constraints:  1) To reset, Close the program and the restart. 2) Uses nearly all ports of micro cont...