国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python-小游戲(打飛機)

2019-11-08 03:21:28
字體:
來源:轉載
供稿:網友

#---------------------------------------------------備注--------------------------------------------------------------------------

#這游戲是學習 (類,函數,循環,條件,判斷)的簡單小游戲;

#運行環境:python-3.2.5,pygame-1.9.2a0.win32-py3.2;

#---------------------------------------------------END--------------------------------------------------------------------------

# -*- coding: utf-8 -*-import pygameimport randomfrom pygame.locals import *from sys import exitpygame.init()class FeiJi:def __init__(self,x,y):self.x = xself.y = yself.image = pygame.image.load('C://Users//bad//Desktop//python-test//image//feiji.png')def restart(self):self.x = 200self.y = 500#加載 飛機 圖像screen.blit(feiji.image, (feiji.x,feiji.y))def move(self):#-----------------------------------#判斷 按鍵 狀態if event.type==pygame.KEYDOWN:if event.key==K_w:keys[0]=Trueelif event.key==K_a:keys[1]=Trueelif event.key==K_s:keys[2]=Trueelif event.key==K_d:keys[3]=Trueif event.type==pygame.KEYUP:if event.key==K_w:keys[0]=Falseelif event.key==K_a:keys[1]=Falseelif event.key==K_s:keys[2]=Falseelif event.key==K_d:keys[3]=False#-----------------------------------# 按鍵 的操作if keys[0]==True:if self.y > 0:self.y-=0.3if keys[1]==True:if self.x > 0:self.x-=0.3if keys[2]==True:if self.y < 600 - feiji.image.get_height():self.y+=0.3if keys[3]==True:if self.x < 300 - feiji.image.get_width():self.x+=0.3#-----------------------------------#---------------END-----------------class ShiTou:def __init__(self):self.x = random.randint(0, 250)self.y = random.randint(-150, 0)self.image = pygame.image.load('C://Users//bad//Desktop//python-test//image//shitou.png')self.active = False#設置石頭下落速度 def speed(self):self.y+=0.2#重置石頭 def restart(self):self.x=random.randint(0, 250)self.y=random.randint(-200, 0)self.active = True#石頭的下落def down(self):if self.active:if self.y < 600:self.speed()if self.y > 600:self.active = Falseelse:self.restart()#-----------------------------------#---------------END-----------------class ZiDan(FeiJi): def __init__(self):self.x=feiji.xself.y=feiji.yself.image = pygame.image.load('C://Users//bad//Desktop//python-test//image//zidan.png')self.active = False#設置子彈發射速度def speed(self):self.y-=0.8#重置子彈def restart(self):self.active = Trueself.y=feiji.yself.x=feiji.x + 7#子彈的發射 def she(self):if self.active:self.speed()if self.y < 0:self.active = Falseelse:self.restart()#-----------------------------------#---------------END-----------------#創建石頭擊落函數#判斷子彈是否擊中石頭,只需要判斷子彈是否穿過石頭。#方法--判斷子彈的x坐標是否大于石頭圖像左邊的x坐標或者是否小于石頭圖像右邊的x坐標def CheckHit(zidan, shitou):    if(shitou.x < zidan.x and zidan.x < shitou.x + shitou.image.get_width()) and (shitou.y < zidan.y and zidan.y < shitou.y + shitou.image.get_height()):        shitou.active = False        zidan.active = False        return True    return False#創建飛機墜毀函數#方法參考“CheckHit”def CheckTouch(shitou, feiji):    if (shitou.x + shitou.image.get_width() > feiji.x and feiji.x + feiji.image.get_width() > shitou.x) and (shitou.y + shitou.image.get_height() > feiji.y and feiji.y + feiji.image.get_height() > shitou.y):        return True    return False#-------------------------------------------------------------------------------------------------------------#---------------------設置游戲常數------------------------------------------------------------------------------width,height=(300,600) #設置游戲窗口大小bg=pygame.image.load('C://Users//bad//Desktop//python-test//image//black.png')#設置背景圖路徑screen=pygame.display.set_mode((width,height)) #新建一個游戲窗口,窗口大小為(width,height)font = pygame.font.Font(None, 32) #游戲運行時的字體font_over = pygame.font.Font(None, 128) #游戲結束時的字體pygame.display.set_caption("test-game") #設置游戲窗口標題keys = [False, False, False, False, False] #按鍵初始化(w,a,s,d,r)gameover = False #游戲狀態jifen = 0 #初始化積分zidan_jiange = 500 #子彈發射間隔時間zidan_index = 0 #引導子彈組#------------------------END----------------------------------------------------------------------------------#----------------------實例化類--------------------------------------------------------------------------------feiji = FeiJi(200,500) #實例化-飛機shitou = [] #實例化-石頭組for s in range(5): #設置實例化數目 shitou.append(ShiTou())#-----------(5個)zidan = [] #實例化-子彈組for z in range(5): #設置實例化數目 zidan.append(ZiDan())#-----------(5個)#------------------------END----------------------------------------------------------------------------------while 1:#游戲主循環for event in pygame.event.get():if event.type==pygame.QUIT:pygame.quit() exit(0)#screen.fill(0)#將沒有圖像的空間填滿(黑色)screen.blit(bg, (0,0))#載入背景圖screen.blit(feiji.image, (feiji.x,feiji.y))#加載飛機圖像if not gameover:#判斷游戲狀態 text_jifen = font.render("Score: %d" %jifen, 0, (0, 0, 0))#文本-積分統計screen.blit(text_jifen, (0, 0))#載入文本(積分統計)feiji.move() #調用 飛機 移動的方法for s in shitou:#加載 石頭組 圖像并且調用石頭下落的方法if CheckTouch(s, feiji):gameover = Trues.down()screen.blit(s.image, (s.x,s.y))zidan_jiange -= 1#設置子彈組循環發射if zidan_jiange < 0:zidan_jiange = 500#子彈發射間隔zidan[zidan_index].restart()#子彈組發射zidan_index = (zidan_index + 1) % 5#設置子彈組循環發射for z in zidan:#加載 子彈組 圖像并且調用子彈射擊的方法z.she()screen.blit(z.image, (z.x,z.y))for s in shitou:if (CheckHit(z, s)):#調用 檢查擊中 函數,判斷子彈是否擊中 石頭jifen+=100         #當子彈 擊中 石頭,積分加 100 else:text_restart = font.render("按 R 鍵重新游戲", 1, (0, 0, 0))#文本-游戲restartscreen.blit(text_restart, (100,350))#載入文本(游戲restart)text_over = font.render("Game Over", 1, (0, 0, 0))#文本-游戲結束screen.blit(text_over, (100,250))#載入文本(游戲結束)text_jifen = font.render("Score: %d" %jifen, 1, (0, 0, 0))#文本-獲取的積分數screen.blit(text_jifen, (100, 300))#載入文本(獲取的積分數)if event.type==pygame.KEYDOWN:if event.key==K_r:#按R鍵重新游戲keys[4]=Trueif event.type==pygame.KEYUP:if event.key==K_r:keys[4]=Falseif keys[4]==True:jifen = 0 #積分清零screen.blit(feiji.image, (200,500))#加載飛機圖像for s in shitou:s.restart()for z in zidan:z.active = Falsekeys = [False, False, False, False, False]#按鍵初始化(w,a,s,d,r) gameover = False#刷新屏幕 pygame.display.flip()
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 横峰县| 德阳市| 德昌县| 德惠市| 自治县| 武清区| 汾阳市| 涿鹿县| 德兴市| 界首市| 德清县| 建昌县| 宜宾县| 邮箱| 玛沁县| 桦川县| 扬中市| 永顺县| 乌拉特中旗| 海淀区| 盐边县| 新郑市| 伊金霍洛旗| 岳普湖县| 巴南区| 桃源县| 宾川县| 九龙城区| 新巴尔虎右旗| 松潘县| 虎林市| 南皮县| 泸州市| 佳木斯市| 金乡县| 镇安县| 乌拉特中旗| 乐平市| 广宁县| 远安县| 博罗县|