Mozilla 发表于 2020-7-11 20:10:08

秀 技 术

改天我再写个网络爬虫的程序。
需要准备:1、至少安装python3.6及以上版本。
2、安装完成后直接输入命令行pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pygame后等待安装完pygame库。
3、下载帖子附件,解压后请不要对文件夹做任何更改。直接运行那个.py文件即可。
接下来是代码:
import pygame
import random
import time

pygame.init()
screen=pygame.display.set_mode()#此为assets/background.png背景图片尺寸

background=pygame.image.load('assets/background.png')#背景图片
pygame.display.set_caption('Flappy Bird')#标题

bgm=pygame.mixer.Sound('sound/bgm2.wav')#mixer专门负责音乐模块的混音器,用Sound拿到一个新的声音文件
channel_1=pygame.mixer.Channel(1)
channel_1.play(bgm)#用频道(channel)1来播放(play)这个声音文件bgm2.wav

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

class Bird(pygame.sprite.Sprite):   #pygame.sprite.Sprite包含编写游戏对象时所需的很多功能
      def __init__(self):
                pygame.sprite.Sprite.__init__(self)#调用主Sprite类的初始化函数
                self.birdSprites=[pygame.image.load('assets/0.png'),
                pygame.image.load('assets/1.png'),
                pygame.image.load('assets/2.png')]#3个造型
                self.a=0#相当于newBird.birdSprites
                self.birdX=50
                self.birdY=100#初始的XY
                self.jumpSpeed=15#跳跃速度
                self.gravity=0.55#跳跃重力
                #让小鸟图片外接一个矩形
                self.rect=self.birdSprites.get_rect()#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 newBird.jumpSpeed<0:#下落造型
                        self.a=1
                if newBird.jumpSpeed>0:#上升造型
                        self.a=2

      def birdCrush(self):
                global keep_going
                #调用矩形检测,若撞上水管,返回True
                resultU=self.rect.colliderect(newWall.wallUpRect)
                resultD=self.rect.colliderect(newWall.wallDownRect)

                if resultU or resultD or newBird.rect.top>512:
                        hit=pygame.mixer.Sound('sound/hit.wav')
                        channel_3=pygame.mixer.Channel(3)
                        channel_3.play(hit)
                        time.sleep(0.3)
                        keep_going=False#撞上/坠地就停止
                        print(er)

class Wall():
      def __init__(self):
                self.wallUp=pygame.image.load('assets/bottom.png')#上水管图片
                self.wallDown=pygame.image.load('assets/top.png')#下水管图片
                #水管附上矩形
                self.wallUpRect=self.wallUp.get_rect()
                self.wallDownRect=self.wallDown.get_rect()

                self.gap=random.randint(50,100)#上下水管间隔
                self.wallx=288#水管的x坐标
                self.offset=random.randint(-50,50)#水管偏移量

                #设置并存储俩水管的Y坐标
                self.wallUpY=360+self.gap-self.offset
                self.wallDownY=0-self.gap-self.offset
                #矩形中心跟着水管图像移动
                self.wallUpRect.center=(self.wallx,self.wallUpY)
                self.wallDownRect.center=(self.wallx,self.wallDownY)

      def wallUpdate(self):
                self.wallx-=2
                #矩形中心重复跟着水管图像移动
                self.wallUpRect.center=(self.wallx,self.wallUpY)
                self.wallDownRect.center=(self.wallx,self.wallDownY)

                if self.wallx<-80:
                        self.wallx=288
                        self.offset=random.randint(-50,50)
                        #重复设置并存储俩水管的Y坐标
                        self.wallUpY=360+self.gap-self.offset
                        self.wallDownY=0-self.gap-self.offset



newBird=Bird()#这个Bird()等于上面的self
newWall=Wall()

er='{{{Made by Mozilla!!!}}}'

while keep_going:
      for event in pygame.event.get():
                if event.type==pygame.QUIT:
                        keep_going=False
                        print(er)
                if event.type==pygame.MOUSEBUTTONDOWN:
                        newBird.jumpSpeed=7
                        #用频道2来播放飞行音效fly.WAV
                        channel_2=pygame.mixer.Channel(2)
                        fly=pygame.mixer.Sound('sound/fly.WAV')
                        channel_2.play(fly)

      screen.blit(background,(0,0))
      screen.blit(newBird.birdSprites,newBird.rect)
      screen.blit(newWall.wallUp,newWall.wallUpRect)
      screen.blit(newWall.wallDown,newWall.wallDownRect)

      newWall.wallUpdate()
      newBird.birdUpdate()#运行定义的函数,见Ln22
      newBird.birdCrush()
      pygame.display.update()
      clock.tick(60)

pygame.quit独立完成。
话说@-墟- 你是不是也该多秀秀你过硬的C++算法技术!

Mozilla 发表于 2020-7-11 20:32:29

游戏玩法可以去网上搜搜,很知名的游戏

黄蕊小白花 发表于 2020-7-11 20:39:39

牛逼了,你这个这么多,我那个太少了,干白皙风。
网络爬虫不要太爽!我用他自动抓取妹子涩图{:2_31:}

admin 发表于 2020-7-11 20:46:32

黄蕊小白花 发表于 2020-7-11 20:39
牛逼了,你这个这么多,我那个太少了,干白皙风。
网络爬虫不要太爽!我用他自动抓取妹子涩图 ...

额~

汪子凯 发表于 2020-7-12 17:30:36

@admin 话说你什么时候你也弄个视频链接生成器;P

admin 发表于 2020-7-12 19:03:43

汪子凯 发表于 2020-7-12 17:30
@admin 话说你什么时候你也弄个视频链接生成器

图床好整,视频不好弄,还没人提供免费视频床呢

admin 发表于 2020-7-16 17:57:07

20.8M,,,我得做个限制了。。。

Mozilla 发表于 2020-7-16 18:51:59

admin 发表于 2020-7-16 17:57
20.8M,,,我得做个限制了。。。

什么?

admin 发表于 2020-7-16 18:54:32

Mozilla 发表于 2020-7-16 18:51
什么?

(ー_ー)!!您上传的附件大小

Mozilla 发表于 2020-7-16 18:55:16

admin 发表于 2020-7-16 18:54
(ー_ー)!!您上传的附件大小

我换个.7z的试试
页: [1] 2 3 4
查看完整版本: 秀 技 术