PythonStudy_打地鼠游戲代碼


# 1、導入模塊
import pygame
from pygame.locals import *
import math
import random

# 2、游戲初始化

# 2.1設置展示窗口
pygame.init()
width , height = 640 , 480
screen = 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 = 100
badtimer1 = 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 = 1
exitcode = 0
while running:
    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 in arrows:
        arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
        screen.blit(arrow1, (projectile[1], projectile[2]))
    for bullet in arrows:
        index = 0
        vlex = math.cos(bullet[0])*10
        vley = math.sin(bullet[0])*10
            # 10 是箭頭的速度
        bullet[1] += vlex
        bullet[2] += vley
        if  bullet[1]<-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480:
            # 如果超出屏幕,刪除箭頭
            arrows.pop(index)
        index += 1

    # 6.4 添加壞蛋
    # 6.4.1 show the badguys
    # 將壞蛋列表的壞蛋顯示到屏幕
    for badguy in badguys:
        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 = 0
    for badguy in badguys:
        # 壞蛋以速度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 = 0
        for bullet in arrows:
            bullrect = pygame.Rect(arrow.get_rect())
            bullrect.left = bullet[1]
            bullrect.top = bullet[2]
            if badrect.colliderect(bullrect):
                # 檢測兩個對象是否重疊
                enemy.play()
                acc[0] += 1
                badguys.pop(index)
                arrows.pop(index1)
            index1 += 1
        index += 1

    # 6.5 添加血槽
    screen.blit(healthbar,(5,5))
        # 先畫一個全紅色的生命值條
    for health1 in range(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 in pygame.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] = True
            elif event.key == K_a:
                keys[1] = True
            elif event.key == K_s:
                keys[2] = True
            elif event.key == K_d:
                keys[3] = True
        if event.type == pygame.KEYUP:
            if event.key == K_w:
                keys[0] = False
            elif event.key == K_a:
                keys[1] = False
            elif event.key == K_s:
                keys[2] = False
            elif event.key == K_d:
                keys[3] = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            # 鼠標如果被點擊,得到鼠標位置並計算箭頭旋轉角度
            shoot.play()
            position = pygame.mouse.get_pos()
            acc[1] += 1
            arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])

    # 9、move player
    if keys[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 = 0
    if 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 in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
    pygame.display.flip()
    pygame.mixer.music.stop()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM