├── resources ├── font │ ├── font.png │ └── font.fnt ├── image │ ├── shoot.png │ ├── gameover.png │ ├── background.png │ ├── shoot_background.png │ ├── shoot_background.pack │ └── shoot.pack └── sound │ ├── bullet.mp3 │ ├── bullet.wav │ ├── button.mp3 │ ├── game_over.mp3 │ ├── game_over.wav │ ├── get_bomb.mp3 │ ├── out_porp.mp3 │ ├── use_bomb.mp3 │ ├── achievement.mp3 │ ├── enemy1_down.mp3 │ ├── enemy1_down.wav │ ├── enemy2_down.mp3 │ ├── enemy2_down.wav │ ├── enemy3_down.mp3 │ ├── enemy3_down.wav │ ├── game_music.mp3 │ ├── game_music.wav │ ├── get_double_laser.mp3 │ └── big_spaceship_flying.mp3 ├── .gitattributes ├── README.md ├── gameRole.py ├── .gitignore └── mainGame.py /resources/font/font.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/font/font.png -------------------------------------------------------------------------------- /resources/image/shoot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/image/shoot.png -------------------------------------------------------------------------------- /resources/sound/bullet.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/bullet.mp3 -------------------------------------------------------------------------------- /resources/sound/bullet.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/bullet.wav -------------------------------------------------------------------------------- /resources/sound/button.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/button.mp3 -------------------------------------------------------------------------------- /resources/image/gameover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/image/gameover.png -------------------------------------------------------------------------------- /resources/sound/game_over.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/game_over.mp3 -------------------------------------------------------------------------------- /resources/sound/game_over.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/game_over.wav -------------------------------------------------------------------------------- /resources/sound/get_bomb.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/get_bomb.mp3 -------------------------------------------------------------------------------- /resources/sound/out_porp.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/out_porp.mp3 -------------------------------------------------------------------------------- /resources/sound/use_bomb.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/use_bomb.mp3 -------------------------------------------------------------------------------- /resources/image/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/image/background.png -------------------------------------------------------------------------------- /resources/sound/achievement.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/achievement.mp3 -------------------------------------------------------------------------------- /resources/sound/enemy1_down.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/enemy1_down.mp3 -------------------------------------------------------------------------------- /resources/sound/enemy1_down.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/enemy1_down.wav -------------------------------------------------------------------------------- /resources/sound/enemy2_down.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/enemy2_down.mp3 -------------------------------------------------------------------------------- /resources/sound/enemy2_down.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/enemy2_down.wav -------------------------------------------------------------------------------- /resources/sound/enemy3_down.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/enemy3_down.mp3 -------------------------------------------------------------------------------- /resources/sound/enemy3_down.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/enemy3_down.wav -------------------------------------------------------------------------------- /resources/sound/game_music.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/game_music.mp3 -------------------------------------------------------------------------------- /resources/sound/game_music.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/game_music.wav -------------------------------------------------------------------------------- /resources/image/shoot_background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/image/shoot_background.png -------------------------------------------------------------------------------- /resources/sound/get_double_laser.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/get_double_laser.mp3 -------------------------------------------------------------------------------- /resources/sound/big_spaceship_flying.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kill-Console/PythonShootGame/HEAD/resources/sound/big_spaceship_flying.mp3 -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PythonShootGame 2 | 3 | A simple shoot game written in Python. 4 | 5 | # Introduction 6 | 7 | This project only include two simple .py files: 8 | 9 | 1. mainGame.py: The initialization and main loop of the game. 10 | 2. gameRole.py: Class of the game role. 11 | 12 | # Requirement 13 | 14 | 1. Python 2.7 15 | 2. Python-Pygame 16 | 17 | # How To Start Game 18 | 19 | ```bash 20 | $ python mainGame.py 21 | ``` 22 | 23 | # License 24 | GPL 25 | 26 | # Screeshot 27 | 28 | ![](http://s2.postimg.org/728c1wy4p/Screenshot_5.png) 29 | 30 | ![](http://s30.postimg.org/fflxcv9ld/Screenshot_6.png) 31 | 32 | # Doc 33 | [使用Pygame制作微信打飞机游戏PC版](https://www.cnblogs.com/dukeleo/p/3339780.html) (a Chinese startup) 34 | -------------------------------------------------------------------------------- /resources/image/shoot_background.pack: -------------------------------------------------------------------------------- 1 | 2 | shoot_background.png 3 | format: RGBA8888 4 | filter: Nearest,Nearest 5 | repeat: none 6 | background 7 | rotate: false 8 | xy: 0, 75 9 | size: 480, 852 10 | orig: 480, 852 11 | offset: 0, 0 12 | index: -1 13 | shoot_copyright 14 | rotate: false 15 | xy: 480, 702 16 | size: 441, 225 17 | orig: 441, 225 18 | offset: 0, 0 19 | index: -1 20 | game_loading1 21 | rotate: false 22 | xy: 0, 37 23 | size: 186, 38 24 | orig: 186, 38 25 | offset: 0, 0 26 | index: -1 27 | game_loading3 28 | rotate: false 29 | xy: 480, 664 30 | size: 186, 38 31 | orig: 186, 38 32 | offset: 0, 0 33 | index: -1 34 | game_loading2 35 | rotate: false 36 | xy: 0, 0 37 | size: 186, 37 38 | orig: 186, 37 39 | offset: 0, 0 40 | index: -1 41 | btn_finish 42 | rotate: false 43 | xy: 186, 27 44 | size: 150, 48 45 | orig: 150, 48 46 | offset: 0, 0 47 | index: -1 48 | game_loading4 49 | rotate: false 50 | xy: 480, 636 51 | size: 114, 28 52 | orig: 114, 28 53 | offset: 0, 0 54 | index: -1 55 | -------------------------------------------------------------------------------- /resources/font/font.fnt: -------------------------------------------------------------------------------- 1 | info face="Deeko Comic Regular" size=40 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=0,0 2 | common lineHeight=26 base=27 scaleW=512 scaleH=512 pages=1 packed=0 3 | page id=0 file="font.png" 4 | chars count=11 5 | char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=27 xadvance=12 page=0 chnl=0 6 | char id=52 x=0 y=0 width=30 height=40 xoffset=1 yoffset=-7 xadvance=30 page=0 chnl=0 7 | char id=51 x=30 y=0 width=27 height=38 xoffset=1 yoffset=-5 xadvance=27 page=0 chnl=0 8 | char id=88 x=57 y=0 width=30 height=34 xoffset=1 yoffset=-5 xadvance=30 page=0 chnl=0 9 | char id=56 x=87 y=0 width=33 height=34 xoffset=1 yoffset=-7 xadvance=34 page=0 chnl=0 10 | char id=57 x=120 y=0 width=30 height=33 xoffset=1 yoffset=-4 xadvance=30 page=0 chnl=0 11 | char id=50 x=150 y=0 width=29 height=33 xoffset=1 yoffset=-3 xadvance=30 page=0 chnl=0 12 | char id=48 x=179 y=0 width=27 height=31 xoffset=1 yoffset=-3 xadvance=28 page=0 chnl=0 13 | char id=54 x=206 y=0 width=31 height=31 xoffset=1 yoffset=0 xadvance=32 page=0 chnl=0 14 | char id=53 x=237 y=0 width=35 height=31 xoffset=1 yoffset=0 xadvance=36 page=0 chnl=0 15 | char id=49 x=272 y=0 width=6 height=31 xoffset=1 yoffset=-5 xadvance=7 page=0 chnl=0 16 | char id=55 x=278 y=0 width=33 height=30 xoffset=1 yoffset=-1 xadvance=34 page=0 chnl=0 17 | kernings count=-1 18 | -------------------------------------------------------------------------------- /gameRole.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Sep 11 16:36:03 2013 4 | 5 | @author: Leo 6 | """ 7 | 8 | import pygame 9 | 10 | SCREEN_WIDTH = 480 11 | SCREEN_HEIGHT = 800 12 | 13 | TYPE_SMALL = 1 14 | TYPE_MIDDLE = 2 15 | TYPE_BIG = 3 16 | 17 | # 子弹类 18 | class Bullet(pygame.sprite.Sprite): 19 | def __init__(self, bullet_img, init_pos): 20 | pygame.sprite.Sprite.__init__(self) 21 | self.image = bullet_img 22 | self.rect = self.image.get_rect() 23 | self.rect.midbottom = init_pos 24 | self.speed = 10 25 | 26 | def move(self): 27 | self.rect.top -= self.speed 28 | 29 | # 玩家类 30 | class Player(pygame.sprite.Sprite): 31 | def __init__(self, plane_img, player_rect, init_pos): 32 | pygame.sprite.Sprite.__init__(self) 33 | self.image = [] # 用来存储玩家对象精灵图片的列表 34 | for i in range(len(player_rect)): 35 | self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha()) 36 | self.rect = player_rect[0] # 初始化图片所在的矩形 37 | self.rect.topleft = init_pos # 初始化矩形的左上角坐标 38 | self.speed = 8 # 初始化玩家速度,这里是一个确定的值 39 | self.bullets = pygame.sprite.Group() # 玩家飞机所发射的子弹的集合 40 | self.img_index = 0 # 玩家精灵图片索引 41 | self.is_hit = False # 玩家是否被击中 42 | 43 | def shoot(self, bullet_img): 44 | bullet = Bullet(bullet_img, self.rect.midtop) 45 | self.bullets.add(bullet) 46 | 47 | def moveUp(self): 48 | if self.rect.top <= 0: 49 | self.rect.top = 0 50 | else: 51 | self.rect.top -= self.speed 52 | 53 | def moveDown(self): 54 | if self.rect.top >= SCREEN_HEIGHT - self.rect.height: 55 | self.rect.top = SCREEN_HEIGHT - self.rect.height 56 | else: 57 | self.rect.top += self.speed 58 | 59 | def moveLeft(self): 60 | if self.rect.left <= 0: 61 | self.rect.left = 0 62 | else: 63 | self.rect.left -= self.speed 64 | 65 | def moveRight(self): 66 | if self.rect.left >= SCREEN_WIDTH - self.rect.width: 67 | self.rect.left = SCREEN_WIDTH - self.rect.width 68 | else: 69 | self.rect.left += self.speed 70 | 71 | # 敌人类 72 | class Enemy(pygame.sprite.Sprite): 73 | def __init__(self, enemy_img, enemy_down_imgs, init_pos): 74 | pygame.sprite.Sprite.__init__(self) 75 | self.image = enemy_img 76 | self.rect = self.image.get_rect() 77 | self.rect.topleft = init_pos 78 | self.down_imgs = enemy_down_imgs 79 | self.speed = 2 80 | self.down_index = 0 81 | 82 | def move(self): 83 | self.rect.top += self.speed -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /resources/image/shoot.pack: -------------------------------------------------------------------------------- 1 | 2 | shoot.png 3 | format: RGBA8888 4 | filter: Nearest,Nearest 5 | repeat: none 6 | enemy3_down6 7 | rotate: false 8 | xy: 0, 747 9 | size: 166, 261 10 | orig: 166, 261 11 | offset: 0, 0 12 | index: -1 13 | enemy3_hit 14 | rotate: false 15 | xy: 166, 750 16 | size: 169, 258 17 | orig: 169, 258 18 | offset: 0, 0 19 | index: -1 20 | enemy3_n1 21 | rotate: false 22 | xy: 335, 750 23 | size: 169, 258 24 | orig: 169, 258 25 | offset: 0, 0 26 | index: -1 27 | enemy3_n2 28 | rotate: false 29 | xy: 504, 750 30 | size: 169, 258 31 | orig: 169, 258 32 | offset: 0, 0 33 | index: -1 34 | enemy3_down1 35 | rotate: false 36 | xy: 0, 486 37 | size: 165, 261 38 | orig: 165, 261 39 | offset: 0, 0 40 | index: -1 41 | enemy3_down2 42 | rotate: false 43 | xy: 0, 225 44 | size: 165, 261 45 | orig: 165, 261 46 | offset: 0, 0 47 | index: -1 48 | enemy3_down5 49 | rotate: false 50 | xy: 673, 748 51 | size: 166, 260 52 | orig: 166, 260 53 | offset: 0, 0 54 | index: -1 55 | enemy3_down3 56 | rotate: false 57 | xy: 839, 748 58 | size: 165, 260 59 | orig: 165, 260 60 | offset: 0, 0 61 | index: -1 62 | enemy3_down4 63 | rotate: false 64 | xy: 165, 486 65 | size: 165, 261 66 | orig: 165, 261 67 | offset: 0, 0 68 | index: -1 69 | hero1 70 | rotate: false 71 | xy: 0, 99 72 | size: 102, 126 73 | orig: 102, 126 74 | offset: 0, 0 75 | index: -1 76 | hero2 77 | rotate: false 78 | xy: 165, 360 79 | size: 102, 126 80 | orig: 102, 126 81 | offset: 0, 0 82 | index: -1 83 | hero_blowup_n1 84 | rotate: false 85 | xy: 165, 234 86 | size: 102, 126 87 | orig: 102, 126 88 | offset: 0, 0 89 | index: -1 90 | hero_blowup_n2 91 | rotate: false 92 | xy: 330, 624 93 | size: 102, 126 94 | orig: 102, 126 95 | offset: 0, 0 96 | index: -1 97 | hero_blowup_n3 98 | rotate: false 99 | xy: 330, 498 100 | size: 102, 126 101 | orig: 102, 126 102 | offset: 0, 0 103 | index: -1 104 | hero_blowup_n4 105 | rotate: false 106 | xy: 432, 624 107 | size: 102, 126 108 | orig: 102, 126 109 | offset: 0, 0 110 | index: -1 111 | enemy2 112 | rotate: false 113 | xy: 0, 0 114 | size: 69, 99 115 | orig: 69, 99 116 | offset: 0, 0 117 | index: -1 118 | enemy2_hit 119 | rotate: false 120 | xy: 432, 525 121 | size: 69, 99 122 | orig: 69, 99 123 | offset: 0, 0 124 | index: -1 125 | ufo2 126 | rotate: false 127 | xy: 102, 118 128 | size: 60, 107 129 | orig: 60, 107 130 | offset: 0, 0 131 | index: -1 132 | enemy2_down1 133 | rotate: false 134 | xy: 534, 655 135 | size: 69, 95 136 | orig: 69, 95 137 | offset: 0, 0 138 | index: -1 139 | enemy2_down2 140 | rotate: false 141 | xy: 603, 655 142 | size: 69, 95 143 | orig: 69, 95 144 | offset: 0, 0 145 | index: -1 146 | enemy2_down3 147 | rotate: false 148 | xy: 672, 653 149 | size: 69, 95 150 | orig: 69, 95 151 | offset: 0, 0 152 | index: -1 153 | enemy2_down4 154 | rotate: false 155 | xy: 741, 653 156 | size: 69, 95 157 | orig: 69, 95 158 | offset: 0, 0 159 | index: -1 160 | ufo1 161 | rotate: false 162 | xy: 267, 398 163 | size: 58, 88 164 | orig: 58, 88 165 | offset: 0, 0 166 | index: -1 167 | bomb 168 | rotate: false 169 | xy: 810, 691 170 | size: 63, 57 171 | orig: 63, 57 172 | offset: 0, 0 173 | index: -1 174 | enemy1_down1 175 | rotate: false 176 | xy: 267, 347 177 | size: 57, 51 178 | orig: 57, 51 179 | offset: 0, 0 180 | index: -1 181 | enemy1_down2 182 | rotate: false 183 | xy: 873, 697 184 | size: 57, 51 185 | orig: 57, 51 186 | offset: 0, 0 187 | index: -1 188 | enemy1_down3 189 | rotate: false 190 | xy: 267, 296 191 | size: 57, 51 192 | orig: 57, 51 193 | offset: 0, 0 194 | index: -1 195 | enemy1_down4 196 | rotate: false 197 | xy: 930, 697 198 | size: 57, 51 199 | orig: 57, 51 200 | offset: 0, 0 201 | index: -1 202 | game_pause_nor 203 | rotate: false 204 | xy: 267, 251 205 | size: 60, 45 206 | orig: 60, 45 207 | offset: 0, 0 208 | index: -1 209 | game_pause_pressed 210 | rotate: false 211 | xy: 810, 646 212 | size: 60, 45 213 | orig: 60, 45 214 | offset: 0, 0 215 | index: -1 216 | enemy1 217 | rotate: false 218 | xy: 534, 612 219 | size: 57, 43 220 | orig: 57, 43 221 | offset: 0, 0 222 | index: -1 223 | bullet1 224 | rotate: false 225 | xy: 1004, 987 226 | size: 9, 21 227 | orig: 9, 21 228 | offset: 0, 0 229 | index: -1 230 | bullet2 231 | rotate: false 232 | xy: 69, 78 233 | size: 9, 21 234 | orig: 9, 21 235 | offset: 0, 0 236 | index: -1 237 | -------------------------------------------------------------------------------- /mainGame.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Sep 11 11:05:00 2013 4 | 5 | @author: Leo 6 | """ 7 | 8 | import pygame 9 | from sys import exit 10 | from pygame.locals import * 11 | from gameRole import * 12 | import random 13 | 14 | 15 | # 初始化游戏 16 | pygame.init() 17 | screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 18 | pygame.display.set_caption('飞机大战') 19 | 20 | # 载入游戏音乐 21 | bullet_sound = pygame.mixer.Sound('resources/sound/bullet.wav') 22 | enemy1_down_sound = pygame.mixer.Sound('resources/sound/enemy1_down.wav') 23 | game_over_sound = pygame.mixer.Sound('resources/sound/game_over.wav') 24 | bullet_sound.set_volume(0.3) 25 | enemy1_down_sound.set_volume(0.3) 26 | game_over_sound.set_volume(0.3) 27 | pygame.mixer.music.load('resources/sound/game_music.wav') 28 | pygame.mixer.music.play(-1, 0.0) 29 | pygame.mixer.music.set_volume(0.25) 30 | 31 | # 载入背景图 32 | background = pygame.image.load('resources/image/background.png').convert() 33 | game_over = pygame.image.load('resources/image/gameover.png') 34 | 35 | filename = 'resources/image/shoot.png' 36 | plane_img = pygame.image.load(filename) 37 | 38 | # 设置玩家相关参数 39 | player_rect = [] 40 | player_rect.append(pygame.Rect(0, 99, 102, 126)) # 玩家精灵图片区域 41 | player_rect.append(pygame.Rect(165, 360, 102, 126)) 42 | player_rect.append(pygame.Rect(165, 234, 102, 126)) # 玩家爆炸精灵图片区域 43 | player_rect.append(pygame.Rect(330, 624, 102, 126)) 44 | player_rect.append(pygame.Rect(330, 498, 102, 126)) 45 | player_rect.append(pygame.Rect(432, 624, 102, 126)) 46 | player_pos = [200, 600] 47 | player = Player(plane_img, player_rect, player_pos) 48 | 49 | # 定义子弹对象使用的surface相关参数 50 | bullet_rect = pygame.Rect(1004, 987, 9, 21) 51 | bullet_img = plane_img.subsurface(bullet_rect) 52 | 53 | # 定义敌机对象使用的surface相关参数 54 | enemy1_rect = pygame.Rect(534, 612, 57, 43) 55 | enemy1_img = plane_img.subsurface(enemy1_rect) 56 | enemy1_down_imgs = [] 57 | enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 347, 57, 43))) 58 | enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(873, 697, 57, 43))) 59 | enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 296, 57, 43))) 60 | enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(930, 697, 57, 43))) 61 | 62 | enemies1 = pygame.sprite.Group() 63 | 64 | # 存储被击毁的飞机,用来渲染击毁精灵动画 65 | enemies_down = pygame.sprite.Group() 66 | 67 | shoot_frequency = 0 68 | enemy_frequency = 0 69 | 70 | player_down_index = 16 71 | 72 | score = 0 73 | 74 | clock = pygame.time.Clock() 75 | 76 | running = True 77 | 78 | while running: 79 | # 控制游戏最大帧率为60 80 | clock.tick(45) 81 | 82 | # 控制发射子弹频率,并发射子弹 83 | if not player.is_hit: 84 | if shoot_frequency % 15 == 0: 85 | bullet_sound.play() 86 | player.shoot(bullet_img) 87 | shoot_frequency += 1 88 | if shoot_frequency >= 15: 89 | shoot_frequency = 0 90 | 91 | # 生成敌机 92 | if enemy_frequency % 50 == 0: 93 | enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0] 94 | enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos) 95 | enemies1.add(enemy1) 96 | enemy_frequency += 1 97 | if enemy_frequency >= 100: 98 | enemy_frequency = 0 99 | 100 | # 移动子弹,若超出窗口范围则删除 101 | for bullet in player.bullets: 102 | bullet.move() 103 | if bullet.rect.bottom < 0: 104 | player.bullets.remove(bullet) 105 | 106 | # 移动敌机,若超出窗口范围则删除 107 | for enemy in enemies1: 108 | enemy.move() 109 | # 判断玩家是否被击中 110 | if pygame.sprite.collide_circle(enemy, player): 111 | enemies_down.add(enemy) 112 | enemies1.remove(enemy) 113 | player.is_hit = True 114 | game_over_sound.play() 115 | break 116 | if enemy.rect.top > SCREEN_HEIGHT: 117 | enemies1.remove(enemy) 118 | 119 | # 将被击中的敌机对象添加到击毁敌机Group中,用来渲染击毁动画 120 | enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1) 121 | for enemy_down in enemies1_down: 122 | enemies_down.add(enemy_down) 123 | 124 | # 绘制背景 125 | screen.fill(0) 126 | screen.blit(background, (0, 0)) 127 | 128 | # 绘制玩家飞机 129 | if not player.is_hit: 130 | screen.blit(player.image[player.img_index], player.rect) 131 | # 更换图片索引使飞机有动画效果 132 | player.img_index = shoot_frequency // 8 133 | else: 134 | player.img_index = player_down_index // 8 135 | screen.blit(player.image[player.img_index], player.rect) 136 | player_down_index += 1 137 | if player_down_index > 47: 138 | running = False 139 | 140 | # 绘制击毁动画 141 | for enemy_down in enemies_down: 142 | if enemy_down.down_index == 0: 143 | enemy1_down_sound.play() 144 | if enemy_down.down_index > 7: 145 | enemies_down.remove(enemy_down) 146 | score += 1000 147 | continue 148 | screen.blit(enemy_down.down_imgs[enemy_down.down_index // 2], enemy_down.rect) 149 | enemy_down.down_index += 1 150 | 151 | # 绘制子弹和敌机 152 | player.bullets.draw(screen) 153 | enemies1.draw(screen) 154 | 155 | # 绘制得分 156 | score_font = pygame.font.Font(None, 36) 157 | score_text = score_font.render(str(score), True, (128, 128, 128)) 158 | text_rect = score_text.get_rect() 159 | text_rect.topleft = [10, 10] 160 | screen.blit(score_text, text_rect) 161 | 162 | # 更新屏幕 163 | pygame.display.update() 164 | 165 | for event in pygame.event.get(): 166 | if event.type == pygame.QUIT: 167 | pygame.quit() 168 | exit() 169 | 170 | # 监听键盘事件 171 | key_pressed = pygame.key.get_pressed() 172 | # 若玩家被击中,则无效 173 | if not player.is_hit: 174 | if key_pressed[K_w] or key_pressed[K_UP]: 175 | player.moveUp() 176 | if key_pressed[K_s] or key_pressed[K_DOWN]: 177 | player.moveDown() 178 | if key_pressed[K_a] or key_pressed[K_LEFT]: 179 | player.moveLeft() 180 | if key_pressed[K_d] or key_pressed[K_RIGHT]: 181 | player.moveRight() 182 | 183 | 184 | font = pygame.font.Font(None, 48) 185 | text = font.render('Score: '+ str(score), True, (255, 0, 0)) 186 | text_rect = text.get_rect() 187 | text_rect.centerx = screen.get_rect().centerx 188 | text_rect.centery = screen.get_rect().centery + 24 189 | screen.blit(game_over, (0, 0)) 190 | screen.blit(text, text_rect) 191 | 192 | while 1: 193 | for event in pygame.event.get(): 194 | if event.type == pygame.QUIT: 195 | pygame.quit() 196 | exit() 197 | pygame.display.update() 198 | --------------------------------------------------------------------------------