飞行的小鸟论文python_python机器学习 玩飞行小鸟游戏

#coding:utf-8

import random

import PATH

import cv2

from itertools import cycle

FPS = 30

ScreenWidth = 288

ScreenHeight = 512

Image = PATH.load()

Screen = Image['background']

PipGapSize = 100 # 管道障碍物中间间隔的长度设置为100

Basey = 400 # 基底线所在的位置

index = random.randint(50, 250) #在gapYs中随机选取

PlayerWidth = Image['player'][0].shape[1]

PlayerHeight = Image['player'][0].shape[0]

PipeWidth = Image['pipe'][0].shape[1]

PipeHeight = Image['pipe'][0].shape[0]

BackgroundHeight = Image['background'].shape[0]

PlayerIndex = cycle([0, 1, 2, 1])

class Bird:

def __init__(self):

self.playerx = int(ScreenWidth * 0.2)

self.playery = int((ScreenHeight - PlayerHeight) / 2)

self.playerVy = 0

self.playerMaxVy = 8 # 小鸟最快的下落速度是每帧8格

self.playerMinVy = -6 # 小鸟最快的上升速度是每帧6格

self.playerAccY = 1 # 小鸟的下落加速度

self.playerFlapAcc = -8 #在小鸟扇动翅膀时的速度

self.playerFlapped = False # 标识符,小鸟扇动翅膀时为True

self.score = self.playerIndex = self.loopIter = 0

self.action = [0,0]

self.reward = 0.1

self.endpoint = False

self.screen = None

self.flag = 0

class GameState:

def __init__(self):

self.basex = 0

self.baseUpline = Image['base'].shape[0] - BackgroundHeight

newPipe1 = GetPipe()

self.upPipes = [

{'x': ScreenWidth, 'y': newPipe1[0]['y']},

]

self.downPipes = [

{'x': ScreenWidth, 'y': newPipe1[1]['y']},

]

self.pipeVx = -4 # 管道障碍物在x轴方向上的移动速度是每帧4格,向x轴负方向移动。就是向左

self.interval_n =0 #间隔加速度

self.interval =40 #间隔

# self.baseground=None #基础画面

# self.showground=None #显示画面

self.score = 0#通用分数

self.playerx = int(ScreenWidth * 0.2)

def pipeline(self):

self.baseground=Screen.copy()

# 管道障碍物向左移动时候的速度

for uPipe, lPipe in zip(self.upPipes, self.downPipes):

uPipe['x'] += self.pipeVx

lPipe['x'] += self.pipeVx

# 生成新的管道

self.interval_n +=1

if self.interval_n ==self.interval:

newPipe = GetPipe()

self.upPipes.append({'x': ScreenWidth+PipeWidth-1, 'y': newPipe[0]['y']})

self.downPipes.append({'x': ScreenWidth+PipeWidth-1, 'y': newPipe[1]['y']})

self.interval_n=0

# 当旧的管道移动到屏幕最左边的时候,将其从upPips与downPips中删除

if self.upPipes[0]['x'] <0:

self.upPipes.pop(0)

self.downPipes.pop(0)

#显示管子

for uPipe, lPipe in zip(self.upPipes, self.downPipes):

self.baseground[0:uPipe['y']+PipeHeight,uPipe['x']-PipeWidth if uPipe['x']-PipeWidth > 0 else 0: ScreenWidth if uPipe['x']>ScreenWidth else uPipe['x']]=Image['pipe'][0][abs(uPipe['y']):320,PipeWidth-uPipe['x'] if PipeWidth-uPipe['x'] > 0 else 0:ScreenWidth+PipeWidth-uPipe['x'] if ScreenWidth+PipeWidth-uPipe['x']>0 else PipeWidth]

self.baseground[lPipe['y']:400,lPipe['x']-PipeWidth if lPipe['x']-PipeWidth > 0 else 0:lPipe['x'] if ScreenWidth >ScreenWidth else lPipe['x']]=Image['pipe'][1][0:400-lPipe['y'],PipeWidth-lPipe['x'] if PipeWidth-lPipe['x'] > 0 else 0:ScreenWidth+PipeWidth-lPipe['x'] if ScreenWidth+PipeWidth-lPipe['x']>0 else PipeWidth]

self.showground = self.baseground.copy()

def frame_step(self, birds):

self.pipeline()

# 确定分数,在小鸟的x超过管道右边界的x时,获得的分数+1

if self.upPipes[0]['x']

self.score += 1

for bird in birds:

bird.screen = self.baseground.copy()

bird.reward = 0.1

bird.endpoint = False

# 0:小鸟不做任务事 1:小鸟扇动翅膀

if bird.action[1] == 1:

if bird.playery > -2 * PlayerHeight:

bird.playerVy = bird.playerFlapAcc

bird.playerFlapped = True

if (bird.loopIter + 1) % 3 == 0:

bird.playerIndex = next(PlayerIndex)

bird.loopIter = (bird.loopIter + 1) % 30

self.basex = -((-self.basex + 100) % self.baseUpline)

# 小鸟在y轴上的移动方式

if bird.playerVy < bird.playerMaxVy and not bird.playerFlapped:

bird.playerVy += bird.playerAccY

if bird.playerFlapped:

bird.playerFlapped = False

bird.playery += min(bird.playerVy, Basey - bird.playery - PlayerHeight)

if bird.playery < 0:

bird.playery = 0

#显示小鸟

bird.playery = int(bird.playery)

self.showground[bird.playery:PlayerHeight+bird.playery,bird.playerx:bird.playerx+PlayerWidth] = addpng(self.showground[bird.playery:PlayerHeight+bird.playery,bird.playerx:bird.playerx+PlayerWidth],Image['player'][bird.playerIndex])

bird.screen[bird.playery:PlayerHeight+bird.playery,bird.playerx:bird.playerx+PlayerWidth] = addpng(bird.screen[bird.playery:PlayerHeight+bird.playery,bird.playerx:bird.playerx+PlayerWidth],Image['player'][bird.playerIndex])

cv2.imshow(str(bird.flag),bird.screen)

# 碰撞检测。发生碰撞时结束游戏,并马上开始新一局的游戏

Crash = CrashHappen({'x': bird.playerx, 'y': bird.playery,

'index': bird.playerIndex},

self.upPipes, self.downPipes)

if Crash:

bird.score = self.score

bird.endpoint = True

# self.__init__()

bird.reward = -1

self.showground = showScore(self.score,self.showground)

# 游戏界面的显示

cv2.imshow("Screen",self.showground)

cv2.waitKey(10)

return birds

def addpng(img,png):

img = cv2.split(img)

png = cv2.split(png)

for i in range(3):

img[i] = img[i]*(255.0 - png[3])/255 + png[i]*(png[3]/255)

return cv2.merge(img)

def showScore(score,sc):

scorenum = [int(x) for x in list(str(score))]

Total = 0

for num in scorenum:

Total += Image['numbers'][num].shape[1]

Xoffset = int((ScreenWidth - Total) / 2)

for num in scorenum:

# Screen.blit(Image['numbers'][num], (Xoffset, ScreenHeight * 0.1))

sc[int(ScreenHeight * 0.1):int(ScreenHeight * 0.1)+Image['numbers'][num].shape[0],Xoffset:Xoffset+Image['numbers'][num].shape[1]]=addpng (sc[int(ScreenHeight * 0.1):int(ScreenHeight * 0.1)+Image['numbers'][num].shape[0],Xoffset:Xoffset+Image['numbers'][num].shape[1]],Image['numbers'][num])

Xoffset += Image['numbers'][num].shape[1]

return sc

def GetPipe():

gapYs = [20, 30, 40, 50, 60, 70, 80, 90] # 管道间隙的上边所在的y轴的值

index = random.randint(0, len(gapYs)-1) #在gapYs中随机选取

gapY = gapYs[index]

gapY += int(Basey * 0.2)

pipeX = ScreenWidth + 10

return [

{'x': pipeX, 'y': gapY - PipeHeight}, # 上管道的起始坐标值

{'x': pipeX, 'y': gapY + PipGapSize}, # 下管道的起始坐标值

]

def CrashHappen(player, upPipes, downPipes):

pi = player['index']

player['w'] = Image['player'][0].shape[1]

player['h'] = Image['player'][0].shape[0]

# 判断小鸟是否与地面相碰撞

if player['y'] + player['h'] >= Basey - 1:

return True

else:

for uPipe, lPipe in zip(upPipes, downPipes):

# 上下管道的矩形位置和长宽数据

if uPipe['x']-PipeWidth < player['x']+PlayerWidth/4*3 and player['x']+PlayerWidth/4

if player['y']+PlayerHeight/4

return True

return False

if __name__ == '__main__':

game = GameState()

birds = []

for i in range(1):

birds.append(Bird())

for _ in range(100):

birds[0].action[1]=1

birds = game.frame_step(birds)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,以下是一个使用Python实现小鸟飞行的简单示例: ```python import pygame import random # 初始化游戏 pygame.init() # 设置窗口大小 screen_width = 500 screen_height = 500 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Flappy Bird") # 加载小鸟图片 bird_img = pygame.image.load("bird.png") bird_rect = bird_img.get_rect() bird_rect.center = (screen_width // 2, screen_height // 2) # 设置地板 floor_img = pygame.image.load("floor.png") floor_rect = floor_img.get_rect() floor_rect.topleft = (0, screen_height - floor_rect.height) # 设置背景 background_img = pygame.image.load("background.png") background_rect = background_img.get_rect() # 设置重力和跳跃力度 gravity = 0.25 jump_force = -5 bird_movement = 0 # 游戏循环 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bird_movement = jump_force # 更新小鸟位置 bird_movement += gravity bird_rect.centery += bird_movement # 绘制背景 screen.blit(background_img, background_rect) # 绘制小鸟 screen.blit(bird_img, bird_rect) # 绘制地板 screen.blit(floor_img, floor_rect) # 更新屏幕 pygame.display.update() # 退出游戏 pygame.quit() ``` 请注意,上述示例中的图片文件需要根据实际情况进行替换。此示例使用了pygame库来实现游戏窗口和图像的绘制,通过键盘事件来控制小鸟的跳跃。小鸟的位置会受到重力的影响,每次按下空格键时,小鸟会向上跳跃一定的力度。游戏循环会不断更新小鸟的位置,并在每次循环中重新绘制背景、小鸟和地板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值