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

GSM message sending system

Aim: To send a message using gsm module from microcontroller to phone. Code: #define F_CPU 8000000UL #define BAUD RATE 9600 #include<avr/io.h> #include<util/delay.h> void lcd_cmd(char value); void lcd_data(char value); void lcd_string(char *str); void lcd_num(unsigned int number); void usart_init(); void usart_transmit(char x); char usart_recieve(); int j=0; void initialize() { lcd_cmd(0x01); //clear LCD lcd_cmd(0x02);    //home position lcd_cmd(0x06);    //L-R //lcd_cmd(0x04); lcd_cmd(0x28); //4 bit mode lcd_cmd(0x0C);    //character generation } void lcd_cmd(char value) { int un,ln; // pc7-pc4 = un ; pc3-pc4 =ln un=value&0xF0; // & is anding function PORTC=0x04|un; _delay_ms(2); PORTC=0x00|un; _delay_ms(2); ln=(value<<4)&0xF0; //<<=left shif by 4 bits _delay_ms(2); PORTC=0x04|ln; _delay_ms(2); PORTC=0x00|ln; } void lcd_data(char value) { ...

Bluetooth Bot

Aim: To control a bot using Bluetooth. Code: #define F_CPU 8000000UL #define BAUD RATE 9600 #include<avr/io.h> #include<util/delay.h> void usart_init(); void usart_transmit(char x); char usart_recieve(); void usart_init(){ UBRRH=0; UBRRL=51; // or 0x33 UCSRB=0x18; UCSRC=0x86; } void usart_transmit(char x){ while((UCSRA&0x20)==0); UDR=x; //data gets stored in UDR } char usart_recieve(){ while((UCSRA&0x80)==0);//Till it gets overflow return UDR; } int main() { DDRC=0xFF; PORTC=0xff; _delay_ms(1000); PORTC=0x00; usart_init(); _delay_ms(2); char a[10]; int i=0; while(1) { if(usart_recieve()=='b'){ if(usart_recieve()=='a'){ if(usart_recieve()=='c'){ if(usart_recieve()=='k'){ if(usart_recieve()=='$'){ PORTC=0b00000101; _delay_ms(100); }}}}} if(usart_recieve()=='r'){ if(usart_recieve()=='i'){ if(usart_recieve()=='g'){ if(usart_recieve()=='h'){ if(usart_recieve()=='t'){ if...

Lighting of a marriage garden

Aim: To design a basic lighting system for a standard marriage garden. Software: Autodesk Revit 2019 Plan of the Garden: Entrance: Stage: Food Area: 3D View of Stage: