[python]pygame游戏开发学习(三)
来源:互联网 发布:手机淘宝设置 店铺分类 编辑:程序博客网 时间:2024/04/27 14:05
今天下午把代码全部重写了一遍,因为之前的太乱,变量到处都是。。。
重写之后用了类和精灵组
功能基本实现
#This editon has been modified carefully#And still I hope you will love it#Code can also be romantic# 2015/11/24####################################################### To import some basic partsimport pygamefrom sys import exitfrom random import randint# To define a new hero classclass Hero(pygame.sprite.Sprite):def __init__(self,init_pos,Hero_Surface): #pos(200,355)pygame.sprite.Sprite.__init__(self)self.image=Hero_Surface #!!! Errorself.pos=init_posself.surface=Hero_Surfaceself.rect=self.image.get_rect()self.rect.topleft=init_posself.speed=2.5def move(self, offset):x=self.rect.left+ offset[pygame.K_RIGHT]-offset[pygame.K_LEFT]y=self.rect.top+offset[pygame.K_DOWN]-offset[pygame.K_UP]if x<0:self.rect.left=0elif x>230:self.rect.left=230else:self.rect.left=xif y<0:self.rect.top=0elif y>325:self.rect.top=325else:self.rect.top=y# To initialize the gameSCREEN_WIDTH=640SCREEN_HIGH=480Background=pygame.image.load('resource/image/Background1.png')Running1=pygame.image.load('resource/image/Running1.png')Running2=pygame.image.load('resource/image/Running2.png')Running3=pygame.image.load('resource/image/Running3.png')Running4=pygame.image.load('resource/image/Running4.png')Hero_Surface=[Running1,Running2]#Game onpygame.init()pygame.display.set_caption('The Otter\'s Adventure')screen=pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HIGH])offset={pygame.K_DOWN:0,pygame.K_UP:0,pygame.K_LEFT:0,pygame.K_RIGHT:0}hero=Hero((200,355),Hero_Surface[0])ANIMATE_CYCLE=30ticks=0while True:screen.blit(Background,(0,0))if ticks>=ANIMATE_CYCLE:ticks=0hero.image=Hero_Surface[ticks//(ANIMATE_CYCLE//2)]screen.blit(hero.image,hero.rect)ticks+=1pygame.display.update()for event in pygame.event.get():if event.type==pygame.QUIT:pygame.quit()exit()elif event.type==pygame.KEYDOWN:if event.key in offset:offset[event.key]=hero.speedelif event.type==pygame.KEYUP:if event.key in offset:offset[event.key]=0hero.move(offset)
0 0
- [python]pygame游戏开发学习(三)
- [python]pygame游戏开发学习(一)
- [python]pygame游戏开发学习(二)
- Pygame (Python 游戏开发)教程
- 学习Python,学习Pygame(三)
- Python--写游戏pygame入门三(桌面保护)
- python之pygame游戏play the ball(三)
- Python和Pygame游戏开发指南(1)MOMERY PUZZLE
- <Python和Pygame游戏开发指南>1
- pygame编写的坦克游戏(三)
- Python游戏:PyGame
- Pygame、Python五子棋游戏
- Python:pygame游戏编程之旅三(玩家控制的小球)
- Python:pygame游戏编程之旅三(玩家控制的小球)
- pygame开发2048游戏
- python实战游戏开发——安装pygame
- 学习Python,学习Pygame(四)
- Pygame开发Python小游戏
- 欢迎使用CSDN-markdown编辑器
- YumRepo Error: All mirror URLs are not using问题解决
- 散列表
- Python实例学习(1) - IP扫描器和163邮箱发信链接
- YumRepo Error: All mirror URLs are not using ftp, http[s] or file. Eg. </HTML>/ 标签: yum 2014-05-26
- [python]pygame游戏开发学习(三)
- RHEL6 64bit下更改YUM配置。yum this system is not registered with rhn的解决办法
- YUM更换源(1)--yum找不到安装包 2013-01-18 20:08 8687人阅读 评论(1) 收藏 举报 分类: linux(70) 公司提供的CentOS VM中,/etc/yum.r
- 文章标题
- 运行时 #import <objc/runtime.h>
- C++之设计模式列表简括
- 机器学习笔记_ 最大熵模型
- 博客积分规则
- C++之定义模板的demo