Python实现飞机大战
学习完python基础顺手写一个飞机大战练练手,这个游戏已经很多人写过了,本人写的比较菜。由于是练手,所以好多该封装的东西都没封装,不喜勿喷,发博客就是记录一下学习进度,以免荒废一个暑假。(里面有个变态模式, 感兴趣的可以拿来玩玩)。文章最后有资源链接。
项目总览
代码部分
main.py
import pygame.sprite
import enemy
import myplane
import bullet
import supply
from random import *
from pygame.locals import *
from background import *
pygame.init()
bg_size = width, height = 480, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战-----design by 吴泽琛")
pygame.mixer.music.load("./sound/飞机大战.mp3")
pygame.mixer.music.set_volume(0.6)
bullet_sound = pygame.mixer.Sound("./sound/bullet.wav")
bullet_sound.set_volume(0.1)
supply_sound = pygame.mixer.Sound("./sound/supply.wav")
supply_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("./sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("./sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("./sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("./sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("./sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("./sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("./sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("./sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
me_down_sound = pygame.mixer.Sound("./sound/me_down.wav")
me_down_sound.set_volume(0.2)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
bg1 = Background()
bg2 = Background(True)
bg_group = pygame.sprite.Group(bg1, bg2)
SUPPLY_TIME = pygame.USEREVENT
DOUBLE_BULLET_TIME = pygame.USEREVENT + 1
INVINCIBLE_TIME = pygame.USEREVENT + 2
BULLET1_NUM = 4
BULLET2_NUM = 12
def inc_speed(target, inc):
for e in target:
e.speed += inc
def main():
pygame.mixer.music.play(-1)
clock = pygame.time.Clock()
me = myplane.MyPlane(bg_size)
enemy_group = pygame.sprite.Group()
small_enemies = pygame.sprite.Group()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 50)
mid_enemies = pygame.sprite.Group()
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 6)
big_enemies = pygame.sprite.Group()
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 4)
bullet1 = []
bullet1_index = 0
for i in range(BULLET1_NUM):
bullet1.append(bullet.Bullet1((me.rect.centerx - 20, me.rect.centery)))
bullet2 = []
bullet2_index = 0
for i in range(BULLET2_NUM // 3):
bullet2.append(bullet.Bullet2((me.rect.centerx - 30, me.rect.centery)))
bullet2.append(bullet.Bullet2((me.rect.centerx, me.rect.centery)))
bullet2.append(bullet.Bullet2((me.rect.centerx + 30, me.rect.centery)))
paused = False
paused_nor_image = pygame.image.load("./images/pause_nor.png")
paused_pressed_image = pygame.image.load("./images/pause_pressed.png")
resume_nor_image = pygame.image.load("./images/resume_nor.png")
resume_pressed_image = pygame.image.load("./images/resume_pressed.png")
paused_image = paused_nor_image
paused_rect = paused_nor_image.get_rect()
paused_rect.left = width - paused_rect.width - 10
paused_rect.top = 10
score = 0
score_font = pygame.font.Font('./font/rough.ttf', 36)
life_image = pygame.image.load("./images/life.png")
life_rect = life_image.get_rect()
life_num = 3
level = 1
running = True
switch_image = False
delay = 100
bomb_image = pygame.image.load("./images/bomb.png")
bomb_image_rect = bomb_image.get_rect()
bomb_font = pygame.font.Font("./font/font.ttf", 48)
bomb_num = 3
bullet_supply = supply.BulletSupply(bg_size)
bomb_supply = supply.BombSupply(bg_size)
pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
is_double_bullet = False
enemy1_destroy_index = 0
enemy2_destroy_index = 0
enemy3_destroy_index = 0
me_destroy_index = 0
recorded = False
game_begin_font = pygame.font.Font("./font/font.ttf", 48)
begin_image = pygame.image.load("./images/begin.png").convert_alpha()
invincible_image = pygame.image.load("./images/invincible.png").convert_alpha()
begin_image_rect = begin_image.get_rect()
invincible_image_rect = invincible_image.get_rect()
game_over_font = pygame.font.Font("./font/font.ttf", 48)
over_image = pygame.image.load("./images/game_over.png")
again_image = pygame.image.load("./images/again.png")
again_image_rect = again_image.get_rect()
over_image_rect = over_image.get_rect()
bg_group.update()
bg_group.draw(screen)
invincible = False
super_me = True
super_bullet = False
out = False
while True:
bg_group.update()
bg_group.draw(screen)
begin_image_rect.left = (width - begin_image_rect.width) // 2
begin_image_rect.top = (height - begin_image_rect.height) // 2
invincible_image_rect.left = (width - invincible_image_rect.width) // 2
invincible_image_rect.top = (height - invincible_image_rect.height) // 2 + 50
screen.blit(begin_image, begin_image_rect)
screen.blit(invincible_image, invincible_image_rect)
clock.tick(60)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
if begin_image_rect.left < event.pos[0] < begin_image_rect.right and \
begin_image_rect.top < event.pos[1] < begin_image_rect.bottom:
invincible = False
out = True
pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
break
if invincible_image_rect.left < event.pos[0] < invincible_image_rect.right and \
invincible_image_rect.top < event.pos[1] < invincible_image_rect.bottom:
invincible = True
out = True
pygame.time.set_timer(SUPPLY_TIME, 10 * 1000)
life_num = 1
break
if out:
break
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1 and paused_rect.collidepoint(event.pos):
paused = not paused
if paused:
pygame.time.set_timer(SUPPLY_TIME, 0)
pygame.mixer.music.pause()
pygame.mixer.pause()
else:
pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
pygame.mixer.music.unpause()
pygame.mixer.unpause()
elif event.type == MOUSEMOTION:
if paused_rect.collidepoint(event.pos):
if paused:
paused_image = resume_pressed_image
else:
paused_image = paused_pressed_image
else:
if paused:
paused_image = resume_nor_image
else:
paused_image = paused_nor_image
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if bomb_num > 0:
bomb_sound.play()
if not invincible:
bomb_num -= 1
for e in enemy_group:
if e.rect.bottom > 0:
e.active = False
elif event.type == SUPPLY_TIME:
supply_sound.play()
if choice([True, False]):
bomb_supply.reset()
else:
bullet_supply.reset()
elif event.type == DOUBLE_BULLET_TIME:
is_double_bullet = False
pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)
elif event.type == INVINCIBLE_TIME:
me.invincible = False
pygame.time.set_timer(INVINCIBLE_TIME, 0)
if level == 1 and score > 180000:
level = 2
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 2)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 1)
if level == 2 and score > 360000:
level = 3
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 0.5)
inc_speed(mid_enemies, 0.5)
if level == 3 and score > 800000 and super_me:
level = 4
upgrade_sound.play()
super_me = False
super_bullet = True
upgrade_sound.play()
me.change_face(bg_size)
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 0.5)
inc_speed(mid_enemies, 0.5)
if level == 4 and score > 1200000:
level = 5
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 2)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 0.1)
inc_speed(mid_enemies, 0.1)
inc_speed(big_enemies, 0.5)
if level == 5 and score > 2000000:
level = 6
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 0.1)
inc_speed(mid_enemies, 0.1)
inc_speed(big_enemies, 0.1)
if level == 6 and score > 3000000:
level = 7
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 0.1)
inc_speed(mid_enemies, 0.1)
inc_speed(big_enemies, 0.1)
bg_group.update()
bg_group.draw(screen)
if life_num and not paused:
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
me.move_up()
if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
me.move_down()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
me.move_left()
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
me.move_right()
if bomb_supply.active:
bomb_supply.move()
screen.blit(bomb_supply.image, bomb_supply.rect)
if pygame.sprite.collide_mask(bomb_supply, me):
get_bomb_sound.play()
if bomb_num < 3:
bomb_num + 1
bomb_supply.active = False
if bullet_supply.active:
bullet_supply.move()
screen.blit(bullet_supply.image, bullet_supply.rect)
if pygame.sprite.collide_mask(bullet_supply, me):
get_bullet_sound.play()
is_double_bullet = True
pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
bullet_supply.active = False
if not (delay % 10):
bullet_sound.play()
if is_double_bullet:
bullets = bullet2
bullets[bullet2_index].reset((me.rect.centerx - 20, me.rect.centery))
bullets[bullet2_index + 1].reset((me.rect.centerx - 60, me.rect.centery))
bullets[bullet2_index + 2].reset((me.rect.centerx + 20, me.rect.centery))
bullet2_index = (bullet2_index + 3) % BULLET2_NUM
else:
bullets = bullet1
bullets[bullet1_index].reset((me.rect.centerx - 17.5, me.rect.centery))
bullet1_index = (bullet1_index + 1) % BULLET1_NUM
for b in bullets:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemies_hit = pygame.sprite.spritecollide(b, enemy_group, False, pygame.sprite.collide_mask)
if enemies_hit:
b.active = False
for e in enemies_hit:
if e in mid_enemies or e in big_enemies:
if super_bullet:
e.energy -= 4
else:
e.energy -= 1
e.hit = True
if e.energy == 0:
e.active = False
else:
e.active = False
for each in small_enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
if not (delay % 3):
if enemy1_destroy_index == 0:
enemy1_down_sound.play()
screen.blit(each.destroy_images[enemy1_destroy_index], each.rect)
enemy1_destroy_index = (enemy1_destroy_index + 1) % 4
if enemy1_destroy_index == 0:
score += 1000
each.reset()
for each in mid_enemies:
if each.active:
each.move()
if each.hit:
screen.blit(each.hit_image, each.rect)
each.hit = False
else:
screen.blit(each.image, each.rect)
pygame.draw.line(screen,
BLACK,
(each.rect.left, each.rect.top - 5),
(each.rect.right, each.rect.top - 5),
2)
energy_remain = each.energy / enemy.MidEnemy.energy
if energy_remain > 0.3:
line_color = GREEN
else:
line_color = RED
pygame.draw.line(screen,
line_color,
(each.rect.left, each.rect.top - 5),
(each.rect.left + each.rect.width * energy_remain, each.rect.top - 5),
2)
else:
if not (delay % 3):
if enemy2_destroy_index == 0:
enemy2_down_sound.play()
screen.blit(each.destroy_images[enemy2_destroy_index], each.rect)
enemy2_destroy_index = (enemy2_destroy_index + 1) % 4
if enemy2_destroy_index == 0:
score += 4000
each.reset()
for each in big_enemies:
if each.active:
each.move()
if each.hit:
screen.blit(each.hit_image, each.rect)
each.hit = False
else:
if switch_image:
screen.blit(each.image1, each.rect)
else:
screen.blit(each.image2, each.rect)
pygame.draw.line(screen,
BLACK,
(each.rect.left, each.rect.top - 5),
(each.rect.right, each.rect.top - 5),
2)
energy_remain = each.energy / enemy.BigEnemy.energy
if energy_remain > 0.4:
line_color = GREEN
else:
line_color = RED
pygame.draw.line(screen,
line_color,
(each.rect.left, each.rect.top - 5),
(each.rect.left + each.rect.width * energy_remain, each.rect.top - 5),
2)
if each.rect.bottom - 50 > 0:
enemy3_fly_sound.play()
else:
if not (delay % 3):
if enemy3_destroy_index == 0:
enemy3_down_sound.play()
screen.blit(each.destroy_images[enemy3_destroy_index], each.rect)
enemy3_destroy_index = (enemy3_destroy_index + 1) % 6
if enemy3_destroy_index == 0:
enemy3_fly_sound.stop()
score += 10000
each.reset()
enemies_down = pygame.sprite.spritecollide(me, enemy_group, False, pygame.sprite.collide_mask)
if enemies_down and not me.invincible:
if not invincible:
me.active = False
for e in enemies_down:
e.active = False
if me.active:
if switch_image:
screen.blit(me.image1, me.rect)
else:
screen.blit(me.image2, me.rect)
else:
if not (delay % 3):
if me_destroy_index == 0:
me_down_sound.play()
screen.blit(me.destroy_images[me_destroy_index], me.rect)
me_destroy_index = (me_destroy_index + 1) % 4
if me_destroy_index == 0:
life_num -= 1
me.reset()
pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)
if life_num:
for life in range(life_num):
screen.blit(life_image, (width - 10 - (life + 1) * life_rect.width, height - 10 - life_rect.height))
bomb_text = bomb_font.render("x %d" % bomb_num, True, WHITE)
text_rect = bomb_text.get_rect()
screen.blit(bomb_image, (10, height - 10 - bomb_image_rect.height))
screen.blit(bomb_text, (20 + bomb_image_rect.width, height - 10 - bomb_image_rect.height))
score_text = score_font.render("Score : %s" % str(score), True, WHITE)
screen.blit(score_text, (10, 5))
elif life_num == 0:
pygame.mixer.music.stop()
pygame.mixer.stop()
pygame.time.set_timer(SUPPLY_TIME, 0)
if not recorded:
recorded = True
with open('record.txt', 'r') as f:
record_score = int(f.read())
if score > record_score:
with open('record.txt', 'w') as f:
f.write(str(score))
record_score_text = score_font.render("Best : %d" % record_score, True, (255, 255, 255))
screen.blit(record_score_text, (50, 50))
game_over_text1 = game_over_font.render("Your Score", True, (255, 255, 255))
game_over_text1_rect = game_over_text1.get_rect()
game_over_text1_rect.left = (width - game_over_text1_rect.width) // 2
game_over_text1_rect.top = height // 3
screen.blit(game_over_text1, game_over_text1_rect)
game_over_text2 = game_over_font.render(str(score), True, (255, 255, 255))
game_over_text2_rect = game_over_text2.get_rect()
game_over_text2_rect.left = (width - game_over_text2_rect.width) // 2
game_over_text2_rect.top = game_over_text1_rect.top + 70
screen.blit(game_over_text2, game_over_text2_rect)
again_image_rect.left = (width - again_image_rect.width) // 2
again_image_rect.top = game_over_text2_rect.bottom + 50
screen.blit(again_image, again_image_rect)
over_image_rect.left = (width - over_image_rect.width) // 2
over_image_rect.top = again_image_rect.bottom + 50
screen.blit(over_image, over_image_rect)
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
if again_image_rect.left < pos[0] < again_image_rect.right and \
again_image_rect.top < pos[1] < again_image_rect.bottom:
main()
if over_image_rect.left < pos[0] < over_image_rect.right and \
over_image_rect.top < pos[1] < over_image_rect.bottom:
pygame.quit()
screen.blit(paused_image, paused_rect)
if not (delay % 3):
switch_image = not switch_image
if delay:
delay -= 1
else:
delay = 100
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
main()
pygame.quit()
input()
supply.py
import pygame
from random import *
class BulletSupply(pygame.sprite.Sprite):
def __init__(self, bg_size):
super().__init__()
self.image = pygame.image.load("./images/bullet_supply.png")
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = -100
self.speed = 5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.y < self.height:
self.rect.y += self.speed
else:
self.active = False
def reset(self):
self.rect.x = randint(0, self.width - self.rect.width)
self.rect.top = -100
self.active = True
class BombSupply(pygame.sprite.Sprite):
def __init__(self, bg_size):
super().__init__()
self.image = pygame.image.load("./images/bomb_supply.png")
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = -100
self.speed = 4
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.y < self.height:
self.rect.y += self.speed
else:
self.active = False
def reset(self):
self.rect.x = randint(0, self.width - self.rect.width)
self.rect.top = -100
self.active = True
enemy.py
import pygame
from random import *
class SmallEnemy(pygame.sprite.Sprite):
def __init__(self, bg_size):
super(SmallEnemy, self).__init__()
self.image = pygame.image.load("./images/enemy1.png")
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("./images/enemy1_down1.png"),
pygame.image.load("./images/enemy1_down2.png"),
pygame.image.load("./images/enemy1_down3.png"),
pygame.image.load("./images/enemy1_down4.png")])
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 2
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-5 * self.height, 0)
self.active = True
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-5 * self.height, 0)
@staticmethod
def add_small_enemies(group1, group2, bg_size, num):
for i in range(num):
e1 = SmallEnemy(bg_size)
group1.add(e1)
group2.add(e1)
class MidEnemy(pygame.sprite.Sprite):
energy = 8
def __init__(self, bg_size):
super().__init__()
self.image = pygame.image.load("./images/enemy2.png")
self.hit_image = pygame.image.load("./images/enemy2_hit.png")
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("./images/enemy2_down1.png"),
pygame.image.load("./images/enemy2_down2.png"),
pygame.image.load("./images/enemy2_down3.png"),
pygame.image.load("./images/enemy2_down4.png")])
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 2
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-10 * self.height, -self.height)
self.active = True
self.hit = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = MidEnemy.energy
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-10 * self.height, -self.height)
@staticmethod
def add_mid_enemies(group1, group2, bg_size, num):
for i in range(num):
e2 = MidEnemy(bg_size)
group1.add(e2)
group2.add(e2)
class BigEnemy(pygame.sprite.Sprite):
energy = 20
def __init__(self, bg_size):
super().__init__()
self.image1 = pygame.image.load("./images/enemy3_n1.png")
self.image2 = pygame.image.load("./images/enemy3_n2.png")
self.hit_image = pygame.image.load("./images/enemy3_hit.png")
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("./images/enemy3_down1.png"),
pygame.image.load("./images/enemy3_down2.png"),
pygame.image.load("./images/enemy3_down3.png"),
pygame.image.load("./images/enemy3_down4.png"),
pygame.image.load("./images/enemy3_down5.png"),
pygame.image.load("./images/enemy3_down6.png")])
self.rect = self.image1.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 2
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-15 * self.height, -5 * self.height)
self.active = True
self.hit = False
self.mask = pygame.mask.from_surface(self.image1)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = BigEnemy.energy
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-15 * self.height, -5 * self.height)
@staticmethod
def add_big_enemies(group1, group2, bg_size, num):
for i in range(num):
e3 = BigEnemy(bg_size)
group1.add(e3)
group2.add(e3)
myplane.py
import webbrowser
import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self, bg_size):
self.image1 = pygame.image.load("./images/me1.png")
self.image2 = pygame.image.load("./images/me2.png")
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("./images/me_destroy_1.png"),
pygame.image.load("./images/me_destroy_2.png"),
pygame.image.load("./images/me_destroy_3.png"),
pygame.image.load("./images/me_destroy_4.png")])
self.active = True
self.invincible = False
self.mask = pygame.mask.from_surface(self.image1)
self.width = bg_size[0]
self.height = bg_size[1]
self.rect = self.image1.get_rect()
self.rect.bottom = bg_size[1] - 60
self.rect.left = (bg_size[0] - self.rect.width) // 2
self.speed = 8
def move_up(self):
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def move_down(self):
if self.rect.bottom < self.height - 60:
self.rect.bottom += self.speed
else:
self.rect.bottom = self.height - 60
def move_left(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def move_right(self):
if self.rect.right < self.width:
self.rect.right += self.speed
else:
self.rect.right = self.width
def reset(self):
self.rect.left = (self.width - self.rect.width) // 2
self.rect.bottom = self.height - 60
self.active = True
self.invincible = True
def change_face(self, bg_size):
self.image1 = pygame.image.load("./images/me5.png")
self.image2 = pygame.image.load("./images/me6.png")
self.mask = pygame.mask.from_surface(self.image1)
self.rect = self.image1.get_rect()
self.rect.bottom = bg_size[1] - 60
self.rect.left = (bg_size[0] - self.rect.width) // 2
self.speed = 10
background.py
import pygame
class Background(pygame.sprite.Sprite):
def __init__(self, is_other=False):
super().__init__()
self.image = pygame.image.load("./images/background.png")
self.rect = self.image.get_rect()
print(self.rect.size)
if is_other:
self.rect.y = -self.rect.height
def update(self):
self.rect.y += 1
if self.rect.y >= 700:
self.rect.y = -self.rect.height
附上游戏需要的资源
链接:https://pan.baidu.com/s/1ujLif8qLgznucOMDNb3pMQ 提取码:wzc6
|