猫史档案馆


【Python作品分享】xiaoniaofeixiang-1-2-1-2【教程贴】

用户:Water__Water__查看:2 回复:2 评论:2 创建时间:2020-05-23T16:31:11


【作品展示】

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("翱翔吧!源码精灵")

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

class Bird():
    def __init__(self):

        self.birdSprites = [pygame.image.load("0.png"), pygame.image.load(
            "1.png"), pygame.image.load("2.png"), pygame.image.load("3.png")]
        self.a = 0
        self.birdX = 50
        self.birdY = 100
        self.jumpSpeed = 0
        self.gravity = 0

        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.offist = 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.offist)
        self.wallDownRect.center = (self.wallx, 520 - self.gap - self.offist)

    def wallUpdate(self):
        self.wallx -= 2

        if self.wallx < -80:
            self.wallx = 288
            self.offist = random.randint(-50, 50)

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


newBird = Bird()
newWall = Wall()

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

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()
    newBird.birdUpdate()

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

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

pygame.quit()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
PX侠客行PX侠客行

麻烦发一下素材

点赞0


评论


Water__Water__

center_imagecenter_imagecenter_imagecenter_imagecenter_imagecenter_imagecenter_image

点赞0


评论