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()

pygame学习笔记的更多相关文章

  1. pygame学习笔记(3)——时间、事件、文字

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.运动速率    上节中,实现了一辆汽车在马路上由下到上行驶,并使用了pygame.time.delay(200 ...

  2. PyGame学习笔记之壹

    新建窗口 代码 '''PyGame学习笔记之壹''' import pygame # 引入 PyGame 库 pygame.init() # PyGame 库初始化 screen = pygame.d ...

  3. pygame学习笔记(4)——声音

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi pygame.mixer是一个用来处理声音的模块,其含义为“混音器”.游戏中对声音的处理一般包括制造声音和播放声音 ...

  4. pygame学习笔记(5)——精灵

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 据说在任天堂FC时代,精灵的作用相当巨大,可是那时候只知道怎么玩超级玛丽.魂斗罗,却对精灵一点也不知.pygame ...

  5. pygame学习笔记(1)——安装及矩形、圆型画图

    pygame是一个设计用来开发游戏的python模块,其实说白了和time.os.sys都是一样的东东.今天开始正式学习pygame,下载地址:www.pygame.org.下载后安装完成即可,在py ...

  6. pygame学习笔记(6)——一个超级简单的游戏

    转载请注明:@小五义  http://www.cnblogs.com/xiaowuyi 学了这么长时间的Pygame,一直想写个游戏实战一下.看起来很简单的游戏,写其来怎么这么难.最初想写个俄罗斯方块 ...

  7. pygame学习笔记(2)——从画点到动画

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.单个像素(画点)利用pygame画点主要有三种方法:方法一:画长宽为1个像素的正方形 #@小五义 http:/ ...

  8. PYGAME学习笔记_01

    01_使用PYGAME创建图形窗口 1.1_游戏的初始化和退出 pygame.init() 写入并初始化所有PYGAME模块,使用其他模块之前,必须先调用init方法 pygame.quit() 卸载 ...

  9. 转 pygame学习笔记(1)——安装及矩形、圆型画图

    http://www.cnblogs.com/xiaowuyi/archive/2012/06/06/2538921.html

随机推荐

  1. Word2016(2013)怎么从任意页插入起始页码

    添加页码   1 小知识:双击任意页的页脚或页眉区域,激活页眉页脚设计界面.双击文档正文区域,返回正文编辑界面. 2 双击任意页的页脚或页眉区域,激活页眉页脚设计界面.单击"页码" ...

  2. 【Phylab2.0】Beta版本项目展示

    团队成员 冯炜韬(PM)http://www.cnblogs.com/toka 岳桐宇(后端)http://www.cnblogs.com/mycraftmw 杨子琛(测试&LaTeX)htt ...

  3. mysql主从复制操作步骤

    注: .做主从复制的两台服务器server-id不能相同 .主从的字符集要一样,不然数据导入会报错 .开启db01的log-bin功能 [root@db01 mysql]# vim /etc/my.c ...

  4. Visual Studio 2010 下 安装RGiesecke.DllExport

    RGiesecke.DllExport 在 UnmanagedExports 中.安装过程如下: 1.首先在"工具"菜单下的"扩展管理器"中,安装 NuGet ...

  5. linux 时间同步的2种方法

    转载自: http://blog.51yip.com/server/1474.html 由于硬件的原因,机器或多或少的根标准时间对不上,一个月的误差几秒到几分钟不等.对于服务器来说时间不准,会有很多麻 ...

  6. EasyPusher应用

    转自https://github.com/EasyDarwin/EasyPusher 本文仅实际体验一下demo,分析一下如何应用. 1)EasyPusher框图预览 2) EasyPusher应用实 ...

  7. 【Matlab】特征值

    特征值 clc;clear; %[V,D]=eig(A) //求取特征值 A=[ 1 2 4; 4 0 7; 9 1 3 ]; [V,D]=eig(A) 结果如下: 求解特征值与特征向量时矩阵必须是方 ...

  8. Visual Studio EventHandler Delegate 和 EventArgs

    EventHandler代理 用来表示处理一个没有事件数据(event data)的事件(event)的 方法. 无论何时事件发生时,事件代理就被调用来触发以前事件驱动的其他事件(监听当前事件TCur ...

  9. [cocos] ( 01 ) cocos2d-x 3.x 开发 环境配置

    有几个需要注意的问题 Windows上使用时, Unable to execute dex: Multiple dex files define 在eclipse中libcoco2dx的Java Bu ...

  10. 两台win7电脑网线直连办法(共享文件夹形式)

    一.背景 一台电脑需要测试,但要不停更新APP,可是该电脑没网络,用U盘太繁琐,即想到用网线将两台 电脑直连,一台电脑共享文件夹给另一台电脑,达到交换文件的目的. 感谢Tony(http://www. ...