python小游戏

python小游戏,第1张

import sys

import pygame

import random

class Bird(object): """定义一个鸟类"""

def __init__(self): self.birdRect=pygame.Rect(65,50,42,42) #创建小鸟矩形

self.birdStatus=[pygame.image.load("0.png"),       pygame.image.load("2.png"), pygame.image.load("dead.png")]

self.status=0

self.status+=1

self.birdX=120

self.birdY=100

self.jump=False

self.jumpSpeed=10

self.gravity=1

self.dead=False

def birdUpdate(self):

"""小鸟设置"""

if self.jump:

self.jumpSpeed-=1

self.birdY-=self.jumpSpeed

else:

self.gravity+=0.2

self.birdY+=self.gravity self.birdRect[1]=self.birdY

class Pipeline(object):

 """定义管道"""

def __init__(self):

self.wallx=400 self.pineUp=pygame.image.load("top.png")

#这些照片自己可以在网上找

self.muisc=300 self.pineDown=pygame.image.load("bottom.png") self.jia=5

self.hei=5

def updatePipeline(self):

"""管道设置"""

self.wallx=self.wallx-self.hei

self.muisc=self.muisc-self.jia

if self.wallx<-50:

global score #设置分数

score+=1

self.wallx=400

self.hei+=2

if self.muisc<-50:

score+=1

self.muisc=300

self.jia+=2 def createMap():

"""更新窗口"""

screen.fill((255,255,255)) #设置颜色 screen.blit(background,(0,0)) #设置位置 screen.blit(Pipeline.pineUp,(Pipeline.wallx,-200)) #位置的改变,分x轴和y轴 screen.blit(Pipeline.pineDown,(Pipeline.muisc,300)) Pipeline.updatePipeline()

if Bird.dead:

Bird.status=2

elif Bird.jump:

Bird.status=1

  screen.blit(Bird.birdStatus[Bird.status],(Bird.birdX,Bird.birdY))
    Bird.birdUpdate()
    screen.blit(font.render('Score:'+str(score),-1,(255,255,255)),(80,50))
    pygame.display.update() #更新设置
def checkDead():
    #小鸟是否死亡
    upRect=pygame.Rect(Pipeline.wallx,-200,Pipeline.pineUp.get_width()-110,Pipeline.pineUp.get_height())
    doRect=pygame.Rect(Pipeline.muisc,300,Pipeline.pineDown.get_width()-110,Pipeline.pineDown.get_height()-36)
    if upRect.colliderect(Bird.birdRect) or doRect.colliderect(Bird.birdRect):
        Bird.dead=True
    if not -18         Bird.dead=True
        return True
    else:
        return False
def getRect():
    """文字设置"""
    final_text1="Game Over"  #文字设置
    final_text2="You final score is"+str(score)
    ft1_font=pygame.font.SysFont("Arial",40)
    ft1_fonf=ft1_font.render(final_text1,1,(242,3,36))
    ft2_font=pygame.font.SysFont("Arial",1)
    ft2_fonf=font.render(final_text2,1,(253,177,6))
    screen.blit(ft1_fonf,[screen.get_width()/2-ft1_fonf.get_width()/2,80])
    screen.blit(ft2_fonf,[screen.get_width()/2-ft2_fonf.get_width()/2,110])
    pygame.display.flip()
if __name__=='__main__':
    """主函数和窗口"""
    pygame.init()
    pygame.font.init()
    font=pygame.font.SysFont(None,50)
    size =width,height=287,511
    screen=pygame.display.set_mode(size)
    clock=pygame.time.Clock()

    Pipeline=Pipeline()
    Bird=Bird()
    score=0
    while True:
      clock.tick(60)
      for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if (event.type==pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
             Bird.jump=True
             Bird.gravity=1
             Bird.jumpSpeed=13

      background=pygame.image.load("background.png")
      if checkDead():
          getRect()
      else:
           createMap()

 

 

最后本人不才  只能这样了  欢迎大佬的意见。

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/725843.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存