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. jQuery中json对象与json字符串互换

    json字符串转json对象:jQuery.parseJSON(jsonStr); json对象转json字符串:JSON.stringify(jsonObj); 根据“|”把字符串变成数组.spli ...

  2. Xamarin.ios 调用接口

    NSString urlstring = new NSString("http://211.155.229.230:8585/api/users/postregister");  ...

  3. jquery tree

      <div class="fl left"  id="nav" style="width:18%" >             ...

  4. 在thinkphp中js文件添加路径

    JS文件不认识__URL__这些tp独特的链接,所以要自己重新定义url,才能使用这些路径. 在js对于的模板上定义url 然后在js文件中引用

  5. 子div设置float后导致父div无法自动撑开的问题

    子div设置float后会导致父div无法自动撑开 原因:内部的DIV因为float:left之后,就丢失了clear:both和display:block的样式,所以外部的DIV不会被撑开. 以下是 ...

  6. 【01-06】JPA 全局单一主键

    建一张主键表 @Override public boolean equals(Object o) { return (o == this || (o instanceof AbstractEntity ...

  7. 【11-10】spring学习笔记-ApplicationContextAware

    package util; /** * @author aloha_world_ * @date 2016年11月10日 下午7:50:08 * @version v1.00 * @descripti ...

  8. 字符串 HDU 1039

    规则: 1.必须至少包含一个元音字母.a e i o u 2.不能包含三个连续元音或者连续辅音字母. 3.不能包含两个连续字母,除了'ee'和'oo'. PS:字母个数(1<= N <=2 ...

  9. mysql 语法

    -create database database_name; show databases;show tables;                                 | -creat ...

  10. 【Android】NavigationView头部点击监听事件

    AndroidStudio给出的模板里面只有列表点击事件,即实现OnNavigationItemSelectedListener中的onNavigationItemSelected方法,根据item的 ...