游戏系统
import pygame
import sys
from map import Map
from hero import Hero
from enemy import Enemy
WIN_WIDTH = 512
WIN_HEIGHT = 768
class PlaneWar(object):
def __init__(self):
pygame.init()
window = pygame.display.set_mode([WIN_WIDTH, WIN_HEIGHT])
self.set_window()
self.map = Map(window)
self.hero = Hero(window)
self.enemy = [Enemy(window) for _ in range(4)]
@staticmethod
def set_window():
pygame.display.set_caption("飞机大战v1.0")
icon = pygame.image.load("res/game.ico")
pygame.display.set_icon(icon)
pygame.display.set_icon(icon)
pygame.mixer.music.load("./res/bg2.ogg")
pygame.mixer.music.play(-1)
def events(self):
"""处理相关事件"""
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
print("关闭了窗口")
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.hero.shoot()
def move(self):
"""处理各个对象的移动"""
self.map.move()
self.hero.move()
for i in self.hero.bullets:
i.move()
for j in self.enemy:
j.move()
def blit(self):
"""处理各个图片对象的绘制"""
self.map.blited()
self.hero.blited()
for i in self.enemy:
i.blited()
def display(self):
"""界面更新"""
pygame.display.update()
def is_bullet_hit_enemy(self):
for i in self.hero.bullets:
for j in self.enemy:
if pygame.Rect.colliderect(i.rect, j.rect):
j.reset()
def is_hero_hit_enemy(self):
for i in self.enemy:
if pygame.Rect.colliderect(i.rect, self.hero.rect):
return True
else:
return False
def gameover(self):
pygame.mixer.music.stop()
boom_sound = pygame.mixer.Sound("./res/gameover.wav")
boom_sound.play()
while True:
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
print("关闭了窗口")
sys.exit()
def run(self):
while True:
self.events()
self.move()
self.is_bullet_hit_enemy()
if self.is_hero_hit_enemy():
break
self.blit()
self.display()
self.gameover()
if __name__ == "__main__":
game = PlaneWar()
game.run()
地图
"""
# 在窗口上显示图片需要3个步骤:
# 1、加载图片 map = pygame.image.load("res/img_bg_level_1.jpg")
# 2、绘制图片 window.blit(map, (0, 0))
# 3、更新界面 pygame.display.update()
"""
import pygame
WIN_WIDTH = 512
WIN_HEIGHT = 768
class Map(object):
def __init__(self, win):
self.img = pygame.image.load("res/img_bg_level_2.jpg")
self.img2 = pygame.image.load("res/img_bg_level_2.jpg")
self.window = win
self.y = 0
self.y2 = -WIN_HEIGHT
def move(self):
self.y += 1
self.y2 += 1
if self.y > WIN_HEIGHT:
self.y = -WIN_HEIGHT
if self.y2 > WIN_HEIGHT:
self.y2 = -WIN_HEIGHT
def blited(self):
self.window.blit(self.img, (0, self.y))
self.window.blit(self.img2, (0, self.y2))
英雄飞机
"""
# 在窗口上显示图片需要3个步骤:
# 1、加载图片 map = pygame.image.load("res/img_bg_level_1.jpg")
# 2、绘制图片 window.blit(map, (0, 0))
# 3、更新界面 pygame.display.update()
"""
import pygame
from bullet import Bullet
WIN_WIDTH = 512
WIN_HEIGHT = 768
class Hero(object):
def __init__(self, win):
self.img = pygame.image.load("res/hero.png")
self.window = win
self.rect = self.img.get_rect()
self.rect[1] = 600
self.rect[0] = WIN_WIDTH / 2 - self.rect[2] / 2
self.speed = 2
self.bullets = []
def move(self):
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_w] or pressed_keys[pygame.K_UP]:
if self.rect[1] > 0:
self.rect[1] -= self.speed
if pressed_keys[pygame.K_s] or pressed_keys[pygame.K_DOWN]:
if self.rect[1] < (WIN_HEIGHT - self.rect[3]):
self.rect[1] += self.speed
if pressed_keys[pygame.K_a] or pressed_keys[pygame.K_LEFT]:
if self.rect[0] > 0:
self.rect[0] -= self.speed
if pressed_keys[pygame.K_d] or pressed_keys[pygame.K_RIGHT]:
if self.rect[0] < (WIN_WIDTH - self.rect[2]):
self.rect[0] += self.speed
def shoot(self):
"""飞机发射子弹的功能"""
self.bullets.append(Bullet(self.window, self.rect[0], self.rect[1], self.rect[2]))
def blited(self):
self.window.blit(self.img, (self.rect[0],self.rect[1]))
for i in self.bullets:
if i.rect[1] < 0:
self.bullets.remove(i)
else:
i.blited()
子弹
"""
# 在窗口上显示图片需要3个步骤:
# 1、加载图片 map = pygame.image.load("res/img_bg_level_1.jpg")
# 2、绘制图片 window.blit(map, (0, 0))
# 3、更新界面 pygame.display.update()
"""
import pygame
WIN_WIDTH = 512
WIN_HEIGHT = 768
class Bullet(object):
def __init__(self, win, hero_x, hero_y, hero_w):
self.img = pygame.image.load("res/hero_bullet_7.png")
self.window = win
self.rect = self.img.get_rect()
self.rect[1] = hero_y - self.rect[3]
self.rect[0] = hero_x + hero_w/2 - self.rect[2]/2
def move(self):
self.rect[1] -= 3
def blited(self):
self.window.blit(self.img, (self.rect[0],self.rect[1]))
敌机
"""
# 在窗口上显示图片需要3个步骤:
# 1、加载图片 map = pygame.image.load("res/img_bg_level_1.jpg")
# 2、绘制图片 window.blit(map, (0, 0))
# 3、更新界面 pygame.display.update()
"""
import pygame
import random
WIN_WIDTH = 512
WIN_HEIGHT = 768
class Enemy(object):
def __init__(self, win):
self.img = pygame.image.load("res/img-plane_%s.png" % random.randint(1, 7))
self.window = win
self.rect = self.img.get_rect()
self.rect[1] = random.randint(-100, 50)
self.rect[0] = random.randint(0, WIN_WIDTH-self.rect[2])
self.speed = random.randint(25, 50) * 0.1
def move(self):
self.rect[1] += self.speed
if self.rect[1] > WIN_HEIGHT:
self.reset()
def reset(self):
self.img = pygame.image.load("res/img-plane_%s.png" % random.randint(1, 7))
self.rect[1] = random.randint(-100, 50)
self.rect[0] = random.randint(0, WIN_WIDTH - self.rect[2])
self.speed = random.randint(30, 50) * 0.1
def blited(self):
self.window.blit(self.img, (self.rect[0],self.rect[1]))