pygame学习笔记

pygame参考文档pdf版:pygame API

html版 pygame API

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

 #!/usr/bin/env python
import sys
import os
import pygame
from pygame import key
import threading
import random
from sys import exit def getcolor(screen):
screen = pygame.display.get_surface()
bg = (173, 216, 230)# blue
screen.fill(bg)
pygame.display.flip() class GameSJB:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800, 0),pygame.RESIZABLE) # width,height
print(pygame.display.get_surface())
pygame.display.set_caption('Pygame Game')
self.font = pygame.font.Font(None, 18)
getcolor(self.screen)
self.image = []
self.imagerect = []
if not os.path.exists('images'):
print('The pics don\'t exit')
pygame.quit()
sys.exit()
else:
print 'YES, the path is ./images'
self.vs = pygame.image.load('images/vs.png').convert()
self.o = pygame.image.load('images/o.png')
'''owdth,oht = self.o.get_size() #pic' size
print(owdth,oht)'''
self.p = pygame.image.load('images/p.png')
self.u = pygame.image.load('images/u.png')
self.title = pygame.image.load('images/title.png')
self.start = pygame.image.load('images/start.png')
self.exit = pygame.image.load('images/exit.png')
for i in range(3):
png = pygame.image.load('images/' + str(i) + '.png')
self.image.append(png)
for i in range(3):
img = self.image[i]
rect = img.get_rect()#the rectangular area
print rect
rect.left += 200 * (i + 1)
rect.top = 400
self.imagerect.append(rect) def isStart(self):
pos = pygame.mouse.get_pos()
print pos
if 350 < pos[0] < 450:
if 300 < pos[1] < 350:
return 0
elif 400 < pos[1] < 450:
return 1
else:
return 2
else:
return 2 def iswin(self, value):
num = random.randint(0,2)
self.screen.blit(self.image[num], (450, 150, 590, 240))
pygame.display.flip()
if num == value:
self.screen.blit(self.image[num], (450,150,590,240))
pygame.display.flip()
elif num < value:
if num == 0:
if value == 2:
self.screen.blit(self.u, (220, 10, 140, 70))
else:
self.screen.blit(self.p, (220,10,140,70))
pygame.display.flip()
else:
self.screen.blit(self.u, (220,10,140,70))
pygame.display.flip()
else:
if num == 2:
if value == 1:
self.screen.blit(self.u, (220, 10,140,70))
else:
self.screen.blit(self.p, (220,10,140,70))
pygame.display.flip()
else:
self.screen.blit(self.u, (220,10,140,70))
pygame.display.flip() def OnMouseButDown(self):
self.screen.blit(self.vs, (300,150,140,140))#draw pic
pos = pygame.mouse.get_pos()
if 400 < pos[1] < 540:
if 200 < pos[0] < 340:
self.screen.blit(self.image[0], (150,150,140,140))
self.iswin(0)
elif 400 < pos[0] < 540:
self.screen.blit(self.image[1], (150,150,140,140))
self.iswin(1)
elif 600 < pos[0] < 740:
self.screen.blit(self.image[2], (150,150,140,140))
self.iswin(2)
else:
pass def run(self):
self.screen.fill((0, 0, 0))
for i in range(3):
self.screen.blit(self.image[i], self.imagerect[i])
pygame.display.flip()
while True:
for event in pygame.event.get(): # close the app by button
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
self.OnMouseButDown()
else:
pass
keys = pygame.key.get_pressed() # close the app by ESC
if keys[pygame.K_ESCAPE]:
sys.exit() def Start(self):
self.screen.blit(self.title, (200, 100, 400, 140))
self.screen.blit(self.start, (350, 300, 100, 50))
self.screen.blit(self.exit, (350, 400, 100, 50))
pygame.display.flip() # refresh
start = 1
while start: # enter into mesg cycle
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.isStart() == 0:
start = 0
elif self.isStart() == 1:
sys.exit()
else:
pass
else:
pass
self.run()
print(os.system(' python --version'))
print('sdl version:',pygame.get_sdl_version())
print('pygame version:',pygame.__version__)
game = GameSJB()
game.Start()
上一篇:iframe内容自适应高度


下一篇:approval workflow in sharepoint designer