当前位置: 首页>后端>正文

小恐龙快跑,不要被逮到拉~ python制作小恐龙游戏

鍓嶈█

鍡ㄥ柦~澶у濂藉憖锛岃繖閲屾槸榄旂帇鍛?!

小恐龙快跑,不要被逮到拉~ python制作小恐龙游戏,第1张

杩欎釜灏忔亹榫欑啛鎮夊彮锛岀浉淇′綘鍦ㄦ懜楸煎緱鏃跺€欑帺杩囧畠~

杩欐槸璋锋瓕娴侀噺鍣ㄤ腑寰堟湁鍚嶇殑褰╄泲锛氬綋浣犵綉缁滃嚭鐜伴棶棰樻椂锛屽氨浼氬嚭鐜颁竴涓€滃皬鎭愰緳娓告垙鈥濄€?/p>

褰撶劧浣犲鏋滄兂瑕佺洿鎺ヨ繘琛屾父鎴忥紝鍙互鍦ㄥ湴鍧€鏍忚緭鍏ワ細chrome://dino

閭d箞浠婂ぉ鎴戜滑寰楃洰鐨勫氨鏄細鐢╬ython鏉ュ埗浣滀竴涓豢鍒剁殑鈥滃皬鎭愰緳娓告垙鈥濓紒

寮€鍙戝伐鍏凤細

Python鐗堟湰锛?.6.4

鐩稿叧妯″潡锛?/h3>

pygame妯″潡锛涗互鍙婁竴浜沺ython鑷甫鐨勬ā鍧椼€?/p>

濡傛灉瀹夎python绗笁鏂规ā鍧?

  1. win + R 杈撳叆 cmd 鐐瑰嚮纭畾, 杈撳叆瀹夎鍛戒护 pip install 妯″潡鍚?(pip install pygame) 鍥炶溅

  2. 鍦╬ycharm涓偣鍑籘erminal(缁堢) 杈撳叆瀹夎鍛戒护

鍑嗗绱犳潗

鍥剧墖

小恐龙快跑,不要被逮到拉~ python制作小恐龙游戏,第2张

闊充箰

小恐龙快跑,不要被逮到拉~ python制作小恐龙游戏,第3张

浠g爜

小恐龙快跑,不要被逮到拉~ python制作小恐龙游戏,第4张

杩愯浠g爜鏂囦欢锛?code>Game7.py

瀵煎叆妯″潡
import cfg
import sys
import random
import pygame
from modules.sprites.scene import *
from modules.sprites.obstacle import *
from modules.sprites.dinosaur import *
from modules.interfaces.gameend import GameEndInterface
from modules.interfaces.gamestart import GameStartInterface
娓告垙鍒濆鍖?/h5>
'''main'''
def main(highest_score):
    pygame.init()
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.display.set_caption('T-Rex Rush 鈥斺€?闈掔伅鏁欒偛 灏忔亹榫?)
瀵煎叆鎵€鏈夊0闊虫枃浠?/h5>
    sounds = {}
    for key, value in cfg.AUDIO_PATHS.items():
        sounds[key] = pygame.mixer.Sound(value)
娓告垙寮€濮嬬晫闈?/h5>
    GameStartInterface(screen, sounds, cfg)
瀹氫箟涓€浜涙父鎴忎腑蹇呰鐨勫厓绱犲拰鍙橀噺
    score = 0
    score_board = Scoreboard(cfg.IMAGE_PATHS['numbers'], position=(534, 15), bg_color=cfg.BACKGROUND_COLOR)
    highest_score = highest_score
    highest_score_board = Scoreboard(cfg.IMAGE_PATHS['numbers'], position=(435, 15), bg_color=cfg.BACKGROUND_COLOR, is_highest=True)
    dino = Dinosaur(cfg.IMAGE_PATHS['dino'])
    ground = Ground(cfg.IMAGE_PATHS['ground'], position=(0, cfg.SCREENSIZE[1]))
    cloud_sprites_group = pygame.sprite.Group()
    cactus_sprites_group = pygame.sprite.Group()
    ptera_sprites_group = pygame.sprite.Group()
    add_obstacle_timer = 0
    score_timer = 0
娓告垙涓诲惊鐜?/h5>
    clock = pygame.time.Clock()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
                    dino.jump(sounds)
                elif event.key == pygame.K_DOWN:
                    dino.duck()
            elif event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
                dino.unduck()
        screen.fill(cfg.BACKGROUND_COLOR)
--闅忔満娣诲姞浜?/h5>
        if len(cloud_sprites_group) < 5 and random.randrange(0, 300) == 10:
            cloud_sprites_group.add(Cloud(cfg.IMAGE_PATHS['cloud'], position=(cfg.SCREENSIZE[0], random.randrange(30, 75))))
--闅忔満娣诲姞浠欎汉鎺?椋為緳
        add_obstacle_timer += 1
        if add_obstacle_timer > random.randrange(50, 150):
            add_obstacle_timer = 0
            random_value = random.randrange(0, 10)
            if random_value >= 5 and random_value <= 7:
                cactus_sprites_group.add(Cactus(cfg.IMAGE_PATHS['cacti']))
            else:
                position_ys = [cfg.SCREENSIZE[1]*0.82, cfg.SCREENSIZE[1]*0.75, cfg.SCREENSIZE[1]*0.60, cfg.SCREENSIZE[1]*0.20]
                ptera_sprites_group.add(Ptera(cfg.IMAGE_PATHS['ptera'], position=(600, random.choice(position_ys))))
--鏇存柊娓告垙鍏冪礌
        dino.update()
        ground.update()
        cloud_sprites_group.update()
        cactus_sprites_group.update()
        ptera_sprites_group.update()
        score_timer += 1
        if score_timer > (cfg.FPS//12):
            score_timer = 0
            score += 1
            score = min(score, 99999)
            if score > highest_score:
                highest_score = score
            if score % 100 == 0:
                sounds['point'].play()
            if score % 1000 == 0:
                ground.speed -= 1
                for item in cloud_sprites_group:
                    item.speed -= 1
                for item in cactus_sprites_group:
                    item.speed -= 1
                for item in ptera_sprites_group:
                    item.speed -= 1
--纰版挒妫€娴?/h5>
        for item in cactus_sprites_group:
            if pygame.sprite.collide_mask(dino, item):
                dino.die(sounds)
        for item in ptera_sprites_group:
            if pygame.sprite.collide_mask(dino, item):
                dino.die(sounds)
--灏嗘父鎴忓厓绱犵敾鍒板睆骞曚笂
        dino.draw(screen)
        ground.draw(screen)
        cloud_sprites_group.draw(screen)
        cactus_sprites_group.draw(screen)
        ptera_sprites_group.draw(screen)
        score_board.set(score)
        highest_score_board.set(highest_score)
        score_board.draw(screen)
        highest_score_board.draw(screen)
--鏇存柊灞忓箷
        pygame.display.update()
        clock.tick(cfg.FPS)
--娓告垙鏄惁缁撴潫
        if dino.is_dead:
            break
娓告垙缁撴潫鐣岄潰
    return GameEndInterface(screen, cfg), highest_score
'''run'''
if __name__ == '__main__':
    highest_score = 0
    while True:
        flag, highest_score = main(highest_score)
        if not flag: break

閰嶇疆浠g爜鏂囦欢锛?code>cfg.py

瀵煎叆妯″潡
import os
灞忓箷澶у皬
SCREENSIZE = (600, 150)
FPS
FPS = 60
闊抽绱犳潗璺緞
AUDIO_PATHS = {
    'die': os.path.join(os.getcwd(), 'resources/audios/die.wav'),
    'jump': os.path.join(os.getcwd(), 'resources/audios/jump.wav'),
    'point': os.path.join(os.getcwd(), 'resources/audios/point.wav')
}
鍥剧墖绱犳潗璺緞
IMAGE_PATHS = {
    'cacti': [os.path.join(os.getcwd(), 'resources/images/cacti-big.png'),
              os.path.join(os.getcwd(), 'resources/images/cacti-small.png')],
    'cloud': os.path.join(os.getcwd(), 'resources/images/cloud.png'),
    'dino': [os.path.join(os.getcwd(), 'resources/images/dino.png'),
             os.path.join(os.getcwd(), 'resources/images/dino_ducking.png')],
    'gameover': os.path.join(os.getcwd(), 'resources/images/gameover.png'),
    'ground': os.path.join(os.getcwd(), 'resources/images/ground.png'),
    'numbers': os.path.join(os.getcwd(), 'resources/images/numbers.png'),
    'ptera': os.path.join(os.getcwd(), 'resources/images/ptera.png'),
    'replay': os.path.join(os.getcwd(), 'resources/images/replay.png')
}
鑳屾櫙棰滆壊
BACKGROUND_COLOR = (235, 235, 235)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

鏁堟灉

小恐龙快跑,不要被逮到拉~ python制作小恐龙游戏,第5张

灏捐

浜虹殑涓€鐢熷氨鍍忓湪鏀€鐧婚珮宄帮紝鍕ゅ鏄綘韪忓疄绋冲仴鐨勫弻鑴氾紝

淇″康鏄綘鎸囧紩鍓嶈鐨勫悜瀵硷紝鍕囨暍鏄綘瀛滃瓬杩藉鐨勬亽蹇冦€?/p>

寮€蹇冩棩鍒颁簡锛屾効浣犵珯绋冲弻鑴氾紝纭畾鏂瑰悜锛屽悜鐫€浣犵殑鐞嗘兂宸呭嘲鍕囨暍鍓嶈锛?/p>

涓嶇敤鎬曪紝鏈潵灏卞湪浣犵殑鑴氫笅銆?/p>

鈥斺€?蹇冪伒楦℃堡

鏈枃绔犲埌杩欓噷灏辩粨鏉熷暒~鎰熷叴瓒g殑灏忎紮浼村彲浠ュ鍒朵唬鐮佸幓璇曡瘯鍝?馃槤

瀵瑰暒锛侊紒璁板緱涓夎繛鍝 馃挄 鍙﹀锛屾杩庡ぇ瀹堕槄璇绘垜寰€鏈熺殑鏂囩珷鍛€~


https://www.xamrdz.com/backend/38j1994344.html

相关文章: