pygame——入门1

来源:互联网 发布:流程优化岗面试题目 编辑:程序博客网 时间:2024/06/09 21:18

虽然是个程序小白,但还是想记录一下从今以后自己写过的代码,说不定就会对谁有所帮助呢~

游戏程序设计课程作业要求写5000行代码,简直是遥遥无期。
虽然python相对于其它主流语言来说并不适合写游戏,但被c++深深伤害到的我实在觉得python才是真爱,便装了pygame开始学习起来。

参考了大大精选翻译的《Beginning Game Development with Python and Pygame –From Novice to Professional》,http://eyehere.net/2011/python-pygame-novice-professional-1/,先尝试了pygame的hello world程序:放置一张背景图片,将光标变成一张png图片并控制移动它。

稍微做了一点点小修改,去掉了窗口的边框并隐藏了原本的光标。

结果图如下:
左边的花朵就是光标图案

其实主要是觉得选的图片太好看了所以有了想记录的冲动TUT

当光标图案到达边界时总会出现卡顿现象是咋回事呢

import pygamefrom pygame.locals import *from sys import exitbackground_image_filename='back1.jpg'mouse_image_filename='back2.png'pygame.init()###set NOFRAME to hide the frame of the window###screen=pygame.display.set_mode((595,371),NOFRAME,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()    ###hide cursor###    pygame.mouse.set_visible(False)     x-=mouse_cursor.get_width()/2    y-=mouse_cursor.get_height()/2    screen.blit(mouse_cursor,(x,y))    pygame.display.update()

初试pygame的小程序~具体注释请参看之前的链接~

0 0