python py 飞翔的小鸟

Code,注释详细,画的不好看

import pygame
import sys
import math
import time
import random
from pygame.locals import *

def f(x):
    return x*2.5,x

pygame.init()

# 设置屏幕宽度和高度
screen_width, screen_height = f(400)
# 创建屏幕对象
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置窗口标题
pygame.display.set_caption('flappy bird')

def Over():
    pygame.quit()
    sys.exit()
class Bird:
    def __init__(self):
        # 初始化小鸟的位置
        self.x = screen_width / 7
        self.y = screen_height / 2
        # 初始化小鸟的大小
        self.size = 10
        # 初始化小鸟翅膀的角度
        self.wing_angle = 0
        # 初始化小鸟翅膀的摆动速度
        self.wing_speed = 0.1
        # 初始化小鸟的颜色
        self.color = [(255, 0, 0),(255,136,0)]
        # 初始化小鸟的飞行速度
        self.speed = 0.01
        # 初始化小鸟的飞行角度
        self.angle = 0

    def draw(self):
        # 如果小鸟超出屏幕底部,则调用Over函数退出程序
        if self.y > screen_height:
            self.y = 400
        # 如果小鸟超出屏幕顶部,则将小鸟的位置设置为屏幕顶部
        if self.y < 0:
            self.y = 0
        
        # 计算小鸟翅膀的角度
        self.angle = self.wing_angle + math.pi / 6
        # 计算小鸟翅膀的位置
        wing_x1 = self.x - self.size * math.cos(self.wing_angle) * 5
        wing_y1 = self.y - self.size * math.sin(self.wing_angle) * 5
        wing_x2 = self.x - self.size * math.cos(self.angle) * 5
        wing_y2 = self.y - self.size * math.sin(self.angle) * 5

        # 绘制小鸟的翅膀
        points = [(wing_x1, wing_y1), (wing_x2, wing_y2), (self.x, self.y)]
        surface1 = pygame.draw.polygon(screen, self.color[1], points, 0)

        # 绘制小鸟的身体
        surface2 = pygame.draw.circle(screen, self.color[0], (self.x, self.y), self.size*2)

        # 绘制小鸟的眼睛
        pygame.draw.circle(screen, (0, 0, 0), (self.x + self.size / 3 * 2, self.y - self.size / 2), self.size / 4)

        self.surface = surface1.union(surface2)
        # 更新小鸟翅膀的角度
        self.wing_angle += self.wing_speed
        if self.wing_angle > math.pi / 6:
            self.wing_speed = -self.wing_speed
        elif self.wing_angle < -math.pi / 6:
            self.wing_speed = -self.wing_speed
        # 更新小鸟的位置
        self.y += 0.25

        # 暂停一段时间,控制小鸟的飞行速度
        time.sleep(self.speed)
    
    def __str__(self):
        return self.surface

class Pipe:
    def __init__(self):
        # 初始化管道的位置
        self.x = screen_width
        self.y = 0
        self.size = random.randint(100, 250)
        self.surface = None  # 添加surface属性
        self.surface2 = None  # 添加surface2属性

    def draw(self):
        # 绘制管道
        surface1 = pygame.draw.rect(screen, (0, 255, 0), (self.x, self.y, 30, self.size))
        surface2 = pygame.draw.ellipse(screen, (0, 255, 0), (self.x - 20, self.y + self.size - 15, 70, 30))
        self.surface = surface1.union(surface2)
        sz = 400 - self.size - 150
        surface1 = pygame.draw.rect(screen, (0, 255, 0), (self.x, 400 - sz, 30, sz))
        surface2 = pygame.draw.ellipse(screen, (0, 255, 0), (self.x - 20, 400 - sz - 15, 70, 30))
        self.surface2 = surface1.union(surface2)
        self.x -= 1
    
    def __str__(self):
        return self.surface

def main():
    # 创建小鸟对象
    bird = Bird()
    bird.draw()

    # 创建管道对象
    pipe = [Pipe(),Pipe()]
    for i in pipe:i.draw()
    pipe[1].x = 1500
    running = True
    # 主循环
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_SPACE or event.key == K_UP:
                    bird.y -= 10
                if event.key == K_DOWN:
                    bird.y += 10
        
        # 检测小鸟是否与管道发生碰撞
        for i in pipe:
            if bird.surface.colliderect(i.surface) or bird.surface.colliderect(i.surface2):
                Over()

        # 清空屏幕
        screen.fill((202, 235, 251))

        # 绘制小鸟和管道
        bird.draw()
        for i in pipe:
            i.draw()
            if i.x < 0:
                pipe.remove(i)
                pipe.append(Pipe())
        
        # 更新屏幕显示
        pygame.display.update()
    main()
main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值