python打地鼠游戏代码100行_PythonStudy_打地鼠游戏代码
#1、導入模塊
importpygamefrom pygame.locals import *
importmathimportrandom#2、游戲初始化
#2.1設置展示窗口
pygame.init()
width , height= 640 , 480screen=pygame.display.set_mode((width,height))#2.2 游戲中需要用到的按鍵keys:wasd
keys =[False,False,False,False]#2.3 游戲玩家的初始位置
playerpos = [100,100]#2.4 跟蹤箭頭
arrows =[]#2.5 記錄玩家精度:消滅的獾的數量and射出的箭頭數量
acc =[0,0]#2.6 添加壞蛋和壞蛋計時器(隔一段時間出來一個壞蛋)
badguys = [[640,100]]
badtimer= 100badtimer1=0#2.7 初始健康值
healthvalue = 194
#2.8 聲音初始化
pygame.mixer.init()#3、導入聲音和圖片
#3.1、導入展示的圖片
player = pygame.image.load(r".\\pic_lib\dude2.png")
background= pygame.image.load(r".\\pic_lib\grass.png")
arrow= pygame.image.load(r".\\pic_lib\bullet.png")
badguyimg1= pygame.image.load(r".\\pic_lib\badguy4.png")
badguyimg= badguyimg1 #添加了一個圖片的復制
castle = pygame.image.load(r".\\pic_lib\castle.png")
healthbar= pygame.image.load(r".\\pic_lib\healthbar.png")
health= pygame.image.load(r".\\pic_lib\health.png")
gameover= pygame.image.load(r".\\pic_lib\gameover.png")
youwin= pygame.image.load(r".\\pic_lib\youwin.png")#3.2、加載游戲操作的聲音
hit = pygame.mixer.Sound(r".\\pic_lib\explode.wav")
enemy= pygame.mixer.Sound(r".\\pic_lib\enemy.wav")
shoot= pygame.mixer.Sound(r".\\pic_lib\shoot.wav")
hit.set_volume(0.05)
enemy.set_volume(0.05)
shoot.set_volume(0.05)#3.3 加載背景音樂
pygame.mixer.music.load(r".\\pic_lib\moonlight.wav")
pygame.mixer.music.play(-1,0.0)
pygame.mixer.music.set_volume(0.25)#4、保持循環,游戲運行
running = 1exitcode=0whilerunning:
badtimer-= 1
#5、clear the screen before drawing it again
screen.fill(0)#6、draw the screen element
#6.0 添加背景
for x in range(round( width/background.get_width() ) +1):for y in range(round( height/background.get_height() ) +1):
screen.blit(background,(x*100,y*100))#圖片小于背景
#6.1 添加castles
screen.blit(castle,(0,30))
screen.blit(castle,(0,135))
screen.blit(castle,(0,240))
screen.blit(castle,(0,345))#6.2 添加玩家
position =pygame.mouse.get_pos()#獲取鼠標位置
angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26))#鼠標的位置和玩家的位置,根據三角定理,可得旋轉角度函數為:atan2()
playerrot = pygame.transform.rotate(player,360-angle*57.29)#將玩家按所需角度旋轉
playerpos1 = (playerpos[0]+playerrot.get_rect().width/2, playerpos[1]+playerrot.get_rect().height/2)#計算玩家旋轉角度后的位置
screen.blit(playerrot, playerpos1)#玩家移動到鼠標指定位置
#6.3 添加箭頭
for projectile inarrows:
arrow1= pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
screen.blit(arrow1, (projectile[1], projectile[2]))for bullet inarrows:
index=0
vlex= math.cos(bullet[0])*10vley= math.sin(bullet[0])*10
#10 是箭頭的速度
bullet[1] +=vlex
bullet[2] +=vleyif bullet[1]640 or bullet[2]480:#如果超出屏幕,刪除箭頭
arrows.pop(index)
index+= 1
#6.4 添加壞蛋
#6.4.1 show the badguys
#將壞蛋列表的壞蛋顯示到屏幕
for badguy inbadguys:
screen.blit(badguyimg,badguy)#6.4.2 next badguy
#定時結束,添加壞蛋到壞蛋列表
if badtimer ==0:
badguys.append([640,random.randint(50,430)])
badtimer= 100-(badtimer1*2)#定時器設置:先慢后快
if badtimer1>=35:
badtimer= 35
else:
badtimer1+= 5
#6.4.3 attack castle
index =0for badguy inbadguys:#壞蛋以速度7,向前行進
badguy[0] -= 7
#炸碉堡的壞蛋被刪除,碉堡掉健康值
badrect =pygame.Rect(badguyimg.get_rect())
badrect.top= badguy[1]
badrect.left=badguy[0]if badrect.left<64:
hit.play()
healthvalue-= random.randint(5,20)
badguys.pop(index)#check for collisions
index1 =0for bullet inarrows:
bullrect=pygame.Rect(arrow.get_rect())
bullrect.left= bullet[1]
bullrect.top= bullet[2]ifbadrect.colliderect(bullrect):#檢測兩個對象是否重疊
enemy.play()
acc[0]+= 1badguys.pop(index)
arrows.pop(index1)
index1+= 1index+= 1
#6.5 添加血槽
screen.blit(healthbar,(5,5))#先畫一個全紅色的生命值條
for health1 inrange(healthvalue):
screen.blit(health,(health1+8,8))#根據承包的生命值往生命條里添加綠色
#7、update the screen
#7 - 更新屏幕
#添加一個計時器:游戲90秒倒計時
font = pygame.font.Font(None,24)
survivedtext= font.render(str(int((90000-pygame.time.get_ticks())/60000)).zfill(2)+":"+str(int((90000-pygame.time.get_ticks())/1000%60)).zfill(2),
True,(0,0,0))
textRect=survivedtext.get_rect()
textRect.topright= [635,5]
screen.blit(survivedtext,textRect)#更新屏幕顯示
pygame.display.flip()#8、loop through the ecents
#8 - 檢查一些新的事件,如果有退出命令,則終止程序的運行
#pygame里,用給按鍵添加事件的方法,來檢測按鍵。
for event inpygame.event.get():if event.type ==pygame.QUIT:#check if event is quit the game
pygame.quit()
exit(0)if event.type ==pygame.KEYDOWN:#check if event is the X button,it is for moving
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] =Falseif event.type ==pygame.MOUSEBUTTONDOWN:#鼠標如果被點擊,得到鼠標位置并計算箭頭旋轉角度
shoot.play()
position=pygame.mouse.get_pos()
acc[1] += 1arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])#9、move player
ifkeys[0]:
playerpos[1]-=10
elif keys[2]:
playerpos[1]+=10
elif keys[1]:
playerpos[0]-=10
elif keys[3]:
playerpos[0]+=10
#10、Win/Lose check
if pygame.time.get_ticks()>=90000:
running=0
exitcode= 1
if healthvalue <=0:
running=0
exitcode=0if acc[1] !=0:
accuracy= format(acc[0]/acc[1]*100,'.2f')else:
accuracy=0#11、Win/Lose display#顯示勝負圖片
if running == 0 and exitcode ==0:
screen.blit(gameover,(0,0))else:
screen.blit(youwin,(0,0))#顯示勝率
pygame.font.init()
font= pygame.font.Font(None,24)
text= font.render("Accuracy:"+str(accuracy)+"%",True,(255,0,0))
textRect=text.get_rect()
textRect.topright= [350,250]
screen.blit(text,textRect)#刷新
while 1:for event inpygame.event.get():if event.type ==pygame.QUIT:
pygame.quit()
exit(0)
pygame.display.flip()
pygame.mixer.music.stop()
總結
以上是生活随笔為你收集整理的python打地鼠游戏代码100行_PythonStudy_打地鼠游戏代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wpf 如何设置弹出窗口必须关闭才能打开
- 下一篇: 无法分配更多的internet句柄怎么回