猫史档案馆


【Python作品分享】翱翔吧,源码精灵!【作品秀】

用户:痴翰痴翰查看:0 回复:1 评论:0 创建时间:2020-02-29T12:19:22


【作品展示】

center_image

 

【作品介绍】

一款小游戏

 

【作品源代码】

import pygame
import random


#初始化,设置游戏界面信息
pygame.init()
screen = pygame.display.set_mode((288, 512))  # 屏幕大小
background = pygame.image.load("background.png")  # 背景图片
pygame.display.set_caption("翱翔吧!源码精灵")  # 标题
pygame.mixer.music.load('BGM.mp3')

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


#定义源码精灵类
class Bird():
    def __init__(self):

        self.birdSprites = [pygame.image.load("0.png"), pygame.image.load(
            "1.png"), pygame.image.load("2.png")]  # 列表                  #鸟图片
        self.a = 0
        self.birdX = 50  # 初始x坐标
        self.birdY = 100  # 初始y坐标
        self.jumpSpeed = 7  # 跳跃速度
        self.gravity = 0.4  # 跳跃重力

        self.rect = self.birdSprites[self.a].get_rect()
        self.rect.center = (self.birdX,self.birdY)

    def birdUpdate(self):
        self.jumpSpeed -= self.gravity
        self.birdY -= self.jumpSpeed
        self.rect.center = (self.birdX, self.birdY)
        if self.jumpSpeed < 0:
            self.a = 1
        if self.jumpSpeed > 0:
            self.a = 2

    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.offset = 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()



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

    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.wallUpdate()

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

    newBird.birdUpdate()
    pygame.display.update()
    clock.tick(60)

pygame.quit()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
superstar_pengsuperstar_peng

素材??????????????????????????????????

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

点赞0


评论