python星际赛车 源代码 硬核游戏!turtle star race arcade game

python trutle star space race game
python trutle star space race game

主要用海龟画图制作的简易赛车接金币游戏,音乐效果比较硬核!借用的是pygamer的混音器模块。

以下是部分代码预览:

"""这是用Python的海龟画图模块制作的赛车小游戏"""

from turtle import Turtle,Screen
from random import randint,choice
from time import sleep
from car import *                           # 导入Car小汽车类

__author__ = "李兴球"
__date__ = "2018/11/26"
__company__ = "风火轮编程"

class ScrollBackground():
    """定义一个滚动背景类"""

class Coin(Turtle):
    def __init__(self,images,sprite,sound):
        Turtle.__init__(self,visible=False)
        self.images = images             
        self.sprite = sprite
        self.sound = sound
        self.penup()                          # 抬笔        
        self.goto_up()                        # 到上边        

        
def move_left():
    if main_sprite.xcor()>-80 and main_sprite.hp >= 0:   # 主角x坐标大于-80并且血大于等于0就能向左移动
        main_sprite.setx(main_sprite.xcor() - 40)
    
def move_right():
    if main_sprite.xcor()< 80 and main_sprite.hp >= 0:   # x坐标小于80并且血大于等于0就能向右移动
        main_sprite.setx(main_sprite.xcor() + 40)

def init_audio(music,collision_sound,coin_sound):
    """初始化音频"""
    metal_sound = None                            # 金属声
    try:
        import pygame                             # 导入pygame模块,无此模块本游戏不会发声
        pygame.mixer.init()                       # 初始化混音器
        pygame.mixer.music.load(music)            # 加载背景音乐
        pygame.mixer.music.play(-1,0)             # 循环播放背景音乐
        metal_sound = pygame.mixer.Sound(collision_sound) # 加载金属碰撞声
        coin_sound = pygame.mixer.Sound(coin_sound)       #加载金币声
         
    except:
        pass
    return metal_sound,coin_sound

def init_screen(background_image,sport_car_image):
    """初始化屏幕"""
    screen = Screen()                            # 新建屏幕对象
    screen.delay(0)                              # 屏幕绘画延时为0
    screen.setup(480,360)                        # 设置屏幕canvas大小
    screen.bgcolor("black")                      # 背景色为黑色
    screen.bgpic("封面2.png")                    # 显示封面
    screen.title("Python星际赛车_作者:李兴球_风火轮编程")

    
    return screen

def make_mainsprite(screen):
    global main_sprite
    main_sprite = Turtle(shape = sport_car_image)
    main_sprite.score = 0                        # 新增自定义属性,得分
    main_sprite.hp = 100                         # 新增自定义属性,血量
    main_sprite.dead = False                     # 新增死亡属性,表示是否死亡
    main_sprite.penup()
    main_sprite.sety(-110)
    screen.onkeypress(move_left,"Left")
    screen.onkeypress(move_right,"Right")
    return main_sprite
    
def begin_game():
    """开始游戏"""
    screen.onkeypress(None,"Return")             # 取消回车键的按键绑定
    screen.bgpic("rode480.png")                  # 背景图书片
    scrollbg = ScrollBackground(background_image,screen) #新建可滚动背景
    scrollbg.move()                              # 背景移动
    main_sprite = make_mainsprite(screen)        # 新建主角,也就是按左右键操作的跑车
    
    
if __name__ == "__main__":

    main_sprite = None
    
    music = "Chiptune.wav"
    collision_sound = "1.wav"
    coin_sound = "接金币.wav"
    metal_sound,coin_sound = init_audio(music,collision_sound,coin_sound)    

    background_image = "rode480.gif"
    sport_car_image = "跑车.gif"
    car_images = ["car" + str(i) + ".gif" for i in range(5)]
    coin_images = ["Coin_Normal.gif","Coin_Picked.gif"]
    screen = init_screen(background_image,sport_car_image)
    
    "按回车开始游戏"
    screen.onkeypress(begin_game,"Return")     #绑定回车键到begin_game函数
    screen.listen()                
    screen.mainloop() 
    

星际赛车_python赛车游戏

如需要查看完整代码,请扫码付款:

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。扫码付款后的朋友可以加QQ: 406273900,提供技术支持及发送作品图片与配音素材。亦可直接向微信号scratch8付款购买。
李兴球的博客_Python创意编程技术前沿 » python星际赛车 源代码 硬核游戏!turtle star race arcade game

学本领,探索更大的世界!

李兴球博客 风火轮编程主页
error: Content is protected !!