Python打飞机(飞机大战)
敌机和主机
文章图片
Snipaste_2018-05-13_19-17-22.png 发子弹
文章图片
Snipaste_2018-05-13_19-23-02.png 连发子弹
文章图片
Snipaste_2018-05-13_19-25-20.png 飞机大战(加音乐代码)
文章图片
Snipaste_2018-05-13_19-28-17.png 飞机大战代码:
import pygame
import random
SCREEN_RECT = pygame.Rect(0,0,480,700)
CREATE_ENEMY_EVENT = pygame.USEREVENT
精灵组
class GameSprite(pygame.sprite.Sprite):
def __init__(self,image_name,speed=1):
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
self.rect.y += self.speed
飞机大战敌机
class Enemy(GameSprite):
def __init__(self):
super().__init__("./images/enemy1.png")
self.speed = random.randint(1, 3)
self.rect.bottom = 0
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0, max_x)
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
print("敌机飞出屏幕...")
飞机大战背景
class Background(GameSprite):
def __init__(self,is_alt=False):
image_name = "./images/background.png"
super().__init__(image_name)
if is_alt:
self.rect.y = -self.rect.height
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -self.rect.height
class Enemy(GameSprite):
def __init__(self):
image_name = "./images/enemy-1.gif"
super().__init__(image_name)
self.speed = random.randint(1,3)
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0,max_x)
self.rect.bottom = 0
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
self.kill()
def __del__(self):
print('删除%s'%self.rect)
class Hero(GameSprite):
def __init__(self):
super().__init__('./images/hero.gif',0)
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 120
self.bullets = pygame.sprite.Group()
def fire(self):
bullet = Bullet()
bullet.rect.bottom = self.rect.y-20
bullet.rect.centerx = self.rect.centerx
self.bullets.add(bullet)
上下左右移动
def update(self):
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_RIGHT]:
self.rect.x += 10
if keys_pressed[pygame.K_LEFT]:
self.rect.x -= 10
if keys_pressed[pygame. K_UP]:
self.rect.y -= 10
if keys_pressed[pygame.K_DOWN]:
self.rect.y += 10
if keys_pressed[pygame.K_SPACE]:
self.fire()
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > SCREEN_RECT.height:
self.rect.bottom = SCREEN_RECT.height
子弹
class Bullet(GameSprite):
def __init__(self):
super().__init__('./images/bullet1.png',-10)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
精灵组
import pygame
import random
SCREEN_RECT = pygame.Rect(0,0,480,700)
CREATE_ENEMY_EVENT = pygame.USEREVENT
class GameSprite(pygame.sprite.Sprite):
def __init__(self,image_name,speed=1):
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
self.rect.y += self.speed
class Enemy(GameSprite):
def __init__(self):
super().__init__("./images/enemy1.png")
self.speed = random.randint(1, 3)
self.rect.bottom = 0
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0, max_x)
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
print("敌机飞出屏幕...")
class Background(GameSprite):
def __init__(self,is_alt=False):
image_name = "./images/background.png"
super().__init__(image_name)
if is_alt:
self.rect.y = -self.rect.height
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -self.rect.height
class Enemy(GameSprite):
def __init__(self):
image_name = "./images/enemy-1.gif"
super().__init__(image_name)
self.speed = random.randint(1,3)
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0,max_x)
self.rect.bottom = 0
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
self.kill()
def __del__(self):
print('删除%s'%self.rect)
class Hero(GameSprite):
def __init__(self):
super().__init__('./images/hero.gif',0)
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 120
self.bullets = pygame.sprite.Group()
def fire(self):
bullet = Bullet()
bullet.rect.bottom = self.rect.y-20
bullet.rect.centerx = self.rect.centerx
self.bullets.add(bullet)
def update(self):
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_RIGHT]:
self.rect.x += 10
if keys_pressed[pygame.K_LEFT]:
self.rect.x -= 10
if keys_pressed[pygame. K_UP]:
self.rect.y -= 10
if keys_pressed[pygame.K_DOWN]:
self.rect.y += 10
if keys_pressed[pygame.K_SPACE]:
self.fire()
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > SCREEN_RECT.height:
self.rect.bottom = SCREEN_RECT.height
class Bullet(GameSprite):
def __init__(self):
super().__init__('./images/bullet1.png',-1)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
推荐阅读
- 遇到一哭二闹三打滚的孩子,怎么办┃山伯教育
- Ⅴ爱阅读,亲子互动——打卡第178天
- 我错了,余生不再打扰
- python学习之|python学习之 实现QQ自动发送消息
- 2018-3-24
- 逻辑回归的理解与python示例
- 日志打卡
- 以读攻“毒”唤新活动曹彦斌打卡第二天
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum