├── .gitignore ├── 3 Level Hungry Snake ├── README ├── explosed-sprite.png ├── level1.py ├── level2.py ├── level3.py ├── start.py └── stone.png ├── Hit The Stone ├── geometrybullet.png ├── hitthestone.py ├── plane.gif └── stone.png ├── README ├── boxes.py ├── colorPlay.py ├── detectSpriteCollision.py ├── event.py ├── fireSprite.png ├── hello.py ├── keyinput.py ├── paint.py ├── rotation.py ├── simple.jpg ├── soundPlay.py ├── sprite.py ├── viewColorCodes.py └── yikes.ogg /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.py[cod] 3 | __pycache__/ 4 | 5 | -------------------------------------------------------------------------------- /3 Level Hungry Snake/README: -------------------------------------------------------------------------------- 1 | Important Notes 2 | ------------------- 3 | 4 | 1. Please keep all the files in a single folder only 5 | 2. This is a cross platform game 6 | 3. Start the game by running start.py.Don't run any other file. 7 | 4. Make Changes At Your Own Risk (Programmer's Hobby) 8 | 5. In case you find out any bug please contact me at : 9 | coolankur2006@gmail.com 10 | ankur.aggarwal2390@gmail.com 11 | www.twitter.com/ankurtwi 12 | -------------------------------------------------------------------------------- /3 Level Hungry Snake/explosed-sprite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankur0890/Pygame-Examples-For-Learning/34dbbb89bbc208733bbb220a2033ffde3516496a/3 Level Hungry Snake/explosed-sprite.png -------------------------------------------------------------------------------- /3 Level Hungry Snake/level1.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # Purpose : A very small,basic and my first game 4 | # Usages : Learning purpose 5 | # Start date : 12/04/2011 6 | # End date : 2/05/2011 7 | # Author : Ankur Aggarwal 8 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 9 | # Version : 0.0.2 10 | # Modification history : level1-Snake passage through the border 11 | ############################################################################ 12 | 13 | 14 | 15 | import pygame 16 | from pygame.locals import * 17 | from sys import exit 18 | from random import randint 19 | 20 | pygame.init() 21 | 22 | counter=0 23 | 24 | def main(): 25 | while True: 26 | b=[] 27 | #update function used for incrementing the counter 28 | def update(): 29 | global counter 30 | counter=(counter+1)%7 31 | # blast function used for creating the blast through sprites on collision 32 | def blast(w,h): 33 | image=pygame.image.load("explosed-sprite.png").convert_alpha() 34 | width,height=image.get_size() 35 | for i in xrange(int(width/w)): 36 | b.append(image.subsurface((i*w,0,w,h))) 37 | #print a 38 | up=1 39 | down=2 40 | right=3 41 | left=4 42 | step=20 43 | block=[20,20] 44 | x=randint(1,20) 45 | y=randint(2,22) 46 | applexy=[] 47 | snakexy=[int(x*20),int(y*20)] 48 | snakelist=[[x*20,y*20],[(x-20)*20,(y*20)]] 49 | apple=0 50 | dead=0 51 | grow=0 52 | direction=right 53 | score=0 54 | start=0 55 | screen=pygame.display.set_mode((640,480),0,24) 56 | clock=pygame.time.Clock() 57 | 58 | #game loop 59 | while not dead: 60 | pressed=pygame.key.get_pressed() 61 | for i in pygame.event.get(): 62 | if i.type==QUIT or pressed[K_q]: 63 | exit() 64 | if pressed[K_LEFT] and direction!=right: 65 | direction=left 66 | elif pressed[K_RIGHT] and direction!=left: 67 | direction=right 68 | elif pressed[K_UP] and direction!=down: 69 | direction=up 70 | elif pressed[K_DOWN] and direction!=up: 71 | direction=down 72 | if direction==right: 73 | snakexy[0]=snakexy[0]+step 74 | if snakexy[0]>=640: 75 | snakexy[0]=0 76 | 77 | elif direction==left: 78 | snakexy[0]=snakexy[0]-step 79 | if snakexy[0]<0: 80 | snakexy[0]=620 81 | 82 | elif direction==up: 83 | snakexy[1]=snakexy[1]-step 84 | if snakexy[1]<0: 85 | snakexy[1]=460 86 | elif direction==down: 87 | snakexy[1]=snakexy[1]+step 88 | if snakexy[1]>=480: 89 | snakexy[1]=0 90 | 91 | if snakelist.count(snakexy)>0: 92 | dead=1 93 | if apple==0: 94 | x1=randint(1,31) 95 | y1=randint(2,22) 96 | applexy=[int(x1*step),int(y1*step)] 97 | apple=1 98 | 99 | snakelist.insert(0,list(snakexy)) 100 | if snakexy[0]==applexy[0] and snakexy[1]==applexy[1]: 101 | apple=0 102 | score=score+1 103 | else: 104 | snakelist.pop() 105 | #display on the screen 106 | screen.fill((0,0,0)) 107 | scr=pygame.font.SysFont("comicsansms",20) 108 | text4=scr.render("Score : %d"%score,True,(0,255,0)) 109 | screen.blit(text4,(500,10)) 110 | pygame.draw.rect(screen,(255,0,0),Rect(applexy,block),0) 111 | for i in snakelist: 112 | pygame.draw.rect(screen,(0,255,0),Rect(i,block)) 113 | pygame.display.flip() 114 | clock.tick(15) 115 | 116 | 117 | if dead==1: 118 | blast(20,20) 119 | for i in xrange(7): 120 | screen.blit(b[counter],(snakexy[0],snakexy[1])) 121 | update() 122 | pygame.display.update() 123 | clock.tick(10) 124 | #game over 125 | screen.fill((0,0,0)) 126 | over=pygame.font.SysFont("comicsansms",40) 127 | text5=over.render("GAME OVER",True,(0,255,0)) 128 | s1=pygame.font.SysFont("comicsansms",30) 129 | s2=pygame.font.SysFont("comicsansms",30) 130 | screen.blit(text5,(50,50)) 131 | screen.blit(text4,(200,200)) 132 | screen.blit(s1.render("Press s To Play Again",True,(0,255,0)),(50,250)) 133 | screen.blit(s2.render("Press l For Level Selection ",True,(0,255,0)),(50,300)) 134 | pygame.display.flip() 135 | while True: 136 | for i in pygame.event.get(): 137 | if i.type==QUIT: 138 | exit() 139 | pressed=pygame.key.get_pressed() 140 | if pressed[K_q]: 141 | exit() 142 | if pressed[K_s]: 143 | main() 144 | if pressed[K_l]: 145 | break 146 | break 147 | 148 | 149 | 150 | if __name__=='__main__': 151 | main() 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /3 Level Hungry Snake/level2.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # Purpose : A very small,basic and my first game 4 | # Usages : Learning purpose 5 | # Start date : 12/04/2011 6 | # End date : 2/05/2011 7 | # Author : Ankur Aggarwal 8 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 9 | # Version : 0.0.2 10 | # Modification history : Level2-add the sprite image + border collision 11 | ############################################################################ 12 | 13 | 14 | 15 | import pygame 16 | from pygame.locals import * 17 | from sys import exit 18 | from random import randint 19 | 20 | counter=0 21 | def main(): 22 | while True: 23 | b=[] 24 | def update(): 25 | global counter 26 | counter=(counter+1)%7 27 | def blast(w,h): 28 | image=pygame.image.load("explosed-sprite.png").convert_alpha() 29 | width,height=image.get_size() 30 | for i in xrange(int(width/w)): 31 | b.append(image.subsurface((i*w,0,w,h))) 32 | 33 | up=1 34 | down=2 35 | right=3 36 | left=4 37 | step=20 38 | block=[20,20] 39 | x=randint(1,20) 40 | y=randint(2,22) 41 | applexy=[] 42 | snakexy=[int(x*20),int(y*20)] 43 | snakelist=[[x*20,y*20],[(x-20)*20,(y*20)]] 44 | apple=0 45 | dead=0 46 | grow=0 47 | direction=right 48 | score=0 49 | start=0 50 | screen=pygame.display.set_mode((640,480),0,24) 51 | clock=pygame.time.Clock() 52 | 53 | #game loop 54 | while not dead: 55 | pressed=pygame.key.get_pressed() 56 | for i in pygame.event.get(): 57 | if i.type==QUIT or pressed[K_q]: 58 | exit() 59 | if pressed[K_LEFT] and direction!=right: 60 | direction=left 61 | elif pressed[K_RIGHT] and direction!=left: 62 | direction=right 63 | elif pressed[K_UP] and direction!=down: 64 | direction=up 65 | elif pressed[K_DOWN] and direction!=up: 66 | direction=down 67 | if direction==right: 68 | snakexy[0]=snakexy[0]+step 69 | if snakexy[0]>=620: 70 | dead=1 71 | 72 | #snake movement 73 | elif direction==left: 74 | snakexy[0]=snakexy[0]-step 75 | if snakexy[0]<20: 76 | dead=1 77 | 78 | elif direction==up: 79 | snakexy[1]=snakexy[1]-step 80 | if snakexy[1]<20: 81 | dead=1 82 | elif direction==down: 83 | snakexy[1]=snakexy[1]+step 84 | if snakexy[1]>=460: 85 | dead=1 86 | if snakelist.count(snakexy)>0: 87 | dead=1 88 | if apple==0: 89 | x1=randint(2,30) 90 | y1=randint(2,22) 91 | applexy=[int(x1*step),int(y1*step)] 92 | apple=1 93 | 94 | snakelist.insert(0,list(snakexy)) 95 | if snakexy[0]==applexy[0] and snakexy[1]==applexy[1]: 96 | apple=0 97 | score=score+1 98 | else: 99 | snakelist.pop() 100 | 101 | #display on the screen 102 | 103 | screen.fill((0,0,0)) 104 | scr=pygame.font.SysFont("comicsansms",20) 105 | text4=scr.render("Score : %d"%score,True,(0,255,0)) 106 | screen.blit(text4,(500,10)) 107 | pygame.draw.line(screen,(0,255,0),(0,0),(0,480),20) 108 | pygame.draw.line(screen,(0,255,0),(0,0),(640,0),20) 109 | pygame.draw.line(screen,(0,255,0),(640,0),(640,480),20) 110 | pygame.draw.line(screen,(0,255,0),(0,480),(640,480),20) 111 | pygame.draw.rect(screen,(255,0,0),Rect(applexy,block),0) 112 | for i in snakelist: 113 | pygame.draw.rect(screen,(0,255,0),Rect(i,block)) 114 | pygame.display.flip() 115 | clock.tick(15) 116 | 117 | if dead==1: 118 | blast(20,20) 119 | for i in xrange(7): 120 | screen.blit(b[counter],(snakexy[0],snakexy[1])) 121 | update() 122 | pygame.display.update() 123 | clock.tick(10) 124 | # game over screen 125 | screen.fill((0,0,0)) 126 | over=pygame.font.SysFont("comicsansms",40) 127 | text5=over.render("GAME OVER",True,(0,255,0)) 128 | s1=pygame.font.SysFont("comicsansms",30) 129 | s2=pygame.font.SysFont("comicsansms",30) 130 | screen.blit(text5,(50,50)) 131 | screen.blit(text4,(200,200)) 132 | screen.blit(s1.render("Press s To Play Again",True,(0,255,0)),(50,250)) 133 | screen.blit(s2.render("Press l For Level Selection ",True,(0,255,0)),(50,300)) 134 | 135 | pygame.display.flip() 136 | while True: 137 | for i in pygame.event.get(): 138 | if i.type==QUIT: 139 | exit() 140 | pressed=pygame.key.get_pressed() 141 | if pressed[K_q]: 142 | exit() 143 | if pressed[K_s]: 144 | main() 145 | if pressed[K_l]: 146 | break 147 | break 148 | 149 | 150 | 151 | 152 | if __name__=='__main__': 153 | main() 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /3 Level Hungry Snake/level3.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # Purpose : A very small,basic and my first game 4 | # Usages : Learning purpose 5 | # Start date : 12/04/2011 6 | # End date : 2/05/2011 7 | # Author : Ankur Aggarwal 8 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 9 | # Version : 0.0.2 10 | # Modification history : Level3 - Stone Fall 11 | ############################################################################ 12 | 13 | 14 | 15 | import pygame 16 | from pygame.locals import * 17 | from sys import exit 18 | from random import randint 19 | 20 | counter=0 21 | def main(): 22 | while True: 23 | b=[] 24 | def update(): 25 | global counter 26 | counter=(counter+1)%7 27 | def blast(w,h): 28 | image=pygame.image.load("explosed-sprite.png").convert_alpha() 29 | width,height=image.get_size() 30 | for i in xrange(int(width/w)): 31 | b.append(image.subsurface((i*w,0,w,h))) 32 | #print a 33 | up=1 34 | down=2 35 | right=3 36 | left=4 37 | step=20 38 | block=[20,20] 39 | x=randint(1,20) 40 | y=randint(2,22) 41 | applexy=[] 42 | x3=randint(1,25) 43 | check=[x3*20,-20] 44 | snakexy=[int(x*20),int(y*20)] 45 | snakelist=[[x*20,y*20],[(x-20)*20,(y*20)]] 46 | apple=0 47 | dead=0 48 | grow=0 49 | direction=right 50 | score=0 51 | start=0 52 | draw=[] 53 | screen=pygame.display.set_mode((640,480),0,24) 54 | clock=pygame.time.Clock() 55 | 56 | #game loop 57 | while not dead: 58 | pressed=pygame.key.get_pressed() 59 | for i in pygame.event.get(): 60 | if i.type==QUIT or pressed[K_q]: 61 | exit() 62 | image2=pygame.image.load("stone.png").convert_alpha() 63 | pygame.transform.scale(image2,(20,20)) 64 | check[1]=check[1]+step 65 | if check[1]>480: 66 | x2=randint(1,25) 67 | check[0]=x2*20 68 | check[1]=0 69 | 70 | if pressed[K_LEFT] and direction!=right: 71 | direction=left 72 | elif pressed[K_RIGHT] and direction!=left: 73 | direction=right 74 | elif pressed[K_UP] and direction!=down: 75 | direction=up 76 | elif pressed[K_DOWN] and direction!=up: 77 | direction=down 78 | if direction==right: 79 | snakexy[0]=snakexy[0]+step 80 | if snakexy[0]>=640: 81 | snakexy[0]=0 82 | 83 | elif direction==left: 84 | snakexy[0]=snakexy[0]-step 85 | if snakexy[0]<0: 86 | snakexy[0]=620 87 | 88 | elif direction==up: 89 | snakexy[1]=snakexy[1]-step 90 | if snakexy[1]<0: 91 | snakexy[1]=460 92 | elif direction==down: 93 | snakexy[1]=snakexy[1]+step 94 | if snakexy[1]>=480: 95 | snakexy[1]=0 96 | 97 | if snakelist.count(snakexy)>0: 98 | for i in snakelist: 99 | if i==snakexy: 100 | draw.append(i) 101 | break 102 | dead=1 103 | if apple==0: 104 | x1=randint(1,31) 105 | y1=randint(2,22) 106 | applexy=[int(x1*step),int(y1*step)] 107 | apple=1 108 | 109 | snakelist.insert(0,list(snakexy)) 110 | if snakexy[0]==applexy[0] and snakexy[1]==applexy[1]: 111 | apple=0 112 | score=score+1 113 | else: 114 | snakelist.pop() 115 | 116 | if snakelist.count(check)>0: 117 | for i in snakelist: 118 | if i==check: 119 | draw.append(i) 120 | break 121 | dead=1 122 | 123 | #display on the screen 124 | screen.fill((0,0,0)) 125 | scr=pygame.font.SysFont("comicsansms",20) 126 | text4=scr.render("Score : %d"%score,True,(0,255,0)) 127 | screen.blit(text4,(500,10)) 128 | screen.blit(image2,check) 129 | pygame.draw.rect(screen,(255,0,0),Rect(applexy,block),0) 130 | for i in snakelist: 131 | pygame.draw.rect(screen,(0,255,0),Rect(i,block)) 132 | pygame.display.flip() 133 | clock.tick(15) 134 | 135 | if dead==1: 136 | blast(20,20) 137 | for i in xrange(7): 138 | screen.blit(b[counter],(tuple(draw))) 139 | update() 140 | pygame.display.update() 141 | clock.tick(10) 142 | 143 | # Game over display 144 | 145 | screen.fill((0,0,0)) 146 | over=pygame.font.SysFont("comicsansms",40) 147 | s1=pygame.font.SysFont("comicsansms",30) 148 | s2=pygame.font.SysFont("comicsansms",30) 149 | text5=over.render("GAME OVER",True,(0,255,0)) 150 | screen.blit(text5,(50,50)) 151 | screen.blit(text4,(200,200)) 152 | screen.blit(s1.render("Press s To Play Again",True,(0,255,0)),(50,250)) 153 | screen.blit(s2.render("Press l For Level Selection ",True,(0,255,0)),(50,300)) 154 | 155 | pygame.display.flip() 156 | while True: 157 | for i in pygame.event.get(): 158 | if i.type==QUIT: 159 | exit() 160 | pressed=pygame.key.get_pressed() 161 | if pressed[K_q]: 162 | exit() 163 | if pressed[K_s]: 164 | main() 165 | if pressed[K_l]: 166 | break 167 | break 168 | 169 | 170 | 171 | if __name__=='__main__': 172 | main() 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | -------------------------------------------------------------------------------- /3 Level Hungry Snake/start.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # Purpose : A very small,basic and my first game 4 | # Usages : Learning purpose 5 | # Start date : 12/04/2011 6 | # End date : 2/05/2011 7 | # Author : Ankur Aggarwal 8 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 9 | # Version : 0.0.2 10 | # Modification history : Start of the game takes place by running this script 11 | ############################################################################ 12 | 13 | 14 | import pygame 15 | from pygame.locals import * 16 | from sys import exit 17 | from random import randint 18 | import level1 19 | import level2 20 | import level3 21 | 22 | 23 | # making out the starting screen 24 | 25 | while True: 26 | pygame.init() 27 | screen=pygame.display.set_mode((640,480),0,24) 28 | caption=pygame.display.set_caption("Hungry Snake") 29 | f=pygame.font.SysFont("comicsansms",50) 30 | text1=f.render("Hungry Snake",True,(0,255,0)) 31 | clock=pygame.time.Clock() 32 | start=pygame.font.SysFont("comicsansms",30) 33 | text2=start.render("Press s to start",True,(0,255,0)) 34 | q=pygame.font.SysFont("comicsansms",30) 35 | text3=q.render("Press q to quit",True,(0,255,0)) 36 | s=[[300,200],[280,200],[260,200],[240,200],[220,200],[200,200],[180,200],[180,220],[160,220],[140,220],[120,220],[120,240],[100,240]] 37 | a=[100,240] 38 | pygame.draw.rect(screen,(255,0,0),Rect(a,[20,20]),0) 39 | screen.blit(text1,(60,60)) 40 | screen.blit(text2,(300,300)) 41 | screen.blit(text3,(300,350)) 42 | for i in s: 43 | pygame.draw.rect(screen,(0,255,0),Rect(i,[20,20]),0) 44 | pygame.display.flip() 45 | clock.tick(10) 46 | pygame.display.flip() 47 | 48 | #event handling (Key events) 49 | while True: 50 | for i in pygame.event.get(): 51 | if i.type==QUIT: 52 | exit() 53 | pressed=pygame.key.get_pressed() 54 | if pressed[K_q]: 55 | exit() 56 | if pressed[K_s]: 57 | break 58 | break 59 | 60 | # Level screen 61 | while True: 62 | press=pygame.key.get_pressed() 63 | for i in pygame.event.get(): 64 | if i.type==QUIT or press[K_q]: 65 | exit() 66 | screen.fill((0,0,0)) 67 | mousepress=pygame.mouse.get_pressed() 68 | l1=pygame.font.SysFont("comicsansms",50) 69 | l2=pygame.font.SysFont("comicsansms",30) 70 | l3=pygame.font.SysFont("comicsansms",30) 71 | l4=pygame.font.SysFont("comicsansms",30) 72 | r2=Rect((100,200),l2.size("Press 1 for Level 1 ")) 73 | r3=Rect((100,250),l3.size("Press 2 for Level 2 ")) 74 | r4=Rect((100,300),l3.size("Press 3 for Level 3 ")) 75 | screen.blit(l1.render("Select Your Level",True,(0,255,0)),(100,100)) 76 | screen.blit(l2.render("Press 1 for Level 1 ",True,(0,255,0)),(100,200)) 77 | screen.blit(l3.render("Press 2 for level 2 ",True,(0,255,0)),(100,250)) 78 | screen.blit(l4.render("Press 3 for level 3 ",True,(0,255,0)),(100,300)) 79 | pygame.display.update() 80 | 81 | if press[K_1] or (r2.collidepoint(pygame.mouse.get_pos()) and mousepress[0]): 82 | level1.main() 83 | if press[K_2] or (r3.collidepoint(pygame.mouse.get_pos()) and mousepress[0]): 84 | level2.main() 85 | if press[K_3] or (r4.collidepoint(pygame.mouse.get_pos()) and mousepress[0]): 86 | level3.main() 87 | 88 | -------------------------------------------------------------------------------- /3 Level Hungry Snake/stone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankur0890/Pygame-Examples-For-Learning/34dbbb89bbc208733bbb220a2033ffde3516496a/3 Level Hungry Snake/stone.png -------------------------------------------------------------------------------- /Hit The Stone/geometrybullet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankur0890/Pygame-Examples-For-Learning/34dbbb89bbc208733bbb220a2033ffde3516496a/Hit The Stone/geometrybullet.png -------------------------------------------------------------------------------- /Hit The Stone/hitthestone.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | 3 | ############################################################################ 4 | # File name : hitthestone.py 5 | # Purpose : A demo game that destroys the falling stones on shooting 6 | # Start date : 16/01/2012 7 | # End date : In Progress (Stand By Due To Time Limitation) 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | ############################################################################ 11 | 12 | #Important : This game is incomplete. It just shoots the bullets when the spacebar is pressed. 13 | # Due to time limitation I am unable to finish this game.I thought this as a demo game 14 | # tutorial for pygame newbies like my other game 'Hungry Snake' 15 | # My intentions was to shoot randomly falling stones with the bullets (something game like that). 16 | # I have already created the stone class but struggled with the sprite group management. 17 | # Leaving this game in between because going somewhere for about 4 months 18 | # where laptops are not allowed. 19 | # I would be greatful to the person who completes this for learning purpose. 20 | # Contact me at ankur.aggarwal2390@gmail.com 21 | 22 | 23 | import pygame 24 | from pygame.locals import * 25 | import random 26 | import time 27 | 28 | pygame.init() 29 | screen=pygame.display.set_mode([640, 480],0,24) 30 | pygame.display.set_caption("Hit The Stone") 31 | background=pygame.Surface(screen.get_size()) 32 | background=background.convert() 33 | screen.blit(background,(0,0)) 34 | 35 | class Plane(pygame.sprite.Sprite): 36 | def __init__(self, image=None): 37 | pygame.sprite.Sprite.__init__(self, self.containers) 38 | self.image = image or Plane._default_image 39 | self.cooldown=15 40 | self.rect=self.image.get_rect() 41 | self.rect.centerx = screen.get_width() / 2 42 | self.distancefromcenter=30 43 | self.rect.centery=screen.get_height()-self.distancefromcenter-40 44 | self.dx=3 45 | self.dy=3 46 | 47 | def update(self): 48 | self.pressed=pygame.key.get_pressed() 49 | if self.pressed[K_DOWN]: 50 | self.rect.centery+=self.dy 51 | elif self.pressed[K_UP]: 52 | self.rect.centery-=self.dy 53 | if self.pressed[K_LEFT]: 54 | self.rect.centerx-=self.dx 55 | elif self.pressed[K_RIGHT]: 56 | self.rect.centerx+=self.dx 57 | 58 | if self.rect.bottom>=screen.get_height(): 59 | self.rect.bottom=screen.get_height() 60 | elif self.rect.top<=0: 61 | self.rect.top=0 62 | 63 | if self.rect.centerx>=screen.get_width()-self.distancefromcenter: 64 | self.rect.centerx=screen.get_width()-self.distancefromcenter 65 | elif self.rect.centerx<=self.distancefromcenter: 66 | self.rect.centerx=self.distancefromcenter 67 | 68 | self.cooldown = max(0, self.cooldown-1) 69 | 70 | 71 | class Stone(pygame.sprite.Sprite): 72 | def __init__(self, image=None): 73 | pygame.sprite.Sprite.__init__(self, self.containers) 74 | self.image=image or Stone._default_image 75 | self.rect=self.image.get_rect() 76 | self.rect.centerx=random.randint(5,630) 77 | self.rect.centery=0 78 | self.dy=4 79 | 80 | def update(self): 81 | self.rect.centery+=self.dy 82 | if self.rect.bottom>=screen.get_height(): 83 | self.rect.centerx=random.randint(5,630) 84 | self.rect.centery=0 85 | 86 | 87 | class Bullet(pygame.sprite.Sprite): 88 | 89 | def __init__(self, posx, posy, image=None): 90 | pygame.sprite.Sprite.__init__(self, self.containers) 91 | 92 | self.image = image or Bullet._default_image 93 | self.rect=self.image.get_rect() 94 | self.rect.center=(posx,posy-30) 95 | self.dy=5 96 | 97 | 98 | def update(self): 99 | self.rect.centery-=self.dy 100 | self.rect.center=(self.rect.centerx,self.rect.centery) 101 | if self.rect.top<=0: 102 | self.kill() 103 | 104 | class Score(pygame.sprite.Sprite): 105 | 106 | def __init__(self): 107 | pygame.sprite.Sprite.__init__(self) 108 | self.font = pygame.font.Font(None, 20) 109 | self.font.set_italic(1) 110 | self.color = Color('white') 111 | self.lastscore = -1 112 | self.update() 113 | self.rect = self.image.get_rect().move(10, 450) 114 | 115 | def update(self): 116 | if SCORE != self.lastscore: 117 | self.lastscore = SCORE 118 | msg = 'SCORE: {}'.format(SCORE) 119 | self.image = self.font.render(msg, 0, self.color) 120 | 121 | 122 | def main(): 123 | allSprites=pygame.sprite.Group() 124 | bullets = pygame.sprite.Group() 125 | stones = pygame.sprite.Group() 126 | 127 | Plane.containers = allSprites 128 | Stone.containers = allSprites, stones 129 | Bullet.containers = allSprites, bullets 130 | 131 | Plane._default_image = pygame.image.load('plane.gif').convert() 132 | Bullet._default_image = pygame.image.load('geometrybullet.png').convert() 133 | Stone._default_image = pygame.image.load('stone.png').convert_alpha() 134 | 135 | clock=pygame.time.Clock() 136 | 137 | max_stones = 10; 138 | stone_spawn_delay = 40; 139 | stone_spawn_cooldown = 0; 140 | 141 | global SCORE 142 | SCORE = 0 143 | allSprites.add(Score()) 144 | 145 | plane=Plane() 146 | 147 | while True: 148 | pressed=pygame.key.get_pressed() 149 | for i in pygame.event.get(): 150 | if i.type==QUIT or pressed[K_q]: 151 | exit() 152 | if pressed[K_SPACE] and plane.cooldown == 0: 153 | Bullet(plane.rect.centerx,plane.rect.centery) 154 | plane.cooldown=15 155 | 156 | for stone in stones: 157 | for bullet in bullets: 158 | if bullet.rect.colliderect(stone): 159 | bullet.kill() 160 | stone.kill() 161 | SCORE += 500 162 | if stone.rect.colliderect(plane): 163 | plane.kill() 164 | stone.kill() 165 | 166 | stone_spawn_cooldown -= 1 167 | if len(stones) < max_stones and stone_spawn_cooldown <= 0: 168 | Stone() 169 | stone_spawn_cooldown = stone_spawn_delay 170 | 171 | allSprites.clear(screen, background) 172 | allSprites.update() 173 | allSprites.draw(screen) 174 | 175 | pygame.display.flip() 176 | clock.tick(60) 177 | 178 | 179 | if __name__=='__main__': 180 | main() 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /Hit The Stone/plane.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankur0890/Pygame-Examples-For-Learning/34dbbb89bbc208733bbb220a2033ffde3516496a/Hit The Stone/plane.gif -------------------------------------------------------------------------------- /Hit The Stone/stone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankur0890/Pygame-Examples-For-Learning/34dbbb89bbc208733bbb220a2033ffde3516496a/Hit The Stone/stone.png -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This repository contains a list of examples through which you can excel in pygame module. You should try these examples using your own logic. Play with them as much as you can, that will help you to explore more. -------------------------------------------------------------------------------- /boxes.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name :boxes.py 4 | # Purpose : Demo Displaying Random Color Squares Usng Sprite Class 5 | # Usages : Learning Purpose 6 | # Start date : 03/01/2012 7 | # End date : 03/01/2012 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # Reference : Game Programming By Andy Harris 11 | # How To Run : python boxes.py 12 | ############################################################################ 13 | 14 | #importng necessary modules 15 | import pygame 16 | from pygame.locals import * 17 | from random import randint 18 | 19 | 20 | pygame.init() 21 | screen=pygame.display.set_mode((640,480),0,24) 22 | pygame.display.set_caption("Boxes All The Way") 23 | 24 | 25 | #inheritance of Sprite class.every Sprite Must Have image and rect property otherwise it wll give you an exception 26 | class Boxes(pygame.sprite.Sprite): 27 | def __init__(self,color): 28 | pygame.sprite.Sprite.__init__(self) 29 | self.image=pygame.Surface((50,50)) 30 | self.image.fill(color) 31 | self.rect=self.image.get_rect() 32 | self.rect.center=(randint(0,640),randint(0,480)) 33 | 34 | 35 | def main(): 36 | background=pygame.Surface(screen.get_size()) 37 | background=background.convert() 38 | background.fill((0,0,0)) 39 | screen.blit(background,(0,0)) 40 | boxes=[] 41 | 42 | #traversng through every color. see 'pydoc pygame.color.THECOLORS' for more details 43 | for colorName in pygame.color.THECOLORS: 44 | boxes.append(Boxes(pygame.color.Color(colorName))) 45 | 46 | allSprites=pygame.sprite.Group(boxes) 47 | 48 | while 1: 49 | for i in pygame.event.get(): 50 | if i.type==QUIT: 51 | exit() 52 | 53 | #following the CUD Rule (Clear,Update,Draw) 54 | allSprites.clear(screen,background) 55 | allSprites.update() 56 | allSprites.draw(screen) 57 | pygame.display.flip() 58 | 59 | 60 | if __name__=='__main__': 61 | main() 62 | 63 | -------------------------------------------------------------------------------- /colorPlay.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name : ../Pygame-Examples-For-Learning/colorPlay.py 4 | # Purpose : A Small Demo Practice Prgram using Pygame API 5 | # Usages : You can play with colors by changing there Intensites 6 | # Start date : 14/12/2011 7 | # End date : 14/12/2011 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # How To Run: python colorPlay.py 11 | ############################################################################ 12 | 13 | #import all the necessary modules 14 | import pygame 15 | from pygame.locals import * 16 | 17 | 18 | #initializations 19 | pygame.init() 20 | screen=pygame.display.set_mode((640,480)) 21 | pygame.display.set_caption("Color Play") 22 | font=pygame.font.SysFont('arial',10) 23 | redFont=font.render('Red',True,(0,0,0)) 24 | greenFont=font.render("Green",True,(0,0,0)) 25 | blueFont=font.render("Blue",True,(0,0,0)) 26 | background=pygame.Surface(screen.get_size()) 27 | background=background.convert() 28 | red=1 29 | green=2 30 | blue=3 31 | redX=75 32 | redY=125 33 | greenX=175 34 | greenY=125 35 | blueX=275 36 | blueY=125 37 | clock=pygame.time.Clock() 38 | currentColor=red 39 | 40 | #Game Loop 41 | while 1: 42 | pressed=pygame.key.get_pressed() 43 | for i in pygame.event.get(): 44 | if i.type==QUIT or pressed[K_q]: 45 | exit() 46 | 47 | 48 | if pressed[K_DOWN] and currentColor==red: 49 | redY=redY+1 50 | elif pressed[K_UP] and currentColor==red: 51 | redY=redY-1 52 | 53 | if pressed[K_DOWN] and currentColor==green: 54 | greenY=greenY+1 55 | elif pressed[K_UP] and currentColor==green: 56 | greenY=greenY-1 57 | 58 | if pressed[K_DOWN] and currentColor==blue: 59 | blueY=blueY+1 60 | elif pressed[K_UP] and currentColor==blue: 61 | blueY=blueY-1 62 | 63 | 64 | 65 | redIntensity=redY-100 66 | greenIntensity=greenY-100 67 | blueIntensity=blueY-100 68 | 69 | if redIntensity<0: 70 | redIntensity=0 71 | redY=100 72 | elif redIntensity>255: 73 | redIntensity=255 74 | redY=355 75 | if greenIntensity<0: 76 | greenIntensity=0 77 | greenY=100 78 | elif greenIntensity>255: 79 | greenIntensity=255 80 | greenY=355 81 | 82 | if blueIntensity<0: 83 | blueIntensity=0 84 | blueY=100 85 | elif blueIntensity>255: 86 | blueIntensity=255 87 | blueY=356 88 | 89 | background.fill((redIntensity,greenIntensity,blueIntensity)) 90 | 91 | screen.fill((0,0,0)) 92 | screen.blit(background,(0,0)) 93 | pygame.draw.line(screen,(255,0,0),(100,100),(100,356),1) 94 | pygame.draw.line(screen,(0,255,0),(200,100),(200,356),1) 95 | pygame.draw.line(screen,(0,0,255),(300,100),(300,356),1) 96 | pygame.draw.rect(screen,(255,0,0),((redX,redY),(50,50)),0) 97 | pygame.draw.rect(screen,(0,255,0),((greenX,greenY),(50,50)),0) 98 | pygame.draw.rect(screen,(0,0,255),((blueX,blueY),(50,50)),0) 99 | screen.blit(redFont,(redX+10,redY+10)) 100 | screen.blit(greenFont,(greenX+10,greenY+10)) 101 | screen.blit(blueFont,(blueX+10,blueY+10)) 102 | screen.blit(font.render("%i"%redIntensity,True,(0,0,0)),(redX+10,redY+30)) 103 | screen.blit(font.render("%i"%greenIntensity,True,(0,0,0)),(greenX+10,greenY+30)) 104 | screen.blit(font.render("%i"%blueIntensity,True,(0,0,0)),(blueX+10,blueY+30)) 105 | if pressed[K_RIGHT] or pressed[K_TAB]: 106 | if currentColor==red: 107 | currentColor=green 108 | screen.blit(greenFont,(200,60)) 109 | elif currentColor==green or pressed[K_TAB]: 110 | currentColor=blue 111 | screen.blit(blueFont,(300,60)) 112 | elif currentColor==blue or pressed[K_TAB]: 113 | currentColor=red 114 | screen.blit(redFont,(100,60)) 115 | 116 | if pressed[K_LEFT]: 117 | if currentColor==red: 118 | currentColor=blue 119 | screen.blit(blueFont,(300,60)) 120 | elif currentColor==green: 121 | currentColor=red 122 | screen.blit(redFont,(100,60)) 123 | elif currentColor==blue: 124 | currentColor=green 125 | screen.blit(greenFont,(200,60)) 126 | clock.tick(15) 127 | pygame.display.flip() 128 | 129 | -------------------------------------------------------------------------------- /detectSpriteCollision.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name : detectSpriteCollsion.py 4 | # Purpose : Demostarting The Killing of Sprite On Collision 5 | # Usages : Logic can be used in real time games 6 | # Start date : 04/01/2012 7 | # End date : 04/01/2012 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # How To Run: python detectSpriteCollision.py 11 | ############################################################################ 12 | 13 | import pygame 14 | from pygame.locals import * 15 | import random 16 | 17 | screen=pygame.display.set_mode((640,480),0,32) 18 | pygame.display.set_caption("Collision Detection") 19 | 20 | 21 | #creating the boxes 22 | class Boxes(pygame.sprite.Sprite): 23 | def __init__(self): 24 | pygame.sprite.Sprite.__init__(self) 25 | self.image=pygame.Surface((50,50)) 26 | self.image.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255))) 27 | self.rect=self.image.get_rect() 28 | self.rect.center=(random.randint(0,255),random.randint(0,255)) 29 | 30 | #creating circle 31 | class Circle(pygame.sprite.Sprite): 32 | def __init__(self): 33 | pygame.sprite.Sprite.__init__(self) 34 | self.image=pygame.Surface((50,50)) 35 | self.image.fill((0,255,0)) 36 | pygame.draw.circle(self.image,(255,0,0),(25,25),25,0) 37 | self.rect=self.image.get_rect() 38 | def update(self): 39 | self.rect.center=pygame.mouse.get_pos() 40 | 41 | 42 | def main(): 43 | background=pygame.Surface(screen.get_size()) 44 | background=background.convert() 45 | background.fill((0,255,0)) 46 | screen.blit(background,(0,0)) 47 | 48 | boxes=[] 49 | for i in xrange(0,10): 50 | boxes.append(Boxes()) 51 | 52 | circle=Circle() 53 | allSprites=pygame.sprite.Group(boxes) 54 | circleSprite=pygame.sprite.Group(circle) 55 | while 1: 56 | for i in pygame.event.get(): 57 | if i.type==QUIT: 58 | exit() 59 | 60 | 61 | #checking the collision.check 'pydoc pygame.sprite.spritecollide' for mode details. True is used for sprite killing. It doesn't kill the sprite in actual.It is still present in the computer memory though.It has just removed it from the group so that no further display of that sprite is possible. 62 | if pygame.sprite.spritecollide(circle,allSprites,True): 63 | print "collision" 64 | 65 | #following the CUD method 66 | allSprites.clear(screen,background) 67 | circleSprite.clear(screen,background) 68 | allSprites.update() 69 | circleSprite.update() 70 | allSprites.draw(screen) 71 | circleSprite.draw(screen) 72 | pygame.display.flip() 73 | 74 | 75 | if __name__=='__main__': 76 | main() 77 | 78 | """You can also check the collision about the rect attributes. There are many ways to do that.Example: 79 | 1.circle.rect.colliderect(box1) will check the collision between the circle and box1 collision 80 | 2. pygame.sprite.collide_rect(sprite1,sprite2) willl also do the same """ 81 | 82 | -------------------------------------------------------------------------------- /event.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name :event.py 4 | # Purpose : A Small Demo Practice Prgram using Pygame API 5 | # Usages : Tells About The Events And Related Parameters. Useful For Event Handling 6 | # Start date : 17/12/2011 7 | # End date : 17/12/2011 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # Dependency: None 11 | ############################################################################ 12 | 13 | #import modules 14 | import pygame 15 | from pygame.locals import * 16 | from sys import exit 17 | 18 | 19 | #initalizing variables (setting up the stage) 20 | pygame.init() 21 | a=pygame.display.set_mode((650,400),RESIZABLE,32) 22 | pygame.display.set_caption("Event list ") 23 | font=pygame.font.SysFont("arial",16) 24 | back=pygame.Surface(a.get_size()) 25 | back=back.convert() 26 | back.fill((255,255,255)) 27 | 28 | 29 | #main loop which displays the events related information 30 | while True: 31 | for i in pygame.event.get(): 32 | if i.type==QUIT: 33 | exit() 34 | else: 35 | text=font.render("%s"%i,1,(10,10,10),(255,255,255)) 36 | a.blit(back,(0,0)) 37 | a.blit(text,(0,0)) 38 | pygame.display.update() 39 | 40 | 41 | -------------------------------------------------------------------------------- /fireSprite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankur0890/Pygame-Examples-For-Learning/34dbbb89bbc208733bbb220a2033ffde3516496a/fireSprite.png -------------------------------------------------------------------------------- /hello.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name : hello.py 4 | # Purpose : A Small Demo Practice Hello World Prgram using Pygame API 5 | # Usages : Displays Hello World In Visual Mode. Considered as the first step of learning pygame API 6 | # Start date : 17/12/2011 7 | # End date : 17/12/2011 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # Dependency: simple.jpg 11 | ############################################################################ 12 | 13 | 14 | #import modules 15 | import pygame 16 | from pygame.locals import * 17 | from sys import exit 18 | 19 | #initalizing variables (setting up the stage) 20 | pygame.init() 21 | screen=pygame.display.set_mode((640,480),0,24) 22 | pygame.display.set_caption("Hello World") 23 | create=pygame.font.SysFont("comicsansms",30) 24 | f=create.render("Hello World",True,(0,0,0),(255,255,255)) 25 | img=pygame.image.load("simple.jpg").convert() 26 | 27 | 28 | #main loop which displays the hello world in visual mode 29 | while True: 30 | for i in pygame.event.get(): 31 | if i.type==QUIT: 32 | exit() 33 | 34 | screen.blit(img,(0,0)) 35 | screen.blit(f,(200,200)) 36 | pygame.display.update() 37 | 38 | -------------------------------------------------------------------------------- /keyinput.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name : keyinput.py 4 | # Purpose : A Small Demo Practice Prgram using Pygame API 5 | # Usages : Displays the pressed keys on the screen 6 | # Start date : 17/12/2011 7 | # End date : 17/12/2011 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # Dependency : None 11 | ############################################################################ 12 | 13 | #importing modules 14 | import pygame 15 | from pygame.locals import * 16 | from sys import exit 17 | 18 | 19 | #initializing variables 20 | pygame.init() 21 | screen=pygame.display.set_mode((640,480),0,24) 22 | pygame.display.set_caption("Key Press Test") 23 | f1=pygame.font.SysFont("comicsansms",24) 24 | 25 | #main loop which displays the pressed keys on the screen 26 | while True: 27 | for i in pygame.event.get(): 28 | if i.type==QUIT: 29 | exit() 30 | a=100 31 | screen.fill((255,255,255)) 32 | if pygame.key.get_focused(): 33 | press=pygame.key.get_pressed() 34 | for i in xrange(0,len(press)): 35 | if press[i]==1: 36 | name=pygame.key.name(i) 37 | text=f1.render(name,True,(0,0,0)) 38 | screen.blit(text,(100,a)) 39 | a=a+100 40 | pygame.display.update() 41 | 42 | -------------------------------------------------------------------------------- /paint.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name :paint.py 4 | # Purpose : Demo of Painting Program Made using Pygame API 5 | # Usages : You can paint through this. Features like save,load,line width can slo be included 6 | # Start date : 26/12/2011 7 | # End date : 28/12/2011 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # Reference : Game Programming By Andy Harris 11 | # How To Run : paint.py 12 | ############################################################################ 13 | 14 | #import all necessary modules 15 | import pygame 16 | from pygame.locals import * 17 | 18 | 19 | #initializations 20 | pygame.init() 21 | screen=pygame.display.set_mode((640,480)) 22 | pygame.display.set_caption('Let\'s Paint') 23 | background=pygame.Surface(screen.get_size()) 24 | background=background.convert() 25 | color=(0,0,0) 26 | startPos=(0,0) 27 | clock=pygame.time.Clock() 28 | background.fill((255,255,255)) 29 | font=pygame.font.SysFont('arial',20) 30 | colorName="Black" 31 | welcomeFont=pygame.font.SysFont('arial',30) 32 | colorConfigFont=pygame.font.SysFont('arial',20) 33 | welcomeScreen=True 34 | 35 | #mainloop 36 | while 1: 37 | #welcomescreen 38 | while welcomeScreen: 39 | for i in pygame.event.get(): 40 | mainScreenPressed=pygame.key.get_pressed() 41 | if i.type==QUIT or mainScreenPressed[K_q]: 42 | exit() 43 | screen.fill((0,0,0)) 44 | screen.blit(welcomeFont.render("Paint Program Example For Learning",True,(0,255,0)),(100,100)) 45 | screen.blit(colorConfigFont.render("Painting Options",True,(0,255,0)),(100,150)) 46 | screen.blit(colorConfigFont.render("d-Default Color(Black)",True,(0,255,0)),(100,175)) 47 | screen.blit(colorConfigFont.render("b-Blue Color",True,(0,255,0)),(100,200)) 48 | screen.blit(colorConfigFont.render("r-Red Color",True,(0,255,0)),(100,225)) 49 | screen.blit(colorConfigFont.render("y-Yellow Color",True,(0,255,0)),(100,250)) 50 | screen.blit(colorConfigFont.render("g-Green Color",True,(0,255,0)),(100,275)) 51 | screen.blit(colorConfigFont.render("e-Eraser",True,(0,255,0)),(100,300)) 52 | screen.blit(colorConfigFont.render("s-Save Image",True,(0,255,0)),(100,325)) 53 | screen.blit(colorConfigFont.render("l-Load Image",True,(0,255,0)),(100,350)) 54 | screen.blit(welcomeFont.render("Press p for painting",True,(0,255,0)),(100,400)) 55 | pygame.display.flip() 56 | if mainScreenPressed[K_p]: 57 | welcomeScreen=False 58 | 59 | #paint screen 60 | pressed=pygame.key.get_pressed() 61 | if pressed[K_r]: 62 | color=(255,0,0) 63 | colorName="Red" 64 | elif pressed[K_g]: 65 | color=(0,255,0) 66 | colorName="Green" 67 | elif pressed[K_b]: 68 | color=(0,0,255) 69 | colorName="Blue" 70 | elif pressed[K_y]: 71 | color=(255,255,0) 72 | colorName="Yellow" 73 | elif pressed[K_e]: 74 | color=(255,255,255) 75 | colorNAme="White" 76 | elif pressed[K_d]: 77 | color=(0,0,0) 78 | colorName="Black" 79 | elif pressed[K_c]: 80 | color=(0,0,0) 81 | colorName="Black" 82 | welcomeScreen=True 83 | elif pressed[K_s]: 84 | pygame.image.save(background,'image.png') 85 | elif pressed[K_l]: 86 | background=pygame.image.load('image.png') 87 | colorName="Black" 88 | color=(0,0,0) 89 | 90 | 91 | for i in pygame.event.get(): 92 | if i.type==QUIT or pressed[K_q]: 93 | exit() 94 | elif i.type==pygame.MOUSEMOTION: 95 | endPos=pygame.mouse.get_pos() 96 | if pygame.mouse.get_pressed()==(1,0,0): 97 | pygame.draw.line(background,color,startPos,endPos,3) 98 | startPos=endPos 99 | 100 | screen.blit(background,(0,0)) 101 | if color==(255,255,255): 102 | screen.blit(font.render("Eraser In Use",True,(0,0,0)),(400,400)) 103 | screen.blit(font.render("Press c for painting options",True,(0,0,0)),(100,400)) 104 | else: 105 | screen.blit(font.render("Press c for painting options",True,(0,0,0)),(100,400)) 106 | screen.blit(font.render("Color In Use : %s"%colorName,True,color),(400,400)) 107 | pygame.display.flip() 108 | 109 | -------------------------------------------------------------------------------- /rotation.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name : rotation.py 4 | # Purpose : A Small Demo Practice Prgram using Pygame API 5 | # Usages : Simplest rotation program using transform class 6 | # Start date : 17/12/2011 7 | # End date : 17/12/2011 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # Dependency : naughty.png 11 | ############################################################################ 12 | 13 | import pygame 14 | from pygame.locals import * 15 | from sys import exit 16 | 17 | pygame.init() 18 | screen=pygame.display.set_mode((640,480),0,0) 19 | pygame.display.set_caption("Rotation using transform module") 20 | 21 | image=pygame.image.load("naughty.png").convert_alpha() 22 | image2=image 23 | a=0 24 | clock=pygame.time.Clock() 25 | while True: 26 | for i in pygame.event.get(): 27 | if i.type==QUIT: 28 | exit() 29 | screen.fill((0,0,0)) 30 | rotation=pygame.mouse.get_rel() 31 | buttonpress=pygame.mouse.get_pressed() 32 | press=pygame.key.get_pressed() 33 | screen.blit(image2,(100-(image2.get_width()/2),100-(image2.get_height()/2))) 34 | if rotation[0] and buttonpress[0]: 35 | a=a+rotation[0] 36 | image2=pygame.transform.rotate(image,a) 37 | if press[K_LEFT]: 38 | a=a+10 39 | image2=pygame.transform.rotate(image,a) 40 | if press[K_RIGHT]: 41 | a=a-10 42 | image2=pygame.transform.rotate(image,a) 43 | pygame.display.update() 44 | clock.tick(15) 45 | 46 | 47 | -------------------------------------------------------------------------------- /simple.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankur0890/Pygame-Examples-For-Learning/34dbbb89bbc208733bbb220a2033ffde3516496a/simple.jpg -------------------------------------------------------------------------------- /soundPlay.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name :soundPlay.py 4 | # Purpose : Demostrating Audio File Involvement In A Pygame Program 5 | # Start date : 30/12/2011 6 | # End date : 30/12/2011 7 | # Author : Ankur Aggarwal 8 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 9 | # Reference : Game Programming By Andy Harris 10 | # Dependancy : yikes.ogg 11 | ############################################################################ 12 | 13 | import pygame 14 | from pygame.locals import * 15 | 16 | 17 | pygame.init() 18 | pygame.mixer.init() #mixer module Intialization 19 | 20 | def main(): 21 | screen=pygame.display.set_mode((640,480),0,24) 22 | pygame.display.set_caption('Sound Testing') 23 | sound=pygame.mixer.Sound('yikes.wav') #creating an audio object 24 | font=pygame.font.SysFont('arial',30) 25 | soundFont=font.render("Press Space To Hear A Sound",True,(0,255,0)) 26 | while 1: 27 | pressed=pygame.key.get_pressed() 28 | for i in pygame.event.get(): 29 | if i.type==QUIT or pressed[K_q]: 30 | exit() 31 | elif pressed[K_SPACE]: 32 | sound.play() #time to play 33 | screen.blit(soundFont,(100,100)) 34 | pygame.display.flip() 35 | 36 | 37 | if __name__=='__main__': 38 | main() 39 | -------------------------------------------------------------------------------- /sprite.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name : sprite.py 4 | # Purpose : A Small Demo Practice Prgram using Pygame API 5 | # Usages : Teaches Us How To Use Sprites 6 | # Start date : 17/12/2011 7 | # End date : 17/12/2011 8 | # Author : Ankur Aggarwal 9 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 10 | # Dependency : fireSprite.png 11 | ############################################################################ 12 | 13 | import pygame 14 | from pygame.locals import * 15 | from sys import exit 16 | 17 | counter=0 18 | def Update(): 19 | global counter 20 | counter=(counter+1)%7 21 | 22 | def sprite(w, h): 23 | a=[] 24 | clock=pygame.time.Clock() 25 | screen=pygame.display.set_mode((200,200),0,24) 26 | image = pygame.image.load("fireSprite.png").convert_alpha() 27 | width,height=image.get_size() 28 | for i in xrange(int(width/w)): 29 | a.append(image.subsurface((i*w,0,w,h))) 30 | while True: 31 | for i in pygame.event.get(): 32 | if i.type==QUIT: 33 | exit() 34 | screen.fill((0,0,0)) 35 | screen.blit(a[counter],(100,100)) 36 | Update() 37 | pygame.display.update() 38 | clock.tick(5) 39 | 40 | sprite(20,20) 41 | 42 | 43 | -------------------------------------------------------------------------------- /viewColorCodes.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ############################################################################ 3 | # File name : viewColorCodes.py 4 | # Usages : Will Display All The Color Codes along with the color name and its value 5 | # Start date : 29/12/2011 6 | # End date : 29/12/2011 7 | # Author : Ankur Aggarwal 8 | # License : GNU GPL v3 http://www.gnu.org/licenses/gpl.html 9 | # How To Run : python viewColorCodes.py 10 | # Reference : Game Programming By Andy Harris 11 | ############################################################################ 12 | 13 | import pygame 14 | from pygame.locals import * 15 | 16 | 17 | #To explore more see the pydoc for pygame.color.THECOLORS 18 | def main(): 19 | for colorName,value in pygame.color.THECOLORS.items(): 20 | print colorName,value 21 | 22 | 23 | if __name__=="__main__": 24 | main() 25 | 26 | -------------------------------------------------------------------------------- /yikes.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankur0890/Pygame-Examples-For-Learning/34dbbb89bbc208733bbb220a2033ffde3516496a/yikes.ogg --------------------------------------------------------------------------------