pygame学习笔记

pygame参考文档pdf版:pygame API

html版 pygame API

 

石头剪子布的简单小游戏,待改进的地方,自适应大小.感兴趣的小伙伴可以依据get_surface()返回值(即当前窗口大小比率)来确定.

 

  1 #!/usr/bin/env python
  2 import sys
  3 import os
  4 import pygame
  5 from pygame import key
  6 import threading
  7 import random
  8 from sys import exit
  9 
 10 
 11 def getcolor(screen):
 12     screen = pygame.display.get_surface()
 13     bg = (173, 216, 230)# blue
 14     screen.fill(bg)
 15     pygame.display.flip()
 16 
 17 
 18 class GameSJB:
 19     def __init__(self):
 20         pygame.init()
 21         self.screen = pygame.display.set_mode((800, 0),pygame.RESIZABLE)  # width,height
 22         print(pygame.display.get_surface())
 23         pygame.display.set_caption('Pygame Game')
 24         self.font = pygame.font.Font(None, 18)
 25         getcolor(self.screen)
 26         self.image = []
 27         self.imagerect = []
 28         if not os.path.exists('images'):
 29             print('The pics don\'t exit')
 30             pygame.quit()
 31             sys.exit()
 32         else:
 33             print 'YES, the path is ./images'
 34         self.vs = pygame.image.load('images/vs.png').convert()
 35         self.o = pygame.image.load('images/o.png')
 36         '''owdth,oht = self.o.get_size() #pic' size
 37         print(owdth,oht)'''
 38         self.p = pygame.image.load('images/p.png')
 39         self.u = pygame.image.load('images/u.png')
 40         self.title = pygame.image.load('images/title.png')
 41         self.start = pygame.image.load('images/start.png')
 42         self.exit = pygame.image.load('images/exit.png')
 43         for i in range(3):
 44             png = pygame.image.load('images/' + str(i) + '.png')
 45             self.image.append(png)
 46         for i in range(3):
 47             img = self.image[i]
 48             rect = img.get_rect()#the rectangular area
 49             print rect
 50             rect.left += 200 * (i + 1)
 51             rect.top = 400
 52             self.imagerect.append(rect)
 53 
 54     def isStart(self):
 55         pos = pygame.mouse.get_pos()
 56         print pos
 57         if 350 < pos[0] < 450:
 58             if 300 < pos[1] < 350:
 59                 return 0
 60             elif 400 < pos[1] < 450:
 61                 return 1
 62             else:
 63                 return 2
 64         else:
 65             return 2
 66 
 67     def iswin(self, value):
 68         num = random.randint(0,2)
 69         self.screen.blit(self.image[num], (450, 150, 590, 240))
 70         pygame.display.flip()
 71         if num == value:
 72             self.screen.blit(self.image[num], (450,150,590,240))
 73             pygame.display.flip()
 74         elif num < value:
 75             if num == 0:
 76                 if value == 2:
 77                     self.screen.blit(self.u, (220, 10, 140, 70))
 78                 else:
 79                     self.screen.blit(self.p, (220,10,140,70))
 80                 pygame.display.flip()
 81             else:
 82                 self.screen.blit(self.u, (220,10,140,70))
 83                 pygame.display.flip()
 84         else:
 85             if num == 2:
 86                 if value == 1:
 87                     self.screen.blit(self.u, (220, 10,140,70))
 88                 else:
 89                     self.screen.blit(self.p, (220,10,140,70))
 90                 pygame.display.flip()
 91             else:
 92                 self.screen.blit(self.u, (220,10,140,70))
 93                 pygame.display.flip()
 94 
 95     def OnMouseButDown(self):
 96         self.screen.blit(self.vs, (300,150,140,140))#draw pic
 97         pos = pygame.mouse.get_pos()
 98         if 400 < pos[1] < 540:
 99             if 200 < pos[0] < 340:
100                 self.screen.blit(self.image[0], (150,150,140,140))
101                 self.iswin(0)
102             elif 400 < pos[0] < 540:
103                 self.screen.blit(self.image[1], (150,150,140,140))
104                 self.iswin(1)
105             elif 600 < pos[0] < 740:
106                 self.screen.blit(self.image[2], (150,150,140,140))
107                 self.iswin(2)
108             else:
109                 pass
110 
111     def run(self):
112         self.screen.fill((0, 0, 0))
113         for i in range(3):
114             self.screen.blit(self.image[i], self.imagerect[i])
115         pygame.display.flip()
116         while True:
117             for event in pygame.event.get():  # close the app by button
118                 if event.type == pygame.QUIT:
119                     pygame.quit()
120                     sys.exit()
121                 elif event.type == pygame.MOUSEBUTTONDOWN:
122                     self.OnMouseButDown()
123                 else:
124                     pass
125             keys = pygame.key.get_pressed()  # close the app by ESC
126             if keys[pygame.K_ESCAPE]:
127                 sys.exit()
128 
129     def Start(self):
130         self.screen.blit(self.title, (200, 100, 400, 140))
131         self.screen.blit(self.start, (350, 300, 100, 50))
132         self.screen.blit(self.exit, (350, 400, 100, 50))
133         pygame.display.flip()  # refresh
134         start = 1
135         while start:  # enter into mesg cycle
136             for event in pygame.event.get():
137                 if event.type == pygame.QUIT:
138                     sys.exit()
139                 elif event.type == pygame.MOUSEBUTTONDOWN:
140                     if self.isStart() == 0:
141                         start = 0
142                     elif self.isStart() == 1:
143                         sys.exit()
144                     else:
145                         pass
146                 else:
147                     pass
148         self.run()
149 print(os.system(' python --version'))
150 print('sdl version:',pygame.get_sdl_version())
151 print('pygame version:',pygame.__version__)
152 game = GameSJB()
153 game.Start()

 

posted @ 2016-11-12 11:27  PKICA  阅读(417)  评论(0编辑  收藏  举报