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()
pygame学习笔记的更多相关文章
- pygame学习笔记(3)——时间、事件、文字
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.运动速率 上节中,实现了一辆汽车在马路上由下到上行驶,并使用了pygame.time.delay(200 ...
- PyGame学习笔记之壹
新建窗口 代码 '''PyGame学习笔记之壹''' import pygame # 引入 PyGame 库 pygame.init() # PyGame 库初始化 screen = pygame.d ...
- pygame学习笔记(4)——声音
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi pygame.mixer是一个用来处理声音的模块,其含义为“混音器”.游戏中对声音的处理一般包括制造声音和播放声音 ...
- pygame学习笔记(5)——精灵
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 据说在任天堂FC时代,精灵的作用相当巨大,可是那时候只知道怎么玩超级玛丽.魂斗罗,却对精灵一点也不知.pygame ...
- pygame学习笔记(1)——安装及矩形、圆型画图
pygame是一个设计用来开发游戏的python模块,其实说白了和time.os.sys都是一样的东东.今天开始正式学习pygame,下载地址:www.pygame.org.下载后安装完成即可,在py ...
- pygame学习笔记(6)——一个超级简单的游戏
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 学了这么长时间的Pygame,一直想写个游戏实战一下.看起来很简单的游戏,写其来怎么这么难.最初想写个俄罗斯方块 ...
- pygame学习笔记(2)——从画点到动画
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.单个像素(画点)利用pygame画点主要有三种方法:方法一:画长宽为1个像素的正方形 #@小五义 http:/ ...
- PYGAME学习笔记_01
01_使用PYGAME创建图形窗口 1.1_游戏的初始化和退出 pygame.init() 写入并初始化所有PYGAME模块,使用其他模块之前,必须先调用init方法 pygame.quit() 卸载 ...
- 转 pygame学习笔记(1)——安装及矩形、圆型画图
http://www.cnblogs.com/xiaowuyi/archive/2012/06/06/2538921.html
随机推荐
- Word2016(2013)怎么从任意页插入起始页码
添加页码 1 小知识:双击任意页的页脚或页眉区域,激活页眉页脚设计界面.双击文档正文区域,返回正文编辑界面. 2 双击任意页的页脚或页眉区域,激活页眉页脚设计界面.单击"页码" ...
- 10个常见的Node.js面试题
如果你希望找一份有关Node.js的工作,但又不知道从哪里入手评测自己对Node.js的掌握程度. 本文就为你罗列了10个常见的Node.js面试题,分别考察了Node.js编程相关的几个主要方面. ...
- Android 热修复方案Tinker
转自:http://blog.csdn.net/l2show/article/details/53925543 Android 热修复方案Tinker(一) Application改造 Android ...
- PHP的final关键字、static关键字、const关键字
在PHP5中新增加了final关键字,它可以加载类或类中方法前.但不能使用final标识成员属性,虽然final有常量的意思,但在php中定义常量是使用define()函数来完成的. final关键字 ...
- Hash算法专题
1.[HDU 3068]最长回文 题意:求一个字符串(len<=110000)的最长回文串 解题思路:一般解法是manacher,但是这一题用hash也是可以ac的 假设当前判断的是以i为中心偶 ...
- C和指针 第十五章 文件I/O
stdio.h中包含了声明FILE结构 struct _iobuf { char *_ptr; //文件输入的下一个位置 int _cnt; //当前缓冲区的相对位置 char *_base; //指 ...
- CentOS 下使用yum安装nodejs
在xenserver虚拟机器上安装nodejs 尝试失败方法, 1.比如解压编译好的文件,使用ln设置全局变量,因为解压出来没有bin目录无法使用 ln -s /home/kun/mysofltwar ...
- 新型的Hbb项目目录结构
- Hbb - ComponentPacket (底层组件包) 数据库组件 网络组件 格式化组件 - Resources (存放所有图片资源文件) - ToolClass (工具类/Helper 独立 ...
- document封装一些常用的方法
/** * 批量修改元素样式 */ function css(domObj,styleArry){ for(var i=0;i<styleArry.length;i++){ domObj.sty ...
- YII2之 Scenario
使用方法 // scenario is set as a property $model = new User; $model->scenario = User::SCENARIO_SHOW; ...