python二维游戏编程 豆瓣_用Python和Pygame写游戏-从入门到精通(1)

Love the new design. I was pleased with the content. Thanks for your excellent blog.

Oh, the 1st comment on my blog. The first time is always sweet(although it doesn’t look like posted by a people…), thanks and I will do better~

感觉作者人超可爱 内心世界很萌

这么说别人 说明你也很萌啊

Thanks for your excellent blog.It helps me a lot.

You got a really useful blog I have been here reading for about half an hour. I am a newbie and your post is valuable for me.

留名,以后详细看

一直在用django,但很少用python写游戏。之前看到讲pygame的也都是英文版。多谢博主分享~

Hi

1.我用你的图片后,运行程序,发现背景没有被填充满

查看Backgroup image的大小发现不是640*480的,而是580*435

2.看你的图片,在fugu.png上没有显示鼠标,而我运行时,在fugu.png上还显示了一个鼠标,这个是为什么?

xishui Post author2011/07/06

看来您是第一个真正实践的啊~伟大!

1: 图片被WP自动缩放了下……现在更新了,谢谢提醒;

2: 这是正常的,因为截图的时候鼠标不被截取,后期会讲到如何隐藏光标用Python和Pygame写游戏-从入门到精通(11),使用pygame.mouse.set_visible。

您好我想问下就是比如其中的一些方法 例如pygame.display.set_caption()这些方法我应该如何查询知道该模块中的类中有这个方法。感觉直接help中也无法查询到就很纠结。而且比如在检测案件的返回值的时候也不知道要检测什么按键怎么输入检测按键名,这些东西的查询不知道何处查询 谢谢指导

Pygame官网 右上的doc不行吗

感谢您 19年在学python, pygame.

pygame.mouse.set_visible(False)把这行代码加到鼠标坐标的代码上面就可以了

@xishui:

1.呵呵,实践出真理嘛

2.关于第2个问题,那就是截图工具配置的问题了,我使用HyperSnap截图工具,默认是不隐藏鼠标的

3.另外一个问题,在你的图片上,Title的左边有一个小人的图标,而在我的程序上没有显示该图标,显示的是未指定的那种图标,但将其最小化时,可以在任务栏上看到那个图标。

xishui Post author2011/07/06

@Kingson: 真是观察入微,不过图标显示仅仅是因为操作系统和pygame的版本的关系,和我们的程序无关不用在意,我们也可以在最终作品中手动指定一个自己喜欢的图标,见py2exe编。

@xishui:

OK,感谢你的热心答复。

这个真的很好!一定要顶,留着学习!

楼主 谢谢你的共享,以后我会和你一样 谢谢

你好 感觉pygame开发个小列子,感觉cpu占用很多 ,这个helloworld 我允许的时候 cpu占用了25%啊!

我是酷睿i3啊

xishui Post author2011/07/21

首先,Python的执行效率肯定是不能和C、C++相比的。不过这个helloworld这么猛并不是因为有多大的运算量,而是急速的刷新引起的。游戏与一般程序不同,一旦启动了,就会用光CPU资源(如果您是单核CPU的话,就是100%了……)来保证画面,这是正常的。在这种情况下,我们可以限制帧率,参考用Python和Pygame写游戏-从入门到精通(8)中的tick(最大帧率),比如设成30,在我的破机器上都可以把CPU占用率有效的控制在5%以下。

学习了, 感谢! 对编程初学非常新颖的知识

十分感谢楼的教程,但我遇到点问题,我下载的是pygame 1.9.2pre程序运行起来提示

Traceback (most recent call last):

File “D:/py/first.py”, line 27, in

x-= mouse_cursor.get_width() / 2

NameError: name ‘mouse_cursor’ is not defined

没有定义mouse_cursor

xishui Post author2011/07/27

这事情很神奇…… 确认mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()执行成功了?名字没有拼错?查找mouse_cursor确认一下吧。

你好楼主,我在做测试的时候,显示没有问题,在点击X关闭窗口时,出现如下提示,再按一次窗口又可以退出了

Traceback (most recent call last):

File “C:python-codeHelloWorldHelloWorld.py”, line 29, in

exit()

SystemExit

大神,还麻烦问下。如果Ubuntu下不在终端通过sudo python ***.py 能够执行已经引入 pygame的py程序么? 在我的程序里既用到了pygame又用到了caffe,可sudo python ***.py无法找到caffe,而python ***.py无法找到pygame,求大神帮忙指点方向。

我试了下,在exit()前面加上pygame.quit()语句就可以正常关闭了,win10

这好像不是win10的问题, 而是IDE的问题. 据说Python自带的IDLE有这个bug, 需要写上pygame.quit() 才能正常退出

楼主出现这个是什么为题呢:

Traceback (most recent call last):

File “C:/Python27/程序/game1.py”, line 17, in

x-=mouse_cursor.get_width() /2

AttributeError: ‘builtin_function_or_method’ object has no attribute ‘get_width’

xishui Post author2011/09/15

神奇~这样简单的脚本也会出现莫名其妙的错误,这个世界真是充满了无限的未知……

试一下在“mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()”

后面加一句print type(mouse_cursor), dir(mouse_cursor)

看看输出是什么?

为什么先背景,后鼠标,可以看到鼠标:如下

screen.blit(background, (0,0))

screen.blit(mouse_cursor, (x, y))

但是先鼠标,后背景,就看不到鼠标呢?如下:

screen.blit(mouse_cursor, (x, y))

screen.blit(background, (0,0))

xishui Post author2011/09/16

真的,为什么呢……

我在白纸上用颜料画好背景,然后再画个小人,嗯,很漂亮;我先画个小人,然后再用颜料把背景涂满画面,人呢?人怎么看不到了?! :) Just the same

楼主~ 当我关闭窗口的时候控制台显示SystemExit但窗口并不关闭而程序无响应.然后再关闭一次才能关闭,每次都这样的.请问是怎么回事吖~菜鸟问~

有没有发现窗体闪烁的情况!?

就是当前窗体(Pygame生成的)被别的窗体部分覆盖以后,被覆盖窗体的偶尔会闪烁一下。

screen = pygame.display.set_mode(size)

screen = pygame.display.set_mode(size,DOUBLEBUF)

screen = pygame.display.set_mode(size,HWSURFACE|DOUBLEBUF)

都一样

@Ken:

win7的系统(aero风格),intel的集显。

@小福: 把SYS.EXIT 改成OS.EXIT就解决了

请问有python的中文api吗

xishui Post author2011/09/26

很可惜没有,要想学好计算机,必须把英语熟练掌握……

runforever2011/10/01

博主是好人啊, 正想学pygame

你好啊

有个问题 我在win7上做开发的没有合适的pygame版本啊

我的是py2.5.5 64位win7 怎么解决啊 !

xishui Post author2011/11/10

你好,虽然64位如日中天,不过有些python库只有32位,还有些64位的库运作起来似乎有些问题,所以Windows上我现在还是只用32位的python……

>>> Traceback (most recent call last):

File “D:Program FilesPyScripterLibrpyc-python2x.ziprpyccorenetref.py”, line 123, in __call__

File “D:Program FilesPyScripterLibrpyc-python2x.ziprpyccorenetref.py”, line 45, in syncreq

File “D:Program FilesPyScripterLibrpyc-python2x.ziprpyccoreprotocol.py”, line 341, in sync_request

File “D:Program FilesPyScripterLibrpyc-python2x.ziprpyccoreprotocol.py”, line 158, in _send_request

File “D:Program FilesPyScripterLibrpyc-python2x.ziprpyccoreprotocol.py”, line 153, in _send

File “D:Program FilesPyScripterLibrpyc-python2x.ziprpyccorechannel.py”, line 50, in send

File “D:Program FilesPyScripterLibrpyc-python2x.ziprpyccorestream.py”, line 119, in write

EOFError: [Errno 10054] An existing connection was forcibly closed by the remote host

大神…我每次运行完都有这些东西…是怎么回事?

xishui Post author2011/11/11

看起来阁下是使用IDLE里运行的?IDLE对初学者是不错,不过运行复杂程序的时候,可能会有各种神奇的问题……试着只用console看看?

purplestonel2011/11/13

请问下博主,Alpha 通道是什么呢? 我的系统告诉我error: Couldn’t open sushiplate.jpg

xishui Post author2011/11/14

抱歉,这个有些复杂了,请自行搜索,简单说是(不)透明度,但是实际还是需要很多东西来理解;至于错误,你是不是没有把图片放到代码文件的目录下呢?

请问代码文件的目录在哪里?

我根据大师的代码实践了一篇,只是显示一个窗口,就是不显示图片和背景 也没有提示错误(我用的是WINpython3.1) 希望大师能指导一下 我用的系统是WINDOWS7 32位 QQ:657804682 帮我解答一下 谢谢诶

您好,我使用自己的背景图片无法显示(白屏),也没有错误,可以帮我看下原因吗?

import pygame #导入pygame库

import sys #需要使用sys的exit()函数

from pygame.locals import *

#定义窗口的分辨率

SCREEN_WIDTH = 700

SCREEN_HEIGHT = 1050

#初始化游戏

pygame.init() #初始化pygame,但出错时不会抛出异常 会返回(成功初始化的模块数,出错的模块数)

screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT],0,32)

#初始化一个面板,有三个参数 1.(width, height) 若未输入为当前屏幕大小 2.标志位 3.位数:用多少位来表现颜色(不需要设置)

pygame.display.set_caption(‘This is first pygame’) #窗口标题

#放入背景图

background = pygame.image.load(‘image/background.jpeg’)

#循环

while True:

#处理游戏退出

for event in pygame.event.get():

#pygame.event.get()方法可以从事件队列得到所有事件的一个列表,我们使用一个循环不断地

#检测每一个事件,一旦我们找到quit事件,我们就退出游戏关闭窗口。

if event.type == pygame.QUIT:

pygame.quit()

exit()

把它们放在同一个文件夹下了,试过把程序和图片放在一个目录下,也试过子文件夹 都不行

我也是同样的问题,只有一个代窗口,没有图形。已经检查过代码没有问题。

如果py文件和图片同一个目录,需要在前面加上 ” ./ ”

例如 “./image/background.jpg”

Traceback (most recent call last):

File “”, line 1, in

pygame.error: Couldn’t open sushiplate.jpg

没有找到图片 你是把图片放到哪里? 我是放到Windows7 桌面 我用的是PythonWin 3.1.2

xishui Post author2011/11/18

很显然,我们要把图片和代码放在同一个目录下。不过如果单纯放在桌面上,有可能会受到中文文件名(如果你的用户名是中文的话)的影响,建议放到纯粹的英文目录下。版本的话,只有安装有合适的pygame,是没有关系的。

博主你好,确定代码无误之后还是存在这个问题:

Traceback (most recent call last):

File “C:\Python27\py.py”, line 20, in

background = pygame.image.load(background_image_filename).convert()

pygame.error: Couldn’t open sushiplate.png

检查了好久问题还是存在,py.py文件和图片都是放在一个文件夹内的,求赐教,谢谢

你的代码有错吧,sushiplate应该是jpg。。。

@xishui:

好吧..表示不知道大神在说什么…自行去google了…

我按照大师的方法实践了一下,不过把背景换了个图片,O(∩_∩)O哈哈~

请教下为什么PYTHON2.6不能运行?是格式问题么?至少需要哪个版本的?

xishui Post author2011/12/01

2.6可以啊,我的就是2.6,请确认安装了python2.6和对应版本的pygame。可以把错误log贴出来看看。

非常感谢楼主送出这么优秀的教程

我是一个新手 请问你用的是什么编辑器

xishui Post author2011/12/01

Vim is my favorite editor ~

also vim is my favorite, could you share your .vimrc and recommand some plugins for vim about python?

I think the best editor is eclipse+Pydev.

在哪里可以下到python2.7对应的版本 ?

>>>

Traceback (most recent call last):

File “C:UsersAdministratorDesktoptry.py”, line 13, in

screen.blit(background,(0,0))

TypeError: argument 1 must be pygame.Surface, not builtin_function_or_method

>>>

为什么会显示这样?我就把第一行改了一下,会影响么?我就改成s=’slumdank.jpg’这样不可以么?

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

没看到你的源代码,但我觉得你应该是这行代码出问题了。看看是不是哪里写错了,比如最后是不是没写上()之类的。

啊。。。。我去,没看时间,过了这么多年了都。。。。囧死了!

哈哈,作者不会介意的

请大神解答……

xishui Post author2011/12/09

当然没什么不可以,不过请先试着把例程运行一遍再修改。我看错误信息,猜是不是“background = ..” 那一行,最后没有加括号?

大神啊,我检查了一下 ,应该没有问题,运行时他出现了窗口,但是没有图片,而且程序就不响应了!是不是因为图片大小的问题呢?

s=’slumdank.jpg’

import pygame

from pygame.locals import*

from sys import exit

pygame.init()

screen=pygame.display.set_mode((640,480),0,32)

background=pygame.image.load(s).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True :

for event in pygame.event.get():

if event.type==QUIT:

exit()

screen.blit(background,(0,0))

x,y=pygame.mouse.get_pos()

x-=mouse_cursor.get_width()/2

y-=mouse_cursor.get_height()/2

screen.blit(mouse_cursor,(x,y))

pygame.display.update()

screen = pygame.display.set_mode((640, 480), 0, 32)这个表示窗口的大小是640*480的,然后0,32 表示什么呢?(请大神解答一下我这个弱智问题……)

其实我是因为大作业要写一个程序,编一个游戏,所以才来学这个的,所以什么都不会(我们只讲了graphics)所以请不要见怪~

@nogerw: 啊??你怎么知道??!!

@rabbitfan: 因。为。我。也。是。电院9班闻于天

楼主 就算不把光标画上去,也会有光标出现的呀 为什么一定要写这句呢“screen.blit(mouse_cursor, (x, y))” ?

xishui Post author2011/12/11

这都开起同学会了啊,挺好,这两天我们同学也聚会了~~

分别回答你的问题:

set_mode的参数,文中也写了,第一个是分辨率,第二个是标志位,含义在表中,可以看本系列第三部分有稍详细一点的,第三个是色深,也就是32位色。

自定义函数退出的话,你可以写成

def gameover():

pygame.quit()

sys.exit()

应该有效。

鼠标的事情,不这么写确实也有光标出现,但那是默认的,比较难看不是么,游戏中我们可能希望光标是一把剑、一只眼睛等等,这时候就需要这样的技术,这里是一条鱼。

最后你的那个程序,除了mouse_image_filename的定义漏了以外,其他都是正确的,如果图片什么的没问题,那可能是你安装的python和pygame有问题了,请试用32位2.6版的python和对应的pygame,一定要配套。

祝你成功!有问题再留言。

@nogerw: 哈哈 我舍友认识你,你们班的

k = pygame.image.load(k).convert()

error: Can’t seek in this data source

大神~~如果我用上面的句法打开gif格式的图片会报错啊 那要怎么打开gif格式的图片呢?

xishui Post author2011/12/13

理论上来说,gif和其他格式的图片没什么区别,看error信息,你确实将k定义成一个gif文件了吗?换两张gif图试试(另外,不要想着直接在pygame中加载能动的gif,绝大多数游戏引擎都不支持,要使用“精灵”来实现)?

@xishui: 算了 我重新找了jpg格式的 (我试了这个确实不能运行gif格式的!)

还有啊~def gameover():

pygame.quit()

sys.exit()

大神你定义的这个函数 随时点击关闭窗口随时都可以关闭么?貌似每次直接点关闭就会出现“ File “C:UsersAdministratorDesktoppython大作页新建文件夹main.py”, line 152, in move

exit()

SystemExit”这样~~,那要怎么合法关闭呢……?

大师。。我直接复制了你的代码运行。运行结果很奇怪:鱼的图片一直处于鼠标的初始位置,而移动鼠标时,会有一个黑色的鼠标光标出现。也就是说鱼一直处于固定的位置,而当我移动鼠标时,程序中显示的是另一个黑色光标的移动。

@xishui: 我的win7也是,退出时卡住,必须强行才能退出。然后试了你建议的那个方法,可以了。。

@halo: 在64位系统上用32位的python和32位的pygame搭配.

建议在screen.blit(background, (0,0)) 进行一次 display.update() 要不然会发现是个黑框框,没有图片神马的。。。

还有就是 #!/usr/bin/python 这句 windows用户就不用加了,

中文的话可以在第一行(必须是第一行)加入#-*-coding:utf8 -*-

你好, 我是刚接触pygame的菜鸟。我按照你的代码并下载了你的两张图片在ubuntu环境下运行了这个hello world。程序能运行但是运行的时候会很卡,有时候甚至会导致ubuntu系统hang住。(ubuntu版本是11.04 ; python 版本是2.7.1+ ;pygame 是1.9.1)。主机是高配置。请问导致这样的现象会是什么原因?按理说这个入门程序是不会导致拥有16G内存的系统hang住的。有点迷惑了,望指点,谢谢 。

非常好的资料~~~非常感谢~~~

忽然发现,今天恰好是这篇帖子发表一周年的日子~~好巧~~

while (True){document.write(‘test’);}

为什么我的总是打不开,试了不下20次了,请教下,

Traceback (most recent call last):

File “C:Documents and SettingsOwner桌面hello.py”, line 22, in

background = pygame.image.load(background_image_filename).convert()

error: Couldn’t open sushiplate

换个全英文路径试试

我用的是python-2.6.6和pygame-1.9.1win32-py2.6

重新安装了一遍软件,现在可以了

Traceback (most recent call last):

File “C:Documents and SettingsOwner桌面勿删hello.py”, line 32, in

exit()

SystemExit

这么多问题?! 博主好耐心, 偶学了python一个多月, 虽然代码还好多看不懂, 不过自个儿敲一遍下来是神马问题也木有~~~好吧, 其实是有问题的, 退不出来”. . . SystemExit. . .”~~(python3.2+pygame1.9.2pre+万恶的vista)

还是要感谢博主的文, 收藏了, 慢慢看~ 🙂

将循环改成最下面的就可以解决了,偶然找得到。http://blog.csdn.net/soasme/article/details/4381737

while True:

#游戏主循环

for event in pygame.event.get():

if event.type == QUIT:

#接收到退出事件后退出程序

exit()

while Ture:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

@pycxz: 将循环改成最下面的就可以解决了,偶然找得到。http://blog.csdn.net/soasme/article/details/4381737

while True:

#游戏主循环

for event in pygame.event.get():

if event.type == QUIT:

#接收到退出事件后退出程序

exit()

while Ture:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

我的环境:win7_64bit python_3.2.3_32bit pygame_1.9.2a0

退出程序一定要先调用pygame.quit(),不然进程会挂住,不知为何。看前面有个ubuntu环境的同学也是这个情况。

前面评论里的的SystemExit是正常现象

sys模块exit()方法的文档是这么说的:Exit the interpreter by raising SystemExit(status).

只调用pygame.quit(),窗口是关闭了,但是解释器进程还在向下执行,一样有错误信息:

Traceback (most recent call last):

File “D:新建文件夹testpygame.py”, line 32, in

screen.blit(background, (0,0))

pygame.error: display Surface quit

退出部分我想还是要这么写:

for event in pygame.event.get():

if event.type == QUIT:

#接收到退出事件后退出程序

pygame.quit()

sys.exit()

或者在while里break

播放音频的模块是不是应该是 pygame.mixer.music ?

to use Chinese comment, you can place a magic comment into the source files either as first or second line in the file, such as:

#coding=utf-8

or

#!/usr/bin/env python

#coding=utf-8

pigsoldier2012/10/13

请问mac下的QUIT事件是用什么快捷键触发的呢 虽然pygame定义的QUIT事件是12 但是貌似除了点击窗口上的关闭外其他快捷键都不能发送出关闭的消息

xishui Post author2012/10/14

穷人米有Mac… 试试Command + Q或者 Command + W?

请问怎么退出啊?一直在无限循环,不知道按什么键可以终止循环啊,python命令窗一直在返回执行的信息,也没办法输命令。只能强制关闭python命令窗。

应该在文章中加一句:不要把代码文件放到中文目录下!

请教大师,我敲了一边代码之后,在我这里一直都是黑框框,背景图片和鼠标图片都没有显示。已经检查过多遍,没有中文目录,图片和代码在一个文件夹内,python2.7 win32和 pygame-1.9.1.win32-py2.7 一致。可就是图片load不上来。

# -*- coding: utf-8 -*-

background_image_filename = 'sushiplate.jpg'

mouse_image_filename = 'fugu.png'

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((640,480),0,32)

pygame.display.set_caption('Hello World!')

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

# pygame.display.update()

screen.blit(background, (0,0))

pygame.display.update()

x,y = pygame.mouse.get_pos()

x-=mouse_cursor.get_width()/2

y-=mouse_cursor.get_height()/2

screen.blit(mouse_cursor, (x,y))

pygame.display.update()

xishui Post author2013/04/05

没看出来么?exit()下面的所有语句,是隶属于while True:的呀,您没有缩进那几行代码,所以代码一直在循环,根本没有执行下面的绘制代码~

我一开始也是一直终止不了,后来把from sys import exit 改为 import sys 就可以了

楼主牛人呀,我是初学者,遇到了问题

1.你程序中的第九行,运行起来错误提示:no module named locals 是我的库没装全么?(python2.7,在IDEL中编写cmd命令下运行) ;

2.在我删除了from pygame.locals import * 以后再运行,提示我图片打不开。图片在其他目录下放着,试着用了图片的完整目录(相对路径,绝对路径都用了)也不行,后来我尝试把图片跟程序放在同一目录下就可以了。

@xishui

import pygame后再from pygame.locals import *是不是有些多余呢?

中文注释会报错,删掉就可以了,感谢作者,这文章真是太棒了

@cqq626:

如果不写from pygame.locals import *,那么line30中if event.type == QUIT:就应该写成if event.type == pygame.QUIT:

为了方便而已

稍微改进一下代码

#!python

import pygame

from pygame.locals import *

background_img = ‘bg.jpg’

mouse_img = ‘mouse.png’

pygame.init()

screen = pygame.display.set_mode((800, 500), 0 , 32)

pygame.display.set_caption(“Hello, World!”)

background = pygame.image.load(background_img).convert()

mouse = pygame.image.load(mouse_img).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

else:

screen.blit(background, (0, 0))

x, y = pygame.mouse.get_pos()

x -= mouse.get_width() / 2

y -= mouse.get_height() / 2

screen.blit(mouse, (x, y))

pygame.display.update()

#! /usr/bin/env python

#coding=utf-8

import pygame

from pygame.locals import *

background_img = ‘bg.jpg’

mouse_img = ‘mouse.png’

pygame.init()

screen = pygame.display.set_mode((800, 500), 0 , 32)

pygame.display.set_caption(“Hello, World!”)

background = pygame.image.load(background_img).convert()

mouse = pygame.image.load(mouse_img).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

exit(0)

screen.fill(0)

screen.blit(background, (0, 0))

x, y = pygame.mouse.get_pos()

x -= mouse.get_width() / 2

y -= mouse.get_height() / 2

screen.blit(mouse, (x, y))

pygame.display.update()

我要说的是…能详细解释一下set_mode()里的第二和第三个参数么。我的教程里没说啊。

我编程基础比较差,请问楼主个问题:

关于 查询命令分离的,

这里代码当中有一行是:

screen = pygame.display.set_mode((640, 480), 0, 32)

按我的理解来看,这行应该是一个 命令,也就是返回值应该是None,但是我试了下这段代码,返回的也就是你说的窗口。这类似于 Image.open(),返回的是个Image,但这个是“打开”动作,所以应该算查询,请问你对 查询命令分离,怎么区分,有什么建议吗?

谁规定了打开不能返回对象的? 不要太死板好不啦…

pygame是包名,display是类名,set_mode是函数名,该函数返回一个display对象…

感谢楼主的分享。

程序运行的时候有个问题,无法关闭,楼主可以详细说下下面的代码吗?

QUIT 不用双引号的?在我的程序里面如果不用会报错,undefined variable:QUIT

if event.type == QUIT:

exit()

我改成这样就可以关闭了

if event.type == pygame.QUIT:

exit()

在win7 中pygame.music不存在,是用pygame.mixer来载入音频的

Traceback (most recent call last):

File “E:/22”, line 14, in

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alphc

AttributeError: ‘pygame.Surface’ object has no attribute ‘convert_alphc’

总是报错,不知道错误在哪,求解?

我跑出來無法執行 跳出這個錯誤

Traceback (most recent call last):

File “C:/Python27/env.py”, line 7, in

import pygame

ImportError: No module named pygame

請問是?

剛新手入門很多都不懂 是否可以加個line 指導一下?

我也刚学,我想是你的pygame没装正确吧,要对应好python和pygame的版本号 以及注意机子的位数(32位还是64位)

写的很好哦!学习了

看了楼主的博客,觉得学到了好多东西,谢谢

楼主,你好幽默啊!

恩,我遇到了一个小白问题,程序运行一切正常,但如何退出呢? 我是win7 64 位系统,貌似只能任务管理器终结了。

改为以下代码就可以了:

if event.type == pygame.QUIT:

pygame.quit()

exit()

补充:我用的是IDLE

谢谢,原来关闭一直会显示程序未响应,现在好了。

原来在自己的机子上运行没问题,但是在另一台XP机子上运行时出现:File is not a Windows BMP file,提示在图片加载background = pygame.image.load(background_img).convert()时有问题。

error: video system not initialized

这是神马节奏??!!

求指点

最近对pygame有点感兴趣, 今天部好环境试了下。

程序跑起来之后抛出:

pygame.error: File is not a Windows BMP file

异常。

请问博主,如何为pygame添加其他格式图片的支持?

我的操作系统是centos6.5。

感谢

xishui Post author2014/06/28

这个恐怕颇为困难,需要修改源码吧?我觉得事先转一下格式不是很好么?还能减小体积。

完美强迫症一下,可以用 pygame.mouse.set_visible(False) 隐藏鼠标图画

赞 哈哈哈 可以隐藏

我直接复制试了一下,但是是黑屏,求教

# -*- coding: utf-8 -*-

background_image_filename=’sushiplate.jpg’

mouse_image_filename=’fugu.ng’

import pygame

from pygame.locals import *

import sys

pygame.init()

screen= pygame.display.set_mode((640,480),0,32)

pygame.display.set_caption(‘Hello,World!’)

background=pygame.image.load(background_image_filename).convert()

mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pyagme.event.get():

if event.type==pygame.QUIT:

pygame.quit()

exit()

screen.blit(background,(0,0))

x, y=pygame.mouse.get_pos()

x-=mouse_cursor.get_width()/2

y-=mouse_cursor.get_height()/2

screen.blit(mouse_cursor,(x,y))

pygame.display.update()

求解为什么黑屏,关闭时还会让pythonwin关闭

while 后有缩进

但是编译的时候问题在background=pygame.image.load(background_image_filename).convert()上,到这里逐步编译会发生如下结果exception raised while running the script

老大 按你的程序弄得 screen.blit(bacground,(0,0)) 后面也加了 pygame.display.update() 为什么我一运行就是黑背景啊

而且程序就会无响应 win7 64位

弄好了 另外我想知道 怎么加一句 回车键就关闭程序 初学者

对于代码,我有几点建议,同时也是可以解决上面及格网友的问题:

1.把 “QUIT” 改成-数字- “12”

2.在文件路径前加个 “r”

4.对于 ”回车键就关闭程序“ 这个问题可以用一种方法来获取事件的值,然后可以在”for event in pygame.event.get()” 后加上 “if event.type == (事件的值)” 来控制,

至于实现方法,如下:

while True:

for event in pygame.event.get():

if event.type :

print event.type

然后就可以在程序里敲回车键,看其打印出来的数值就行了,

3.建议把”form sys import exit”去掉(不去掉也无所谓~)

你好,我运行时有一个问题……鱼的图片一直处于固定的位置,而移动鼠标时,会有一个另一个黑色的鼠标光标出现。也就是鱼的图片并没有成为鼠标光标。

下面是我自己敲的代码,到底是哪里出问题了,谢谢~

a=’sushiplate.jpg’

mouse_image_filename=’fugu.png’

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen=pygame.display.set_mode((640,480),0,32)

pygame.display.set_caption(‘homework’)

background=pygame.image.load(a).convert()

mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

mainloop=True

while mainloop:

## for event in pygame.event.get():

#####if event.type==pygame.QUIT:

####### mainloop=False

#######pygame.quit()

###screen.blit(background,(0,0))

###x,y=pygame.mouse.get_pos()

###x-=mouse_cursor.get_width()/2

### y-=mouse_cursor.get_height()/2

###screen.blit(mouse_cursor,(x,y))

### pygame.display.update

用#表示缩进了……缩进应该没有问题,因为运行的时候没有报错

自己检查出来了 ………………羞耻……不该那么早问的……

thanks a lot !!!!!

我想在python3上安装pygame,mac osx 10下

——————————————

• sudo port install smpeg

• sudo port install PORTMIDI

• sudo port install JPEG

————————————

Hunting dependencies…

Framework SDL not found

Framework SDL_ttf not found

Framework SDL_image not found

Framework SDL_mixer not found

Framework smpeg not found

PNG : not found

JPEG : found

SCRAP : not found

PORTMIDI: not found

Framework CoreMidi found

If you get compiler errors during install, doublecheck

the compiler flags in the “Setup” file.

——————————————————————

bla~ bla~

p.macosx-10.6-x86_64-3.4/src/gfxdraw.o

unable to execute /usr/bin/gcc-4.2: No such file or directory

error: command ‘/usr/bin/gcc-4.2’ failed with exit status 1

:pygame-1.9.1release zenghb$ cd ~

:~ zeng$ /usr/bin/

-bash: /usr/bin/: is a directory

可以给一些指导吗,谢谢

qq463732273

exit()前应该加pygame.quit(),可以解决退出时的问题

pygame.error: counldn’t open sushiplate.jpg

第二天还是这个样子~~我的天!!!求助

我也一样。。。不知道是什么原因。。。

运行时显示错误:error: Couldn’t open chouyu.png。昨天还能正常打开,今天就打开失败了,麻烦楼主看看。。。。。

xishui Post author2015/04/20

图片文件不见了?

今天又能正常打开了,真是奇怪。。。。

同样的问题在我打开一个音乐文件的时候也出现过,明天头一天还能打开那个文件,第二天就打不开了,而且我肯定没把那个文件移动到别处。

做个标记,以后学习!

background_image_filename=’sushiplate.jpg’

mouse_image_filename=’fugu.png’

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen=pygame.display.set_mode((640,480),0,32)

pygame.display.set_caption(“Hello, World!”)

background=pygame.image.load(background_image_filename).convert()

mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type==QUIT:

exit()

screen.blit(background, (0,0))

x,y=pygame.mouse.get_pos()

x-=mouse_cursor.get_width()/2

y-=mouse_cursor.get_height()/2

screen.blit(mouse_cursor,(x,y))

pygame.display.update()

这是全部代码,不知道哪里缩进有问题,总显示IndentationError: unindent does not match any outer indentation level。查了全部字符,也没有不正常的呀,请楼主帮忙找一下~谢谢

background_image_filename=’sushiplate.jpg’

mouse_image_filename=’fugu.png’

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen=pygame.display.set_mode((640,480),0,32)

pygame.display.set_caption(“Hello, World!”)

background=pygame.image.load(background_image_filename).convert()

mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type==QUIT:

exit()

screen.blit(background, (0,0))

x,y=pygame.mouse.get_pos()

x-=mouse_cursor.get_width()/2

y-=mouse_cursor.get_height()/2

screen.blit(mouse_cursor,(x,y))

pygame.display.update()

刚才的格式不对,不好意思

background_image_filename=’sushiplate.jpg’

mouse_image_filename=’fugu.png’

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen=pygame.display.set_mode((640,480),0,32)

pygame.display.set_caption(“Hello, World!”)

background=pygame.image.load(background_image_filename).convert()

mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

while True:

****for event in pygame.event.get():

********if event.type==QUIT:

************exit()

****screen.blit(background, (0,0))

****x,y=pygame.mouse.get_pos()

****x-=mouse_cursor.get_width()/2

****y-=mouse_cursor.get_height()/2

****screen.blit(mouse_cursor,(x,y))

****pygame.display.update()

不好意思啊,太蠢了,是这样子的

不错的文章,内容博学多才.禁止此消息:nolinkok@163.com

不错的文章,内容完美无缺.禁止此消息:nolinkok@163.com

不错的文章,内容出口成章.禁止此消息:nolinkok@163.com

如何实现这个窗口可变大小呢,resizable怎么用 ,求告知

后边有篇文章讲这个

我本来都看到第8篇了,突然发现第一篇还有问题没有解决。在鱼的那个矩形除了鱼之外的部分怎么没有显示,和透明度有关吗。请各位赐教啊

xishui Post author2015/10/04

应该如此,要么用了不透明的背景,要么没有加载alpha通道

pygame.error: Couldn’t open sunshiplate.jpg 图片和程序放在同一个目录下了,还是不行啊 求楼主解答

pygame.error: Couldn’t open sunshiplate.jpg 图片和程序放在同一个目录下了,还是不行啊 求楼主解答

background_image_filename = ‘sunshiplate.jpg’

mouse_miage_filename = ‘fugu.png’

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((640,480),0,32)

pygame.display.set_caption(“Hello,World!”)

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_miage_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background,(0,0))

x,y = pygame.mouse.get_pos()

x-= mouse_cursor.get_width() / 2

y-= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor,(x,Y))

pygame.display.update()

xishui Post author2015/10/09

那么,您是在什么地方执行这个py文件的呢?直接双击?还是cmd到所在目录下然后”python xxxx.py”的?

在cmd里面执行的,一直报这个错。照片和代码是在同一个文件夹下呀

你好,在第一部分我有两个问题:(1)我用的是windows平台和pythonwin工具,直接执行脚本的时候 from pygame.locals import *这句出错了,意思是没有locals这个模块;但是我在交互窗口输入这句话可以执行,之后再执行脚本的 时候就通过了。网上没有搜到相关的解释,烦劳你给解释下。(2)窗口不知道如何正常关闭,如果点击小“x”退出,整个程序就会异常退出。不知道你设置的那个QUIT是在什么事件的标志呢?

我建议你换pycharge这个IDE,pythonwin不如pycharge强大。

能够自动补全一些符合,很方便。

读取的图片不需要文件地址吗?电脑怎么找到图片在哪里

把图片和文件放在同一个文件夹下就可以了

博主很棒,绘制新的光标 怎么把之前的除掉呢

这么好的文章我今天才看到,刚看了一篇就忍不住来赞,感谢博主,文章注释写的很详细,很适合我们初学的人学习,博客的风格也很简洁,我很喜欢。

新手求助

看了第一节之后试着跟着做,然后在画面上显示了一个背景一个光标,还有几个按键,几个对话框(想做一个类似游戏界面的东西)然后,按键什么的看了后面几节做出来了,也可以用

但是有个很大的问题是当画面显示的东西多的时候老是有闪烁的现象。一开始我以为是我没能及时跟新,于是我每一次blit的后面都加了句pygame.display.flip(),发现还是不行,于是我换了pygame.display.update(),结果还是没解决闪烁问题,求解

flip()好像是和双缓冲显示配套的吧,可能不太好用……抱歉,其实我也是不太懂……

xishui Post author2016/07/21

双缓冲还是很好用的一个技术,放心现在是块显卡都支持,只管用吧。

在最上面写上一行#-*-coding:utf-8-*-就可以了,主要是不支持中文的缘故

我说的是输入注释报错加上#-*-coding:utf-8-*-就可以了。。。。

我说的是输入注释报错加上#-*-coding:utf-8-*-就可以了。。。。

博主,在Pygame 1.9.2 Pre 中,pygame.music貌似改成了pygame.mixer_music了,楼主能否查证一下,谢谢!

截图: http://i2.piimg.com/524586/4cef0c8ff0bae78b.png

我退出时,显示

Traceback (most recent call last):

File “E:\python\pygame\hello_word.py”, line 19, in

exit();

SystemExit

不知道是什么原因?

非常感谢你的博客,希望能给初学者解释的更清楚些,比如我在使用您代码时候,就遇到一个问题,我不知道该把图片放在哪里,呵呵。这是真的。因为没接触过。后来翻看下面的评论看见一位朋友介绍说,把图片放在和代码相同的文件夹即可。(如果图片很多,该怎么归类才比较有顺序,还是一股脑都放在一个文件夹下呢)

谢谢你。

xishui Post author2016/09/16

可以根据类型分几个文件夹,文件名里写路径。而且估计pyyhon中读取gzip的方法也可能能用(只是猜测),这样就更好看了。

第一次学习,换了背景图,确实能运行,但是还不懂原理

支持楼主

请教一个问题:在代码:screen = pygame.display.set_mode((640, 480), 0, 32),如果报mode((640, 480), 0, 32)更改为更高的分辨率,如mode((1280, 960), 0, 32)的话,如何使背景图片能够自动填充整个背景? 也就是说,像我们打开浏览器一样,背景随鼠标拖动窗口大小的改变而改变。其用到的函数和方法是哪些?谢谢。

xishui Post author2016/09/16

系列读下去,就会发现了

谢谢楼主,楼主好人,你的文章很好。

wl12314798992017/01/12

Couldn’t set console screen info

谢谢楼主的讲解!非常精彩!

受教了!

Traceback (most recent call last):

File “C:/Users/Hacker/Desktop/程序/pygame.py”, line 6, in

import pygame

File “C:/Users/Hacker/Desktop/程序\pygame.py”, line 7, in

from pygame.locals import *

ImportError: No module named locals

#!/usr/bin/env python

background_image_filename = ‘sushiplate.jpg’

mouse_image_filename = ‘fugu.png’

import pygame

from pygame.locals import *

from sys import exit

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)

pygame.display.set_caption(“Hello, World!”)

background = pygame.image.load(background_image_filename).convert()

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == QUIT:

exit()

screen.blit(background, (0,0))

x, y = pygame.mouse.get_pos()

x-= mouse_cursor.get_width() / 2

y-= mouse_cursor.get_height() / 2

screen.blit(mouse_cursor, (x, y))

pygame.display.update()

[root@localhost python]# python pygame_hello_world.py

Traceback (most recent call last):

File “pygame_hello_world.py”, line 22, in

background = pygame.image.load(background_image_filename).convert()

pygame.error: File is not a Windows BMP file

linux下提示不是BMP

图片和代码是放在一起的

楼主求教:

执行代码出现如下错误,我用load总是告诉我找不到文件,那个failed loading libjpeg,dylib:dlopen(linjpeg,dylib是啥意思?

background = pygame.image.load(background_image_filename).convert()

pygame.error: Failed loading libjpeg.dylib: dlopen(libjpeg.dylib, 2): image not found

应该是你没下载或没放在一个目录里(和程序一个目录)。

Traceback (most recent call last):

File “C:/Users/����/Desktop/Pythontest/test2/test2.1.py”, line 19, in

if event.type() == 12:

TypeError: ‘int’ object is not callable

请问楼主这是什么问题 运行的时候 黑屏 闪退 谢谢!

程序不能直接运行,运行条件为python2.7.11 ,pygame 1.92

if event.type == QUIT: #这句出错

修改为 if event.type == ‘QUIT’: #可以运行

大神,我直接双击运行文件,但是那个窗口一闪而过,怎么办,我是新手,刚接触,不知道怎么办,应该怎么样才能运行起来

作者您好,我自己修改了一下源码,让这个小demo能支持鼠标或者键盘控制,一开始用键盘控制,当控制光标移到边缘在移动会出现图片移到窗口外面,我限制了一下x和y的值来控制光标不到外面。但是用鼠标控制的时候,用之前限制数值的方法就失效了,请问该怎么处理?

while True:

pygame.mouse.set_visible(False)

screen.blit(shubiao, (0, 0))

if config.control[‘move’]==’key’:

for event in pygame.event.get():

if event.type == QUIT:exit()

if event.type == KEYDOWN:

if event.key in move:move[event.key] = 1

elif event.type == KEYUP:

if event.key in move:move[event.key] = 0

if (1434-99>x>=0 and 1030-118>y>=0):

x -= move[K_LEFT]

x += move[K_RIGHT]

y -= move[K_UP]

y += move[K_DOWN]

else:

x=x+1 if x<1434-99 else 1433-99

y=y+1 if ybackground.get_width() /2) >0.0 else xx

yy -= background.get_height() / 2 if (1030-118>background.get_height() / 2) >0.0 else yy

screen.blit(background, (xx, yy))

pygame.display.update()

修改一下之前评论的代码格式

while True:

pygame.mouse.set_visible(False)

screen.blit(shubiao, (0, 0))

if config.control[‘move’]==’key’:

for event in pygame.event.get():

if event.type == QUIT:exit()

if event.type == KEYDOWN:

if event.key in move:move[event.key] = 1

elif event.type == KEYUP:

if event.key in move:move[event.key] = 0

if (1434-99>x>=0 and 1030-118>y>=0):

x -= move[K_LEFT]

x += move[K_RIGHT]

y -= move[K_UP]

y += move[K_DOWN]

else:

x=x+1 if x<1434-99 else 1433-99

y=y+1 if ybackground.get_width() /2) >0.0 else xx

yy -= background.get_height() / 2 if (1030-118>background.get_height() / 2) >0.0 else yy

screen.blit(background, (xx, yy))

pygame.display.update()

python 3.5.2 pygame 1.9.3

可以运行,但窗口无法正常关闭

建议在exist()前面加上pygame.quit()

您好我想问下就是比如其中的一些方法 例如pygame.display.set_caption()这些方法我应该如何查询知道该模块中的类中有这个方法。感觉直接help中也无法查询到就很纠结。而且比如在检测案件的返回值的时候也不知道要检测什么按键怎么输入检测按键名,这些东西的查询不知道何处查询 谢谢指导

您好,请问会不会更新3.x的教程呢

非常感谢

xishui Post author2017/11/22

应该不会啦,真是抱歉

哈哈,为啥我把分辨率调大了以后那些多余空间,鱼经过的时候会留下无数重影。。。。不是每次都重绘了么

为什么 screen.blit(background, (0,0))这句要在循环里?

您好,博主,首先谢谢你的分享。

我是第一次用pygame的纯新手,不过还好有一些Javaweb开发的经验,我遇到了一个问题,就是点击X关闭窗口时,没有效果,然后鼠标移到上面发现图片也没有跟着走了,然后我百度了pygame点X不退出的原因,发现exit()方法只是让程序终止,如果需要pygame的窗口关闭的话,需要调用pygame.quit(),但是您的代码并没有,希望可以改正。

另外因为我是Python初学者,所以一开始我把pygame.quit()放到了exit()方法下,所以还是错误的,后来才想到其中的原因,所以我的代码和注释是这么写的:

for event in pygame.event.get():

# 接收到退出指令

if event.type == pygame.QUIT:

# pygame窗口关闭

pygame.quit()

# 程序终止

exit()

image.load.

can not found load in image

不知道要下载哪个image里的load了。没有单单写load的,。。。。

你好,为什么我的窗口无法关闭

background_image_filename=”sushiplate.jpg”

mouse_image_filename=”fugu.png”

import pygame

pygame.init()

screen=pygame.display.set_mode((640,480),0,32)

pygame.display.set_caption(“Hello World!”)

background=pygame.image.load(background_image_filename).convert()

mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

else:

screen.blit(background,(0,0))

x,y=pygame.mouse.get_pos()

x-=mouse_cursor.get_width()/2

y-=mouse_cursor.get_height()/2

screen.blit(mouse_cursor,(x,y))

pygame.display.update()

改进了下代码,在py3.6下能够运行达到目的

谢谢你的教程!

在一个公众号看到了这篇文章,所以是授权过吗?

为什么会提示打不开sushiplate这个图片

博主好,

初学pygame, 用pygame写游戏, 启动时总有一个毫秒级别时间的图片或自定义背景色的加载时间, 启动时总是先闪一下原来的黑色背景, 这个问题不知道大家注意过吗? 初步判断应该是pygame库的天生问题, 现在我用的是1.9.4(当前时间是19年5月). 不知道博主有没有这个问题的解法?

from pygame.locals import* 显示错误,这咋办啊

楼主我想问下 macos上不能显示图片怎么解决

用miniconda吧,基于brew 安装有问题,可以基于官网安装或者直接用conda

打卡,第一个程序运行成功。mac上配了一天环境。mac上有问题推荐用miniconda

楼主你好,我是用pycharm写的这段代码。

if event.type ==QUIT:

这里的QUIT一直红色报错。但是呢,运行是正常的,能退出,加了引号就不能关闭窗口。

新人有点强迫症,这个能处理掉不,似乎不是导入包的问题?

感谢 这个应该是最棒的pygame 新手教程了

不明白的是下载的图片放哪里呢?

为什么在运行的时候总是反馈无法打开图片文件呢?Couldn’t open sushiplate.jpg

回报大佬,我发现了from pygame.locals import *这条命令其实并不需要,只要把QUIT改成pygame.QUIT好

回报大佬我发现from pygame.locals import *这条命令其实可以不需要,只要更改QUIT为pygame.QUIT就好了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值