猫史档案馆


【Python作品分享】翱翔吧!源码精灵【教程贴】

用户:yj皓月当空yj皓月当空查看:0 回复:0 评论:0 创建时间:2020-03-14T09:42:32


【作品展示】

center_image

 

【作品介绍】

 

【作品源代码】

#导入库
import pygame
import random

#游戏初始化
pygame.init()
screen = pygame.display.set_mode((288, 512))
background = pygame.image.load("背景.png")
pygame.mixer.music.load("bgm.mp3")  # 加载BGM
pygame.mixer.music.play(-1)

pygame.display.set_caption("翱翔吧~源码神精灵")


#获得游戏素材
class Bird():
    def __init__(self):
        self.birdSprites = [pygame.image.load("鸟{}.png".format(i)) for i in range(1, 4)]  # 加载图片
        self.a = 0
        self.birdX = 50
        self.birdY = 100
        self.jumpSpeed = 0  # 起跳速度
        self.gravity = 0.4  # 重力下降速度

        self.rect = self.birdSprites[self.a].get_rect()  #获取当前角色矩形框
        self.rect.center = (self.birdX, self.birdY)  #把中心点绑定在图片左上角

        if self.jumpSpeed < 0:
            self.a = 1
        if self.jumpSpeed > 0:
            self.a = 2

    def birdUpdate(self):
        self.jumpSpeed -= self.gravity  # 飞翔速度加快
        self.birdY -= self.jumpSpeed  # 更新坐标
        self.rect.center = (self.birdX, self.birdY)  #中心点绑定在左上角

    def birdCrush(self):
        resultU = self.rect.colliderect(newWall.wallUpRect)
        resultD = self.rect.colliderect(newWall.wallDownRect)
        if resultU or resultD or newBird.rect.top > 512:

            return True


class Wall():
    def __init__(self):
        self.wallDown = pygame.image.load("bottom.png")
        self.wallUp = pygame.image.load("top.png")
        self.wallx = 288  #柱子移动

        self.gap = 100  #间隔
        self.offset = random.randint(-50, 50)  #偏移量

        self.wallUpRect = self.wallUp.get_rect()
        self.wallDownRect = self.wallDown.get_rect()

        self.wallUpRect.center = (self.wallx, 0 - self.gap - self.offset)
        self.wallDownRect.center = (self.wallx, 520 - self.gap - self.offset)


    def wallUpdate(self):
        self.wallx -= 2  #柱子向鸟移动

        if self.wallx < -80:
            self.wallx = 288
            self.offest = random.randint(-50, 50)  #偏移量

        self.wallUpRect.center = (self.wallx, 0 - self.gap - self.offset)
        self.wallDownRect.center = (self.wallx, 520 - self.gap - self.offset)


newBird = Bird()
newWall = Wall()


# background = pygame.image.load("背景.png")
# newBird = pygame.image.load("鸟1.png")
# birdX = 50
# birdY = 100
# jumpSpeed = 0  #起跳速度
# gravity = 0.4  #因为重力下降的速度

clock = pygame.time.Clock()
keep_going = True



#pygame循环,源码精灵翱翔
while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False

        if (event.type == pygame.MOUSEBUTTONDOWN):
            newBird.jumpSpeed = 7

    newBird.birdUpdate()

    screen.blit(background, (0, 0)) 
    screen.blit(newBird.birdSprites[newBird.a], newBird.rect)

    screen.blit(newWall.wallUp, newWall.wallUpRect)
    screen.blit(newWall.wallDown, newWall.wallDownRect)  # (newWall.wallx, 520 - newWall.gap - newWall.offset))
    newWall.wallUpdate()

    if newBird.birdCrush():
        print("撞上了!")
        keep_going = False
    

    pygame.display.update()
    clock.tick(30)

    


#退出游戏
pygame.quit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页