Tuesday, March 24, 2020

pygame colour filling in python


import pygame, sys
from pygame.locals import *
pygame.init()
displaysurf=pygame.display.set_mode((800,400),0,32)
pygame.display.set_caption('Drawing!')
#setup colour
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
SILVER = (192, 192, 192)
PINK=(255,105,180)

displaysurf.fill(WHITE)
pygame.draw.line(displaysurf, BLUE, [0, 0], [100, 100], 30)
pygame.draw.line(displaysurf, BLUE, [0, 0], [100, 100], 10)

#pygame.draw.line(displaysurf, GREEN, [0, 0], [60, 100], 5)
pygame.draw.line(displaysurf, GREEN, [0, 0], [0, 400], 100)
pygame.draw.line(displaysurf, RED, [100,0], [100, 400], 100)
pygame.draw.line(displaysurf, BLACK, [300,0], [300, 400], 100)
pygame.draw.line(displaysurf, BLUE, [400,0], [400, 400], 100)
pygame.draw.line(displaysurf, GREEN, [500, 0], [500, 400], 100)
pygame.draw.line(displaysurf, PINK, [600,0], [600, 400], 100)
pygame.draw.line(displaysurf, SILVER, [700,0], [700, 400], 100)
pygame.draw.line(displaysurf, BLUE, [800,0], [800, 400], 100)


pixobj=pygame.PixelArray(displaysurf)
pixobj[480][380]=BLACK
pixobj[482][382]=BLACK
pixobj[484][384]=BLACK
pixobj[486][386]=BLACK
pixobj[488][388]=BLACK
del pixobj


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
           
    pygame.display.update()

output:


Monday, March 23, 2020

first pygame in python

import pygame, sys
from pygame.locals import *
pygame.init()
displaysurf=pygame.display.set_mode((400,300))
pygame.display.set_caption('Hello world!!')
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
           
    pygame.display.update()

output:

even or odd using python language

list_of_numbers=[1,2,3,4,5,6]
for number in list_of_numbers:
    if number%2==0:
        print(number,"is even")
    else :
        print(number,"is odd")

print("end of the code")

output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
end of the code

tuple in python

n=input("enter a comma separated number:")
l=n.split(",")
t=tuple(l)
print("l=",l,"t=",t)
print(type(l),"\n",type(t))

output:
enter a comma separated number:2,3,4,5
l= ['2', '3', '4', '5'] t= ('2', '3', '4', '5')
<class 'list'>
 <class 'tuple'>

dictionary in python

n=int(input("enter a number:"))
d=dict()
for i in range(1,n+1):
    #print("i=",i)
    d[i]=i*i
print(d)


output:

enter a number:5
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}


Thursday, March 12, 2020

if and else in python language


x=50
y=50

if x>=0 and x<= 100 and y>=25 and y<=250 :
    print("player is inside the area.sound the alarm")
else:
    print("player is outside  the area. Do  nothing")

output:
player is inside the area.sound the alarm


example2:
israining=True
issunny=True
if israining and issunny:
    print("sun showers")

output:
sun showers

if in python

first=input("First string:")
second=input("second string")
if first>second:
    tup=(first,second)
else:
        tup=(first,second)
print("%s is greater than %s"%tup)

output:
First string:anil

second stringabhi
anil is greater than abhi

How to display scores using python in pygame related


print("%s scored %d and completed %.2f of the quest"%("anil",15,50))

output:
anil scored 15 and completed 50.00 of the quest


example:
print("%20s scored %20d and completed"%("anil",15))

output:
                anil scored                   15 and completed

example3:
print("%-20s%20d"%("anil",15))
output:
anil                                  15

example 4:
print("%-10s%10s%10s\n%-10s%10d%10d\n%-10s%10d%10d"%('Team','Won','Lost','Leafs',1,1,'sabres',0,2))

output:
Team             Won      Lost
Leafs              1         1
sabres             0         2


example 5:
print("%-10s%10s%10s\n%-10s%10d%10d\n%-10s%10d%10d"%('Team','Won','Lost','india',1,1,'srilanka',0,2))

output:
Team             Won      Lost
india                1         1
srilanka           0         2


example 6:
player='abhi'
print(player)

output:
abhi

example7:
formatter="%-10s%10s%10s\n"
header =formatter%("Team","Won","Lost")
india= formatter%("india",1,1)
srikanth= formatter%("srikanth",0,2)

print("%s%s%s"%(header,india,srikanth))
Team             Won      Lost
india              1         1
srikanth           0         2

example 8:
formatter="%-10s%10s%10s\n"
header =formatter%("Team","Won","Lost")
india= formatter%("india",1,1)
srikanth= formatter%("srikanth",0,2)

#print("%s%s%s"%(header,india,srikanth))
table="%s%s%s"%(header,india,srikanth)
print(table)

output:
Team             Won      Lost
india              1         1
srikanth           0         2




Saturday, March 7, 2020

super in Inheritance in python


class a:
    def __init__(self,gene):
        print(gene,"this is initialisation")
        #print(d)
       
    def fn1(self):
        print("this is This is inside fn1")
        #print(d)


class b(a):
    def __init__(self,a):
        print("this is initialisation of b",a)
    def fn2(self,a):
        print(a,"this is This is inside fn2")
        super(). __init__('anil')
       
   
x=5     
buf=b(x)
buf.fn2(x)
print()
x='durgam'
buf=b(x)
buf.fn2(x)

output:
this is initialisation of b 5
5 this is This is inside fn2
anil this is initialisation

this is initialisation of b durgam
durgam this is This is inside fn2
anil this is initialisation

How to write multiple classes and accessing them using python

class a:
    def __init__(self):
        print("this is initialisation")
    def fn1(self):
        print("this is This is inside fn1")


class b:
    def __init__(self):
        print("this is initialisation of b")
    def fn2(self):
        print("this is This is inside fn2")
   
       
buf=a()
buf.fn1()
c=b()
c.fn2()


output:
this is initialisation
this is This is inside fn1
this is initialisation of b
this is This is inside fn2

how to write classes in python

class a:
    def __init__(self):
        print("this is initialisation")
    def fn1(self):
        print("this is This is inside fn1")
       
b=a()
b.fn1()


output:
this is initialisation
this is This is inside fn1

python class topic video