我用pygame来还原谷歌断网小游戏 dino

直接上图

同样素材没有的需要替换或者找我,代码有不懂的话可以看视频

import pygame
import time
import random
import time
pygame.init()
clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((900,300))

fenshu = pygame.font.SysFont("微软雅黑", 30)
new_fenshu = fenshu.render("score:", True, (0, 0, 0))

image_list = []
Cactus_list = []
number_list= []
b = 0
zaoxing = 0
sudu = 0
score = 0
picture = 0
color = (255,255,255)
game_open = 1
defeng = 0
pygame.mixer.Sound('background.ogg').play(-1)  # 0为只播放一次
class Way:
    def __init__(self,open = False):
        if open:
            self.image = pygame.image.load('dino/ground.png')
            self.rect = self.image.get_rect()
            self.rect.y = 280
            self.rect.x = 900
        else:
            self.image = pygame.image.load('dino/ground.png')
            self.rect = self.image.get_rect()
            self.rect.y = 280
            self.rect.x = 0
    def draw(self):
        self.rect.x -= 8
        if self.rect.x <= -900:
            self.rect.x = 900
        SCREEN.blit(self.image, self.rect)


class Dinosaur(pygame.sprite.Sprite):
    def __init__(self):

        for i in range(1, 7):
            self.image = pygame.image.load("dino/dino_run%s.png" % str(i))
            self.rect = self.image.get_rect(left = 60, top = 240, width=30, height=30)
            image_list.append(self.image)



    def draw(self):
        global b
        global sudu
        if b >= 6:
            b = 0
        # for event in pygame.event.get():
        #     if event.type == pygame.QUIT:
        #         exit()

        if self.rect.y >= 240:
            sudu = 0
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        pygame.mixer.Sound('preview.mp3').play(0)  # 0为只播放一次
                        sudu = -20

        # 注意这段代码,你看他是每次减一,那就是第一次是-20,然后-19,-18,-17,-16
        else:
            sudu -= -1
        print(self.rect.y)
        self.rect.y += sudu

        SCREEN.blit(image_list[b], self.rect)
        b += 1





class Cactus():
    def __init__(self):

        for i in range(1, 10):
            self.image = pygame.image.load("dino/cactus%s.png" % str(i))
            self.rect = self.image.get_rect(left = 800, top = 240, width=30, height=30)
            Cactus_list.append(self.image)

    def move(self):
        global defeng
        global zaoxing
        global score
        global color
        self.rect.x -= 8
        if self.rect.x <= -20:

            defeng += 1

            score += 1
            self.rect.x = 900
            zaoxing =random.randint(1,8)


        if score >= 3:
            color = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))
            score = 0

        SCREEN.blit(Cactus_list[zaoxing],self.rect)

    def event_watch(self):
        global game_open
        if self.rect.colliderect(din.rect):
               game =  GameOver()
               game.geameover_draw()
               restart = Restart()
               restart.restart_draw()
               pygame.display.update()


               game_open = 0
class GameOver:
    def __init__(self):
        self.image = pygame.image.load('dino/gameover.png')
        self.rect = self.image.get_rect(center = (450, 150))
    def geameover_draw(self):
        SCREEN.blit(self.image,self.rect)

class Restart:
    def __init__(self):
        self.image = pygame.image.load('dino/restart.png')
        self.rect = self.image.get_rect(center = (450, 180))

    def restart_draw(self):
        SCREEN.blit(self.image,self.rect)

    def chick(self):
        global game_open
        for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    x, y = event.pos
                    print(event.pos)

                    if self.rect.collidepoint(x, y):
                        game_open = 1
                        cactus.rect.x = 900
                        print(11111)



way = Way()
way1 = Way(True)

din = Dinosaur()

cactus = Cactus()
restart = Restart()

while True:
    if game_open == 1:
        SCREEN.fill(color)
        SCREEN.blit(new_fenshu,(10,10))

        new_defeng = fenshu.render(str(defeng), True, (0, 0, 0))
        SCREEN.blit(new_defeng,(100,10))


        way.draw()
        way1.draw()
        din.draw()
        cactus.move()
        cactus.event_watch()
        pygame.display.update()
        clock.tick(60)

    else:

        restart.chick()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值