Python飞机大战代码,分五个文件存放,每个文件代表着不同的功能!

main.py

import pygame
import sys
import traceback
import myplane
import enemy
import bullet
import supplyfrom pygame.locals import *
from random import *pygame.init()
pygame.mixer.init()bg_size = width,height = 480 , 852
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("打飞机!!!")
BLACK = (0,0,0)
GREEN = (0,0,255)
RED = (255,0,0)
WHITE = (255,255,255)background = pygame.image.load("masturbation/image/background.png").convert()#  载入音乐
pygame.mixer.music.load("masturbation/bg_music/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("masturbation/bg_music/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("masturbation/bg_music/use_bomb.ogg")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("masturbation/bg_music/out_porp.ogg")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("masturbation/bg_music/get_bomb.ogg")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("masturbation/bg_music/get_double_laser.ogg")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("masturbation/bg_music/achievement.ogg")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("masturbation/bg_music/big_spaceship_flying.ogg")
enemy3_fly_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("masturbation/bg_music/enemy2_down.wav")
enemy3_down_sound.set_volume(0.1)
enemy2_down_sound = pygame.mixer.Sound("masturbation/bg_music/enemy3_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("masturbation/bg_music/enemy1_down.wav")
enemy1_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("masturbation/bg_music/game_over.ogg")
me_down_sound.set_volume(0.2)def add_small_enemies(group1,group2,num):for i in range(num):e1 = enemy.SmallEnemy(bg_size)group1.add(e1)group2.add(e1)def add_mid_enemies(group1,group2,num):for i in range(num):e2 = enemy.MidEnemy(bg_size)group1.add(e2)group2.add(e2)def add_big_enemies(group1,group2,num):for i in range(num):e3 = enemy.BigEnemy(bg_size)group1.add(e3)group2.add(e3)def inc_speed(target, inc):for each in target:each.speed += incdef main():# 绘制分数score = 0# 难度级别level = 1score_font = pygame.font.Font("masturbation/font/CAIPIRINHA.woff.ttf" , 36)pygame.mixer.music.play(-1)# 生成我方飞机me = myplane.MyPlane(bg_size)enemies = pygame.sprite.Group()# 生成敌方小型飞机small_enemies = pygame.sprite.Group()add_small_enemies(small_enemies,enemies,15)# 生成敌方中型飞机mid_enemies = pygame.sprite.Group()add_mid_enemies(mid_enemies,enemies,5)# 生成敌方大型飞机big_enemies = pygame.sprite.Group()add_big_enemies(big_enemies,enemies,2)clock = pygame.time.Clock()# 中单图片索引e1_destroy_index = 0e2_destroy_index = 0e3_destroy_index = 0me_destroy_index = 0running = True# 用于切换图片switch_image = True# 用于延迟delay = 100# 加载暂停游戏图片#是否暂停游戏paused = Falsepause_nor_image = pygame.image.load("masturbation/image/game_pause_nor.png").convert_alpha()pause_pressed_image = pygame.image.load("masturbation/image/game_pause_pressed.png").convert_alpha()resume_nor_image = pygame.image.load("masturbation/image/game_resume_nor.png").convert_alpha()resume_pressed_image = pygame.image.load("masturbation/image/game_resume_pressed.png").convert_alpha()paused_rect = pause_nor_image.get_rect()paused_rect.left, paused_rect.top = width - paused_rect.width - 10 , 10# 默认显示paused_image = pause_nor_image# 全屏炸弹bomb_image = pygame.image.load("masturbation/image/bomb.png").convert_alpha()bomb_rect = bomb_image.get_rect()bomb_font = pygame.font.Font("masturbation/font/CAIPIRINHA.woff.ttf",48)bomb_num = 3# 每30秒发放一个补给包bomb_supply = supply.Bomb_Supply(bg_size)bullet_supply = supply.Bullet_Supply(bg_size)SUPPLY_TIME = USEREVENTpygame.time.set_timer(SUPPLY_TIME, 30 *1000)# 生成子弹bullet1 = []bullet1_index = 0BULLET1_NUM = 4for i in range(BULLET1_NUM):bullet1.append(bullet.Bullet1(me.rect.midtop))# 生成超级子弹bullet2 = []bullet2_index = 0BULLET2_NUM =8for i in range(BULLET2_NUM // 2):bullet2.append(bullet.Bullet2((me.rect.centerx - 30,me.rect.centery)))bullet2.append(bullet.Bullet2((me.rect.centerx + 30,me.rect.centery)))# 超级子弹定时器DOUBLE_BULLET_TIME = USEREVENT + 1# 是否使用超级子弹is_double_bullet = False# 生命数量life_image = pygame.image.load("masturbation/image/life_image.png").convert_alpha()life_rect = life_image.get_rect()life_num = 3# 解除我方的无敌状态INVINCIBLE_TIME = USEREVENT + 2# 用于阻止重复打开记录文件recorded = False# 游戏的结束画面gameover_font = pygame.font.Font("masturbation/font/CAIPIRINHA.woff.ttf",48)again_image = pygame.image.load("masturbation/image/game_Reagain.png").convert_alpha()again_rect = again_image.get_rect()gameover_image = pygame.image.load("masturbation/image/game_over.png").convert_alpha()gameover_rect = gameover_image.get_rect()while running:# 绘制背景screen.blit(background,(0,0))for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()# 随机产生补给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 = Falsepygame.time.set_timer(DOUBLE_BULLET_TIME,0)# 解除无敌elif event.type == INVINCIBLE_TIME:me.invincible = Falsepygame.time.set_timer(INVINCIBLE_TIME, 0)# 炸弹按键检测elif event.type == KEYDOWN:if event.key == K_SPACE:if bomb_num:bomb_num -= 1# 背景音乐停止pygame.mixer.music.stop()# 停止所有音效pygame.mixer.stop()bomb_sound.play()for each in enemies:if each.rect.bottom > 0:each.active = False# 检测鼠标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_imageelse:paused_image = pause_pressed_imageelse:if paused:paused_image = resume_nor_imageelse:paused_image = pause_nor_image# 检测用户的键盘操作key_pressed = pygame.key.get_pressed()if key_pressed[K_w] or key_pressed[K_UP]:me.moveUp()if key_pressed[K_s] or key_pressed[K_DOWN]:me.moveDown()if key_pressed[K_a] or key_pressed[K_LEFT]:me.moveLeft()if key_pressed[K_d] or key_pressed[K_RIGHT]:me.moveRight()if life_num and not paused:           # 绘制大型敌机for each in big_enemies:if each.active:each.move()# 大型飞机被击中时if each.hit:screen.blit(each.image_hit,each.rect)each.hit = Falseelse:# if switch_image:screen.blit(each.image1, each.rect)else:screen.blit(each.image2, each.rect)# 即将出现时播放音效if each.rect.bottom == -50:enemy3_fly_sound.play(-1)# 绘制血槽pygame.draw.line(screen,BLACK, (each.rect.left , each.rect.top - 5) , (each.rect.right , each.rect.top - 5) , 2)# 生命大于20%显示绿色,否则显示红色energy_remain = each.energy / enemy.BigEnemy.energyif energy_remain > 0.2:energy_color = GREENelse:energy_color = REDpygame.draw.line(screen, energy_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 e3_destroy_index == 0:enemy3_down_sound.play()screen.blit(each.destroy_images[e3_destroy_index], each.rect)e3_destroy_index = (e3_destroy_index + 1) % 6if e3_destroy_index == 0:# 加分score += 10000enemy3_fly_sound.stop()each.reset()# 绘制中型敌机     for each in mid_enemies:if each.active:# 大型飞机被击中时each.move()if each.hit:screen.blit(each.image_hit,each.rect)each.hit = Falseelse:screen.blit(each.image, each.rect)else:# 毁灭if not(delay % 3):if e2_destroy_index == 0:enemy2_down_sound.play()screen.blit(each.destroy_images[e2_destroy_index], each.rect)e2_destroy_index = (e2_destroy_index + 1) % 4if e2_destroy_index == 0:# 加分score += 6000each.reset()# 绘制小型敌机      for each in small_enemies:if each.active:each.move()screen.blit(each.image, each.rect)else:# 毁灭if not(delay % 3):if e1_destroy_index == 0:enemy1_down_sound.play()screen.blit(each.destroy_images[e1_destroy_index], each.rect)e1_destroy_index = (e1_destroy_index + 1)  %  4if e1_destroy_index == 0:# 加分score += 1000each.reset()# 检测飞机碰撞enemies_down = pygame.sprite.spritecollide(me,enemies,False,pygame.sprite.collide_mask)if enemies_down and not me.invincible:me.active = Falsefor 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) % 4if me_destroy_index == 0:life_num -= 1me.reset()pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)# running = False# 绘制剩余的生命数量if life_num:for i in range(life_num):screen.blit(life_image, \(width - 10 - (i + 1) * life_rect.width, height - 10 - life_rect.height))# 绘制超级子弹补给并检测是否获得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# 超级子弹限制时间18秒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# print(len(bullets))bullets[bullet2_index + 0].reset((me.rect.centerx - 30,me.rect.centery))bullets[bullet2_index + 1].reset((me.rect.centerx + 30,me.rect.centery))bullet2_index = (bullet2_index + 2) % BULLET2_NUMelse:bullets = bullet1bullets[bullet1_index].reset(me.rect.midtop)bullet1_index = (bullet1_index + 1) % BULLET1_NUM# 检测子弹是否击中敌机for b in bullets:if b.active:b.move()screen.blit(b.image, b.rect)enemy_hit = pygame.sprite.spritecollide(\b,enemies,False,pygame.sprite.collide_mask)if enemy_hit:b.active = Falsefor e in enemy_hit:if e in mid_enemies or e in big_enemies:e.hit = Truee.energy -= 1if e.energy == 0:e.active = Falseelse:e.active = False# 绘制全屏炸弹补给并检测是否获得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 += 1bomb_supply.active = False# 绘制分数score_text = score_font.render(\"Score : %s" % str(score), True , WHITE)screen.blit(score_text , (20 , 10))# 绘制全屏炸弹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_rect.height))screen.blit(bomb_text, (20 + bomb_rect.width, height - 13 - text_rect.height))# 根据用户的分数来增加难度if level == 1 and score > 50000:level = 2upgrade_sound.play()# 增加三架小型敌机,两架中性敌机,一架大兴敌机add_small_enemies(small_enemies,enemies,3)add_mid_enemies(mid_enemies,enemies,2)add_big_enemies(big_enemies,enemies,1)# 提高小型飞机的速度inc_speed(small_enemies, 1)if level == 2 and score > 300000:level = 3upgrade_sound.play()# 增加5架小型敌机,3架中性敌机,2架大兴敌机add_small_enemies(small_enemies,enemies,5)add_mid_enemies(mid_enemies,enemies,3)add_big_enemies(big_enemies,enemies,2)# 提高小,中型飞机的速度inc_speed(small_enemies, 1)inc_speed(mid_enemies, 1)if level == 3 and score > 600000:level = 4upgrade_sound.play()# 增加5架小型敌机,3架中性敌机,2架大兴敌机add_small_enemies(small_enemies,enemies,5)add_mid_enemies(mid_enemies,enemies,3)add_big_enemies(big_enemies,enemies,2)# 提高小,中型飞机的速度inc_speed(small_enemies, 1)inc_speed(mid_enemies, 1)if level == 4 and score > 1000000:level = 5upgrade_sound.play()# 增加5架小型敌机,3架中性敌机,2架大兴敌机add_small_enemies(small_enemies,enemies,5)add_mid_enemies(mid_enemies,enemies,3)add_big_enemies(big_enemies,enemies,2)# 提高小,中,大型飞机的速度inc_speed(small_enemies, 1)inc_speed(mid_enemies, 1)inc_speed(big_enemies, 1)# 切换图片if not (delay % 5):switch_image = not switch_imagedelay -= 1if not delay:delay = 100# 玩家死亡                                       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_scorer_text = score_font.render( \"Best  :  %d" % record_score, True, (255,255,255))screen.blit(record_scorer_text, (50, 50))gameover_text1 = gameover_font.render( \"Your Scroe", True, (255,255,255))gameover_text1_rect = gameover_text1.get_rect()gameover_text1_rect.left, gameover_text1_rect.top = \(width - gameover_text1_rect.width) // 2, height // 3screen.blit(gameover_text1, gameover_text1_rect)gameover_text2 = gameover_font.render( \str(score), True, (255,255,255))gameover_text2_rect = gameover_text2.get_rect()gameover_text2_rect.left, gameover_text2_rect.top = \(width - gameover_text2_rect.width) // 2, gameover_text1_rect.bottom + 10 screen.blit(gameover_text2, gameover_text2_rect)again_rect.left, again_rect.top = \(width - again_rect.width) // 2, \gameover_text2_rect.bottom + 50screen.blit(again_image, again_rect)gameover_rect.left, gameover_rect.top = (width - again_rect.width) // 2,again_rect.bottom + 10screen.blit(gameover_image,gameover_rect)if pygame.mouse.get_pressed()[0]:pos = pygame.mouse.get_pos()if again_rect.left <pos[0] <again_rect.right \and again_rect.top < pos[1] < again_rect.bottom:main()elif gameover_rect.left < pos[0] < \gameover_rect.right and gameover_rect.top < pos[1] < \gameover_rect.bottom:pygame.quit()sys.exit()# 绘制暂停按钮screen.blit(paused_image, paused_rect)pygame.display.flip()clock.tick(60)if __name__ == "__main__":try:main()except SystemExit:passexcept:traceback.print_exc()pygame.quit()input()

enemy.py

import pygame
from random import *# 小型飞机的定义
class SmallEnemy(pygame.sprite.Sprite):def __init__(self,bg_size):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load("masturbation/image/enemy1.png").convert_alpha()self.destroy_images = []self.destroy_images.extend([\pygame.image.load("masturbation/image/enemy1_down1.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy1_down2.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy1_down3.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy1_down4.png").convert_alpha()  \])self.rect = self.image.get_rect()self.width,self.height = bg_size[0],bg_size[1]self.speed = 2self.active = Trueself.rect.left,self.rect.top = \randint(0,self.width - self.rect.width),\randint(-5 * self.height,0)self.mask = pygame.mask.from_surface(self.image)def move(self):if self.rect.top < self.height:self.rect.top += self.speedelse:self.reset()def reset(self):self.active = Trueself.rect.left,self.rect.top = \randint(0,self.width - self.rect.width),\randint(-5 * self.height,0)# 中型飞机的定义
class MidEnemy(pygame.sprite.Sprite):energy = 8def __init__(self,bg_size):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load("masturbation/image/enemy2.png").convert_alpha()self.image_hit = pygame.image.load("masturbation/image/enemy2_hit.png").convert_alpha()self.destroy_images = []self.destroy_images.extend([\pygame.image.load("masturbation/image/enemy2_down1.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy2_down2.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy2_down3.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy2_down4.png").convert_alpha()  \])self.rect = self.image.get_rect()self.width,self.height = bg_size[0],bg_size[1]self.speed = 1self.active = Trueself.hit = Falseself.rect.left,self.rect.top = \randint(0 , self.width - self.rect.width),\randint(-10 * self.height , -self.height)self.mask = pygame.mask.from_surface(self.image)self.energy = MidEnemy.energydef move(self):if self.rect.top < self.height:self.rect.top += self.speedelse:self.reset()def reset(self):self.active = Trueself.rect.left,self.rect.top = \randint(0 , self.width - self.rect.width),\randint(-10 * self.height , -self.height)self.energy = MidEnemy.energy# 大型飞机的定义
class BigEnemy(pygame.sprite.Sprite):energy = 20def __init__(self,bg_size):pygame.sprite.Sprite.__init__(self)self.image1 = pygame.image.load("masturbation/image/enemy3_n1.png").convert_alpha()self.image2 = pygame.image.load("masturbation/image/enemy3_n2.png").convert_alpha()self.image_hit = pygame.image.load("masturbation/image/enemy3_hit.png").convert_alpha()self.destroy_images = []self.destroy_images.extend([\pygame.image.load("masturbation/image/enemy3_down1.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy3_down2.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy3_down3.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy3_down4.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy3_down5.png").convert_alpha(), \pygame.image.load("masturbation/image/enemy3_down6.png").convert_alpha()  \])self.rect = self.image1.get_rect()self.width,self.height = bg_size[0],bg_size[1]self.speed = 1self.active = Trueself.hit = Falseself.rect.left,self.rect.top = \randint(0 , self.width - self.rect.width),\randint(-15 * self.height , -5 * self.height)# randint(-5 * self.height,0)self.mask = pygame.mask.from_surface(self.image1)self.energy = BigEnemy.energydef move(self):if self.rect.top < self.height:self.rect.top += self.speedelse:self.reset()def reset(self):self.active = Trueself.rect.left,self.rect.top = \randint(0 , self.width - self.rect.width),\randint(-15 * self.height , -5 * self.height)self.energy = BigEnemy.energy

myplane.py

    import pygameclass MyPlane(pygame.sprite.Sprite):def __init__(self,bg_size):pygame.sprite.Sprite.__init__(self)self.image1 = pygame.image.load("masturbation/image/hero2.png").convert_alpha()self.image2 = pygame.image.load("masturbation/image/hero1.png").convert_alpha()self.destroy_images = []self.destroy_images.extend([\pygame.image.load("masturbation/image/hero_blowup_n1.png").convert_alpha(), \pygame.image.load("masturbation/image/hero_blowup_n2.png").convert_alpha(), \pygame.image.load("masturbation/image/hero_blowup_n3.png").convert_alpha(), \pygame.image.load("masturbation/image/hero_blowup_n4.png").convert_alpha()  \])self.rect = self.image1.get_rect()self.width,self.height = bg_size[0] , bg_size[1]self.rect.left,self.rect.top = \(self.width - self.rect.width) // 2, \(self.height - self.rect.height - 40)self.speed = 10self.active = Trueself.mask = pygame.mask.from_surface(self.image1)self.invincible = Falsedef moveUp(self):if self.rect.top > 0:self.rect.top -= self.speedelse:self.rect.top = 0def moveDown(self):if self.rect.bottom < self.height:self.rect.top += self.speedelse:self.rect.bottom = self.heightdef moveLeft(self):if self.rect.left > 0:self.rect.left -= self.speedelse:self.rect.left = 0def moveRight(self):if self.rect.right < self.width:self.rect.left += self.speedelse:self.rect.right = self.widthdef reset(self):self.rect.left,self.rect.top = \(self.width - self.rect.width) // 2, \(self.height - self.rect.height - 40)self.active = Trueself.invincible = Truebullet.pyimport pygameclass Bullet1(pygame.sprite.Sprite):def __init__(self , position):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load("masturbation/image/bullet1.png").convert_alpha()self.rect = self.image.get_rect()self.rect.left , self.rect.top = positionself.speed = 12self.active = Trueself.mask = pygame.mask.from_surface(self.image)def move(self):self.rect.top -= self.speedif self.rect.top < 0:self.active = Falsedef reset(self,position):self.rect.left,self.rect.top = positionself.active = True# 双子弹
class Bullet2(pygame.sprite.Sprite):def __init__(self , position):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load("masturbation/image/bullet1.png").convert_alpha()self.rect = self.image.get_rect()self.rect.left , self.rect.top = positionself.speed = 14self.active = Trueself.mask = pygame.mask.from_surface(self.image)def move(self):self.rect.top -= self.speedif self.rect.top < 0:self.active = Falsedef reset(self,position):self.rect.left,self.rect.top = positionself.active = True

supply.py

import pygame
from random import *class Bullet_Supply(pygame.sprite.Sprite):def __init__(self, bg_size):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load("masturbation/image/ufo1.png").convert_alpha()self.rect = self.image.get_rect()self.width, self.height = bg_size[0], bg_size[1]self.rect.left, self.rect.bottom = randint(0 , self.width - self.rect.width), -100self.speed = 5self.active = Falseself.mask = pygame.mask.from_surface(self.image)def move(self):if self.rect.top < self.height:self.rect.top += self.speedelse:self.active = Falsedef reset(self):self.active = Trueself.rect.left, self.rect.bottom = randint(0 , self.width - self.rect.width), -100class Bomb_Supply(pygame.sprite.Sprite):def __init__(self, bg_size):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load("masturbation/image/ufo2.png").convert_alpha()self.rect = self.image.get_rect()self.width, self.height = bg_size[0], bg_size[1]self.rect.left, self.rect.bottom = randint(0 , self.width - self.rect.width), -100self.speed = 5self.active = Falseself.mask = pygame.mask.from_surface(self.image)def move(self):if self.rect.top < self.height:self.rect.top += self.speedelse:self.active = Falsedef reset(self):self.active = Trueself.rect.left, self.rect.bottom = randint(0 , self.width - self.rect.width), -100

可到:https://download.csdn.net/download/qq_42673921/10746951 下载素材和源代码

Python飞机大战代码相关推荐

  1. python飞机大战代码/day14

    import pygame from pygame.locals import * import random import time class HeroBullet():       #制作英雄子 ...

  2. python飞机大战计分代码_Python 飞机大战代码练习

    Python 飞机大战代码练习 最近在自学Python,参照代码自己写了一遍飞机大战游戏的代码.主要应用的模块为pygame.整个代码如下所示,主要分为主模块和各种精灵类定义模块,记录一下自己的学习历 ...

  3. python飞机大战,感受python的乐趣(详细中文解读,含完整代码)

    python飞机大战 本文章代码是根据<python编程从入门到实践>(第2版)编写而成. 1.外星人部分代码 import pygame from pygame.sprite impor ...

  4. python飞机大战设计思路_python飞机大战pygame游戏背景设计详解

    本文实例讲述了python飞机大战pygame游戏背景设计.分享给大家供大家参考,具体如下: 目标 背景交替滚动的思路确定 显示游戏背景 01. 背景交替滚动的思路确定 运行 备课代码,观察 背景图像 ...

  5. python飞机大战联网版_Python 飞机大战搞怪版本

    python 飞机大战搞怪版本 (飞机为迷你亚索,外星人为迷你小诺手,由于时间关系和图片素材较难寻找,仅仅做了简易版,没有贴上背景图片.由于篇幅原因,对于函数讲解较为简略,可以自行搜索相应函数的用法) ...

  6. python飞机大战没有运行界面_python3实现飞机大战

    本文实例为大家分享了python3实现飞机大战的具体代码,供大家参考,具体内容如下 以下是亲测Python飞机大战全部代码,在保证有pygame环境支持并且有Python3解释器的话完全没问题! 如果 ...

  7. C++的学习心得和知识总结(十六)|基于EasyX实现小甲鱼Python飞机大战项目(C++版)

    目录结构 注:提前言明 本文借鉴了以下博主.书籍或网站的内容,其列表如下: 1.小甲鱼Python项目 – 飞机大战 2.本文使用的掩码图生成工具 自动生成遮罩图的程序,点击前往 3.EasyX官方链 ...

  8. Python 飞机大战游戏中 获取被击中飞机的坐标位置信息

    Python 飞机大战游戏中 获取被击中飞机的坐标位置信息 在参考现有的飞机大战游戏代码,写第一个python游戏,子弹击中飞机后,飞机消失,想着如果能有爆照效果就好了. 于是新建了一个爆炸效果的sp ...

  9. python飞机大战源码素材包_小甲鱼python基础教程飞机大战源码及素材

    原博文 2018-12-22 23:32 − 百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取 ...

  10. python飞机大战创建多个敌机_python飞机大战敌机爆炸怎么编写?

    问题描述 python开发飞机大战时出现以下错误: Traceback (most recent call last): File "D:/python/飞机大战.py", lin ...

最新文章

  1. 跟安全技术大师学习黑客攻防技术 ——《黑客攻防技术宝典:web实战篇》
  2. AI新浪潮:截止2022年,全球74%的计算将来自端侧
  3. is_best = recent_bleu4 > best_bleu4
  4. [Leetcode] Bus Routes 公交线路
  5. C++主要操作符重载的定义和总结
  6. 蓝桥杯 入门训练 A+B问题进阶版(两个3000位数的加法)
  7. html + css 实现淘宝首页(静态页面)
  8. html5读取运动传感器,一种基于六轴传感器的脚步运动识别方法与流程
  9. CNTV CBOX的服务项
  10. 网站metro风格正式发布
  11. selenium爬取亚马逊商品评论
  12. 号称病毒之王的“熊猫烧香”详细分析
  13. python爬取ppt代码_Python爬取PPT模板小工具
  14. tableau各种精典示例经验总结01
  15. ettercap无线局域网内DNS欺骗实例
  16. python 词语频率统计_计算词和词组频率的Python nltk
  17. about unit test in android ppt,选修5 unit4Learning about language.ppt
  18. 2022年跨境电商(美国区)预计趋势
  19. 华铭智能属于芯片概念吗_绩优滞涨的科技股名单来了!这类千亿概念股业绩翻10倍,两大活跃资金加仓股仅6只,射频芯片龙头在列...
  20. Node,Vue,Vite,D3下载与可视化

热门文章

  1. 数学建模算法与应用_CORDIC算法详解(三) CORDIC 算法之线性系统及其数学应用...
  2. 获取当前节点之后的同级节点_04面试常问:分库分表之后,id 主键如何处理?...
  3. 韩顺平 jdbc 之 mysql,(韩顺平讲解)jdbc学习(四)---java连接mysql
  4. php 构造函数参数传值,php 构造函数参数
  5. php 获取小数精度,php小数精度问题
  6. 技术支持和测试的区别_PlatON测试网升级至0.13.0版本并正式启用全新账户地址格式 | 云图双周报2020.07.0107.15...
  7. sql中 replace函数
  8. 1954-计算机基础知识大赛 1
  9. 给FCKeditor添加自定义按钮的方法
  10. 利用神经网络来计算XOR