python大作业 课设
生活随笔
收集整理的這篇文章主要介紹了
python大作业 课设
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需要全套源碼評論留言
# -*- coding:utf-8 -*- #順序都是按照小(enemy0),中(enemy1),大(enemy2),角色(hero)來排序的 import pygame import time import random import sys from pygame.locals import * #全局變量 #窗口 window_screen = None #hero hero = None #得分 hit_score = 0 #是否暫停 is_pause = False#圖片變量 #暫停圖片 pause_image = None #hero_fire_music hero_fire_music = None #number照片 number_image = [] #score_hp照片 score_hp_image = None #單三管炮彈照片 one_or_three_barral = [] #三管炮彈子彈余量照片 bullet_3_stock = None #max_score max_score_image = None #boss_hp boss_HP_image = None #line line_image = None #背景 background = None #重新開始 restart = None #退出游戲 exit_game = None #操作說明 description = None#關于飛機 ##飛機HP HP_list = [1, 20, 100, 20]#enemy0, enemy1, enemy2, hero #飛機大小 plane_size = [{"width":51, "height":39}, {"width":69, "height":89}, {"width":165, "height":246}, {"width":100, "height":124}] #各種飛機爆炸效果計數更換圖片 plane_bomb_time = [5, 8, 14, 8]#enemy0, enemy1, enemy2, hero #飛機最大連發子彈數 plane_maximum_bullet = [2, 5, 7, 8]#enemy0, enemy1, enemy2, hero #血量補給 blood_supply = None #子彈補給 bullet_supply = None#關于子彈 #子彈類型圖片 bullet_type = ["bullet1.png", "bullet-1.gif", "bullet2.png", "bullet.png"] #子彈傷害值 bullet_damage_value = [1, 2, 3, 1]#enemy0, enemy1, enemy2, hero #補給(血量與三發彈藥數目) supply_image = ["bomb-1.gif", "bomb-2.gif"] #補給的大小 supply_size = [{"width":66, "height":88}, {"width":66, "height":88}] #敵機引用列表 enemy0_list = []#enemy0存在飛機列表 enemy0_maximum = 8#enemy0飛機存在最大值 enemy1_list = []#enemy1存在飛機列表 enemy1_maximum = 1 enemy2_list = []#enemy2存在飛機列表 enemy2_maximum = 1class Base(object):"""所有類的父類"""def __init__(self, screen_temp, x, y, image_name):self.x = xself.y = yself.screen = screen_tempself.image = pygame.image.load(image_name)class BasePlane(Base):"""飛機父類"""def __init__(self, plane_type, screen_temp, x, y, image_name, picture_num, HP_temp):Base.__init__(self, screen_temp, x, y, image_name)#plane_type飛機類型self.bullet_list = [] #存儲發射出去的子彈的引用self.plane_type = plane_type #飛機類型標示, 3是hero#爆炸效果用的如下屬性self.hitted = False #表示是否要爆炸self.bomb_picture_list = [] #用來存儲爆炸時需要的圖片self.bomb_picture_num = picture_num #飛機爆炸效果的圖片數量self.picture_count = 0#用來記錄while True的次數,當次數達到一定值時才顯示一張爆炸的圖,然后清空,,當這個次數再次達到時,再顯示下一個爆炸效果的圖片self.image_index = 0#用來記錄當前要顯示的爆炸效果的圖片的序號self.HP = HP_temp #飛機hpself.fire_bullet_count = 0#飛機已發射子彈計數def display(self):"""顯示玩家的飛機"""global hit_scoreglobal HP_listglobal plane_bomb_time#飛機爆炸效果計數#如果被擊中,就顯示爆炸效果,否則顯示普通的飛機效果if self.hitted == True and self.image_index < self.bomb_picture_num and self.HP <= 0:self.screen.blit(self.bomb_picture_list[self.image_index], (self.x, self.y))if self.plane_type != 3 and self.image_index == 0 and self.picture_count == 0:if self.plane_type == 0:#擊毀enemy0得分+HPif hit_score < 650:#初始血量為1hit_score += HP_list[self.plane_type]else:#初始血量為2hit_score += HP_list[self.plane_type]/2elif self.plane_type == 1:#擊毀enemy1得分+HP/2hit_score += HP_list[self.plane_type]/2else:#擊毀enemy2得分+HP/4hit_score += HP_list[self.plane_type]/4self.picture_count += 1if self.picture_count == plane_bomb_time[self.plane_type]: #根據飛機類型不同,爆炸效果持續的時間不同self.picture_count = 0self.image_index += 1elif self.image_index < self.bomb_picture_num:self.screen.blit(self.image, (self.x, self.y)) #顯示原圖if self.hitted == True and not self.bullet_list and self.image_index >= self.bomb_picture_num and self.HP <= 0:del_plane(self) #刪除被擊中敵機的對象#敵機飛出window后刪除if self.y > 860:del_plane(self)#刪除越界子彈del_outWindow_bullet(self)#創建出爆炸效果的圖片的引用def crate_images(self, bomb_picture_name):for i in range(1, self.bomb_picture_num + 1):self.bomb_picture_list.append(pygame.image.load("./feiji/" + bomb_picture_name + str(i) + ".png"))#判斷是否被擊中def isHitted(self, plane, width, height):# widht和height表示范圍if plane.bullet_list and self.HP:for bullet in plane.bullet_list:if bullet.x > self.x+0.05*width and bullet.x < self.x+0.95*width and bullet.y+0.1*height > self.y and bullet.y < self.y + 0.8*height:self.HP -= bullet.damage_value#hero的HP減去子彈的傷害值if self.plane_type == 3:show_score_HP()plane.bullet_list.remove(bullet) #刪除擊中的子彈self.hitted = Trueif plane.plane_type == 3 and plane.barrel_2 and plane.barrel_3:for bullet in plane.barrel_2:#判斷炮管3是否擊中if bullet.x > self.x+0.05*width and bullet.x < self.x+0.95*width and bullet.y+0.1*height > self.y and bullet.y < self.y + 0.8*height:self.HP -= bullet.damage_value#hero的HP減去子彈的傷害值plane.barrel_2.remove(bullet) #刪除擊中的子彈self.hitted = Truefor bullet in plane.barrel_3:#判斷炮管3是否擊中if bullet.x > self.x+0.05*width and bullet.x < self.x+0.95*width and bullet.y+0.1*height > self.y and bullet.y < self.y + 0.8*height:self.HP -= bullet.damage_value#hero的HP減去子彈的傷害值plane.barrel_3.remove(bullet) #刪除擊中的子彈self.hitted = True#飛機開火def fire(self, bullet_maximun):if self.HP > 0:random_num = random.randint(1, 60)if (random_num == 10 or random_num == 45) and len(self.bullet_list) < bullet_maximun:self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y, self))self.fire_bullet_count += 1class HeroPlane(BasePlane):global supply_sizedef __init__(self, screen_temp):BasePlane.__init__(self, 3, screen_temp, 210, 728, "./feiji/hero1.png", 4, HP_list[3]) #super().__init__()BasePlane.crate_images(self, "hero_blowup_n")self.key_down_list = [] #用來存儲鍵盤上下左右移動鍵self.space_key_list = []#保存space鍵self.is_three_bullet = Falseself.barrel_2 = []#2號炮管(左)self.barrel_3 = []#3號炮管(右)self.three_bullet_stock = 50#三管齊發子彈初始值為50#單鍵移動方向def move_left(self):self.x -= 7def move_right(self):self.x += 7def move_up(self):self.y -= 6def move_down(self):self.y += 6#雙鍵移動方向def move_left_and_up(self):self.x -= 5self.y -= 6def move_right_and_up(self):self.x += 5self.y -= 6def move_lef_and_down(self):self.x -= 5self.y += 6def move_right_and_down(self):self.x += 5self.y += 6#控制飛機左右移動范圍sdef move_limit(self):if self.x < 0:self.x = -2elif self.x + 100 > 480:self.x = 380if self.y > 728:self.y = 728elif self.y < 350:self.y += 6#鍵盤按下向列表添加按鍵def key_down(self, key):self.key_down_list.append(key)#鍵盤松開向列表刪除按鍵def key_up(self, key):if len(self.key_down_list) != 0: #判斷是否為空try:self.key_down_list.remove(key)except Exception:pass#控制hero的持續移動def press_move(self):if len(self.key_down_list) != 0:if len(self.key_down_list) == 2:#兩個鍵if (self.key_down_list[0] == K_LEFT and self.key_down_list[1] == K_UP) or (self.key_down_list[1] == K_LEFT and self.key_down_list[0] == K_UP):#key_down_list列表存在按鍵為left,up 或 up,left時調用move_left_and_up()方法self.move_left_and_up()elif (self.key_down_list[0] == K_RIGHT and self.key_down_list[1] == K_UP) or (self.key_down_list[1] == K_RIGHT and self.key_down_list[0] == K_UP):self.move_right_and_up()elif (self.key_down_list[0] == K_LEFT and self.key_down_list[1] == K_DOWN) or (self.key_down_list[1] == K_LEFT and self.key_down_list[0] == K_DOWN):self.move_lef_and_down()elif (self.key_down_list[0] == K_RIGHT and self.key_down_list[1] == K_DOWN) or (self.key_down_list[1] == K_RIGHT and self.key_down_list[0] == K_DOWN):self.move_right_and_down()else:#一個鍵if self.key_down_list[0] == K_LEFT:self.move_left()elif self.key_down_list[0] == K_RIGHT:self.move_right()elif self.key_down_list[0] == K_UP:self.move_up()elif self.key_down_list[0] == K_DOWN:self.move_down()#自爆def bomb(self):self.hitted = Trueself.HP = 0#記得改回0#鍵盤按下向列表添加spacedef space_key_down(self, key):self.space_key_list.append(key)#鍵盤松開向列表刪除spacedef space_key_up(self, key):if len(self.space_key_list) != 0: #判斷是否為空try:self.space_key_list.pop(0)except Exception:raise#按鍵space不放,持續開火def press_fire(self):if len(self.bullet_list) == 0 and len(self.space_key_list):self.fire()else:if len(self.space_key_list) != 0:if self.bullet_list[len(self.bullet_list)-1].y < self.y-14-60:self.fire()#開火def fire(self):global plane_maximum_bullethero_fire_music.play()if not self.is_three_bullet:if len(self.bullet_list) < plane_maximum_bullet[self.plane_type]:#單發炮臺子彈限制為8self.bullet_list.append(Bullet(self.screen, self.x+40, self.y-14, self))else:#沒有子彈限制#主炮管self.bullet_list.append(Bullet(self.screen, self.x+40, self.y-14, self))#創建2,3號炮管子彈self.barrel_2.append(Bullet(self.screen, self.x+5, self.y+20, self))self.barrel_3.append(Bullet(self.screen, self.x+75, self.y+20, self))self.three_bullet_stock -= 1#三管炮彈彈藥余量-1if not self.three_bullet_stock:#三管齊發彈藥用完self.is_three_bullet = False#是否吃到補給def supply_hitted(self, supply_temp, width, height):# widht和height表示范圍if supply_temp and self.HP:#更加精確的判斷是否吃到補給supply_temp_left_x = supply_temp.x + supply_size[supply_temp.supply_type]["width"]*0.15supply_temp_right_x = supply_temp.x + supply_size[supply_temp.supply_type]["width"]*0.85supply_temp_top_y = supply_temp.y + supply_size[supply_temp.supply_type]["height"]*0.4supply_temp_bottom_y = supply_temp.y + supply_size[supply_temp.supply_type]["height"]*0.9if supply_temp_left_x > self.x+0.05*width and supply_temp_right_x <self.x+0.95*width and supply_temp_top_y < self.y+0.95*height and supply_temp_bottom_y > self.y+0.1*height:if supply_temp.supply_type == 0:#0為血量補給,吃到血量補給self.HP -= supply_temp.supply_HP#血量-(-3)if self.HP > 41:#血量最大值為41 self.HP = 41show_score_HP()else:#吃到彈藥補給self.is_three_bullet = Trueself.three_bullet_stock += 20#三管炮彈余量+20del_supply(supply_temp)class Enemy0Plane(BasePlane):"""enemy0的類"""def __init__(self, screen_temp):random_num_x = random.randint(12, 418)random_num_y = random.randint(-50, -40)BasePlane.__init__(self, 0, screen_temp, random_num_x, random_num_y, "./feiji/enemy0.png", 4, HP_list[0])BasePlane.crate_images(self, "enemy0_down")#第二個參數為飛機的plane_type, 0為enemy0def move(self):self.y += 4class Enemy1Plane(BasePlane):"""enemy1的類"""def __init__(self, screen_temp):BasePlane.__init__(self, 1, screen_temp, 205, -90, "./feiji/enemy1.png", 4, HP_list[1])BasePlane.crate_images(self, "enemy1_down")self.direction = "right" #用來存儲飛機默認顯示方向self.num_y = random.randint(15, 400)#出現后左右移動的y值#移動def move(self):if self.direction == "right":self.x += 4elif self.direction == "left":self.x -= 4# 方向判斷if self.x+70 > 480:self.direction ="left"elif self.x < 0:self.direction = "right"if self.y < self.num_y:self.y += 3elif self.fire_bullet_count > 10:#已發射子彈數超過10,即向下移動self.y += 4class Enemy2Plane(BasePlane):"""enemy2的類"""def __init__(self, screen_temp):BasePlane.__init__(self, 2, screen_temp, 158, -246, "./feiji/enemy2.png", 5, HP_list[2])BasePlane.crate_images(self, "enemy2_down")self.direction = "right" #用來存儲飛機默認顯示方向#移動def move(self):if self.direction == "right":self.x += 5elif self.direction == "left":self.x -= 5# 方向判斷if self.x+165 > 480:self.direction ="left"elif self.x < 0:self.direction = "right"if self.y < 0:self.y += 4elif self.fire_bullet_count > 25:#已發射子彈數超過25,即向下移動self.y += 3class BaseBullet(Base):"""子彈基類"""global bullet_damage_valuedef __init__(self, screen_temp, x, y, image_name, plane):Base.__init__(self, screen_temp, x, y, image_name)if plane:self.damage_value = bullet_damage_value[plane.plane_type]#子彈顯示def display(self):self.screen.blit(self.image, (self.x, self.y))class Bullet(BaseBullet):"""hero子彈"""global bullet_typedef __init__(self, screen_temp, x, y, plane):BaseBullet.__init__(self, screen_temp, x, y, "./feiji/"+bullet_type[plane.plane_type], plane)def move(self):self.y -= 16def judge(self):if self.y < 0:return Trueelse:return Falseclass EnemyBullet(BaseBullet):"""enemy子彈"""global bullet_typeglobal plane_sizedef __init__(self, screen_temp, x, y, plane):BaseBullet.__init__(self, screen_temp, x+plane_size[plane.plane_type]["width"]/2, y+plane_size[plane.plane_type]["height"]/2, "./feiji/"+bullet_type[plane.plane_type], plane) #x, y 為子彈發射的位置def move(self):self.y += 7#越界判斷def judge(self):if self.y > 852:return Trueelse:return Falseclass supply_2_hero(BaseBullet):"""hero補給"""def __init__(self, screen_temp, x, y, suppl_type_temp, speed_temp, s_HP):BaseBullet.__init__(self, screen_temp, x, y, "./feiji/"+supply_image[suppl_type_temp], None)self.speed = speed_tempself.supply_HP = s_HPself.supply_type = suppl_type_tempdef move(self):self.y += self.speed#越界判斷def judge(self):if self.y > 855:return Trueelse:return False#函數 def del_outWindow_bullet(plane):"""刪除plane的越界子彈"""bullet_list_out = []#越界子彈for bullet in plane.bullet_list:bullet.display()bullet.move()if bullet.judge(): #判斷子彈是否越界bullet_list_out.append(bullet)#刪除越界子彈if bullet_list_out:for bullet in bullet_list_out: plane.bullet_list.remove(bullet)#如果為hero并且為三管齊發則判斷炮管23的子彈是否越界if plane.plane_type == 3 and (plane.barrel_2 or plane.barrel_3):barrel2_bullet_out = []#越界子彈barrel3_bullet_out = []#越界子彈#判斷炮管2for bullet in plane.barrel_2:bullet.display()bullet.move()if bullet.judge(): #判斷子彈是否越界barrel2_bullet_out.append(bullet)#刪除越界子彈if barrel2_bullet_out:for bullet in barrel2_bullet_out: plane.barrel_2.remove(bullet)#判斷炮管3for bullet in plane.barrel_3:bullet.display()bullet.move()if bullet.judge(): #判斷子彈是否越界barrel3_bullet_out.append(bullet)#刪除越界子彈if barrel3_bullet_out:for bullet in barrel3_bullet_out: plane.barrel_3.remove(bullet)def del_plane(plane):"""回收被擊中的敵機的對象"""global heroglobal hit_scoreglobal enemy0_listglobal enemy1_listglobal enemy2_listif plane in enemy0_list: #回收對象為enemy0enemy0_list.remove(plane)elif plane in enemy1_list:enemy1_list.remove(plane)elif plane in enemy2_list:enemy2_list.remove(plane)elif plane == hero:#回收對象為herohit_score = 0show_score_HP()hero = Nonedef del_supply(supply):"""回收補給"""global blood_supplyglobal bullet_supplyif supply == blood_supply:#回收對象為血量補給blood_supply = Noneelif supply == bullet_supply:bullet_supply = Nonedef reborn():"""Hero重生"""global heroglobal window_screenglobal hit_scorehero = HeroPlane(window_screen)show_score_HP()hit_score = 0#將最高分寫入到文件 def max_score_2_file():global hit_scorefile = Nonetry:file = open("./飛機大戰得分榜.txt", 'r+')except Exception:file = open("./飛機大戰得分榜.txt", 'w+')finally:if file.read():#判斷文件是否為空file.seek(0, 0)#定位到文件開頭file_score = eval(file.read())if hit_score > file_score:file.seek(0, 0)#定位到文件開頭file.truncate()#清空文件內容file.write(str(hit_score))else:file.write(str(hit_score))file.close()def create_enemy_plane():"""生成敵機"""global window_screenglobal hit_scoreglobal enemy0_listglobal enemy0_maximumglobal enemy1_listglobal enemy1_maximumglobal enemy2_listglobal enemy2_maximumglobal HP_listif hit_score < 40:random_num = random.randint(1, 70)HP_list = [1, 20, 100, 20]elif hit_score < 450:random_num = random.randint(1, 60)HP_list = [1, 20, 120, 20]elif hit_score < 650:random_num = random.randint(1, 60)HP_list = [1, 30, 140, 20]elif hit_score < 850:random_num = random.randint(1, 55)HP_list = [2, 36, 160, 20]else:random_num = random.randint(1, 50)HP_list = [2, 40, 180, 20]random_appear_boss1 = random.randint(18, 28)random_appear_boss2 = random.randint(80, 100)#enemy0if (random_num == 20 or random == 40) and len(enemy0_list) < enemy0_maximum:enemy0_list.append(Enemy0Plane(window_screen))#enemy1if (hit_score >= random_appear_boss1 and (hit_score%random_appear_boss1) == 0) and len(enemy1_list) < enemy1_maximum:enemy1_list.append(Enemy1Plane(window_screen))#enemy2if (hit_score >= random_appear_boss2 and (hit_score%random_appear_boss2) == 0) and len(enemy2_list) < enemy2_maximum:enemy2_list.append(Enemy2Plane(window_screen))def create_supply_2_hero(s_type):"""為hero創建血量和彈藥補給"""global window_screenglobal blood_supplyglobal bullet_supplyglobal enemy2_listif enemy2_list:#enemy2存在時補給概率更大random_limitation = 1201else:random_limitation = 2080random_supply = random.randint(1, random_limitation)if (random_supply%690) == 0 and s_type == 0:#血量補給blood_supply = supply_2_hero(window_screen, random.randint(0, 480-58), random.randint(-105, -95), s_type, 3, -3)# -補給類型, -速度, -補給血量值(用的是減法)elif (random_supply%300) == 0 and s_type == 1:#彈藥補給bullet_supply = supply_2_hero(window_screen, random.randint(0, 480-60), random.randint(-115, -108), s_type, 3, 0)def enemy_display_move_fire(enemy):"""敵機的展示,移動,開火"""global window_screenglobal heroglobal plane_maximum_bulletenemy.display() #enemy展示enemy.move() #控制敵機的移動enemy.fire(plane_maximum_bullet[enemy.plane_type]) #敵機開火if hero:#飛機擊中判斷hero.isHitted(enemy, plane_size[hero.plane_type]["width"], plane_size[hero.plane_type]["height"]) #是否擊中heroenemy.isHitted(hero, plane_size[enemy.plane_type]["height"], plane_size[enemy.plane_type]["height"]) #是否擊中enemydef supply_display_move(supply):"""補給的判斷"""supply.display()supply.move()if supply.judge():#越界回收del_supply(supply)總結
以上是生活随笔為你收集整理的python大作业 课设的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言编译器网址
- 下一篇: 微型计算机控制技术第三版第四章课后答案,