目錄
前言
今天我們來一起開啓我們的PyGame製作之路。
首先,我們要對這麼隆重的日子做個標記,嘻嘻🤭
其實我是來自天津…
散了,不說了讓我們一起來看看今天的代碼吧!
第一步:激活模塊
首先,我們需要cmd執行pip install pygame
以及一些舊版本的Python需要執行pip install cfg,math
來下載我們需要的模塊,接下來,就是激活啦!
import cfg
import math
import random
import pygame
from modules.Sprites import *
from modules.interfaces import *
很多小夥伴說:這是什麼意思啊?
我的回答:這都不懂?我看你都沒見過Python吧。。。
遊戲初始化
首先,我們需要加載自定義函數模塊:
def initGame():
初始化pygame, 設置展示窗口
要初始化pygame,這樣子纔可以讓pygame把該忘的都忘了。。。
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption('Bunnies and Badgers')
加載必要的遊戲素材
爲什麼我就不說了吧。。。
game_images = {}
for key, value in cfg.IMAGE_PATHS.items():
game_images[key] = pygame.image.load(value)
game_sounds = {}
for key, value in cfg.SOUNDS_PATHS.items():
if key != 'moonlight':
game_sounds[key] = pygame.mixer.Sound(value)
return screen, game_images, game_sounds
主函數
一句def,自定義函數跑起來~
def main():
初始化
還記得爲什麼要這樣做嗎?
screen, game_images, game_sounds = initGame()
播放背景音樂
這句代碼可有可無
pygame.mixer.music.load(cfg.SOUNDS_PATHS['moonlight'])
pygame.mixer.music.play(-1, 0.0)
字體加載
別把這個丟了~
font = pygame.font.Font(None, 24)
定義兔子
角色定義,可自行修改
bunny = BunnySprite(image=game_images.get('rabbit'), position=(100, 100))
跟蹤玩家的精度變量, 記錄了射出的箭頭數和被擊中的獾的數量
acc_record = [0., 0.]
生命值
可自行修改
healthvalue = 194
弓箭
arrow_sprites_group = pygame.sprite.Group()
獾
badguy_sprites_group = pygame.sprite.Group()
badguy = BadguySprite(game_images.get('badguy'), position=(640, 100))
badguy_sprites_group.add(badguy)
定義了一個定時器, 使得遊戲裏經過一段時間後就新建一支獾
badtimer = 100
badtimer1 = 0
遊戲主循環, running變量會跟蹤遊戲是否結束, exitcode變量會跟蹤玩家是否勝利.
running, exitcode = True, False
clock = pygame.time.Clock()
while running:
–在給屏幕畫任何東西之前用黑色進行填充
screen.fill(0)
該部分的其他內容
由於太長,我就不一個一個寫在目錄裏了,大家在代碼裏自行查看註釋即可。
# --添加的風景也需要畫在屏幕上
for x in range(cfg.SCREENSIZE[0]//game_images['grass'].get_width()+1):
for y in range(cfg.SCREENSIZE[1]//game_images['grass'].get_height()+1):
screen.blit(game_images['grass'], (x*100, y*100))
for i in range(4): screen.blit(game_images['castle'], (0, 30+105*i))
# --倒計時信息
countdown_text = font.render(str((90000-pygame.time.get_ticks())//60000)+":"+str((90000-pygame.time.get_ticks())//1000%60).zfill(2), True, (0, 0, 0))
countdown_rect = countdown_text.get_rect()
countdown_rect.topright = [635, 5]
screen.blit(countdown_text, countdown_rect)
# --按鍵檢測
# ----退出與射擊
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
game_sounds['shoot'].play()
acc_record[1] += 1
mouse_pos = pygame.mouse.get_pos()
angle = math.atan2(mouse_pos[1]-(bunny.rotated_position[1]+32), mouse_pos[0]-(bunny.rotated_position[0]+26))
arrow = ArrowSprite(game_images.get('arrow'), (angle, bunny.rotated_position[0]+32, bunny.rotated_position[1]+26))
arrow_sprites_group.add(arrow)
# ----移動兔子
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w]:
bunny.move(cfg.SCREENSIZE, 'up')
elif key_pressed[pygame.K_s]:
bunny.move(cfg.SCREENSIZE, 'down')
elif key_pressed[pygame.K_a]:
bunny.move(cfg.SCREENSIZE, 'left')
elif key_pressed[pygame.K_d]:
bunny.move(cfg.SCREENSIZE, 'right')
# --更新弓箭
for arrow in arrow_sprites_group:
if arrow.update(cfg.SCREENSIZE):
arrow_sprites_group.remove(arrow)
# --更新獾
if badtimer == 0:
badguy = BadguySprite(game_images.get('badguy'), position=(640, random.randint(50, 430)))
badguy_sprites_group.add(badguy)
badtimer = 100 - (badtimer1 * 2)
badtimer1 = 20 if badtimer1>=20 else badtimer1+2
badtimer -= 1
for badguy in badguy_sprites_group:
if badguy.update():
game_sounds['hit'].play()
healthvalue -= random.randint(4, 8)
badguy_sprites_group.remove(badguy)
# --碰撞檢測
for arrow in arrow_sprites_group:
for badguy in badguy_sprites_group:
if pygame.sprite.collide_mask(arrow, badguy):
game_sounds['enemy'].play()
arrow_sprites_group.remove(arrow)
badguy_sprites_group.remove(badguy)
acc_record[0] += 1
# --畫出弓箭
arrow_sprites_group.draw(screen)
# --畫出獾
badguy_sprites_group.draw(screen)
# --畫出兔子
bunny.draw(screen, pygame.mouse.get_pos())
# --畫出城堡健康值, 首先畫了一個全紅色的生命值條, 然後根據城堡的生命值往生命條裏面添加綠色.
screen.blit(game_images.get('healthbar'), (5, 5))
for i in range(healthvalue):
screen.blit(game_images.get('health'), (i+8, 8))
# --判斷遊戲是否結束
if pygame.time.get_ticks() >= 90000:
running, exitcode = False, True
if healthvalue <= 0:
running, exitcode = False, False
# --更新屏幕
pygame.display.flip()
clock.tick(cfg.FPS)
# 計算準確率
accuracy = acc_record[0] / acc_record[1] * 100 if acc_record[1] > 0 else 0
accuracy = '%.2f' % accuracy
showEndGameInterface(screen, exitcode, accuracy, game_images)
讓程序跑起來
'''run'''
if __name__ == '__main__':
main()
最後
不用你告訴我,我也知道,這個程序肯定報錯,因爲我只提供了代碼,但是自定義模塊我可沒給你。。。
手機掃描以下二維碼進入百度網盤提取文件:
提取碼:sx74
或者微信掃描以下二維碼提取:
電腦:
鏈接: https://pan.baidu.com/s/1zs-FswCqbtLsfanBYre9lA
提取碼: sx74