猫史档案馆


【Python作品分享】酷跑游戏-2-1-1-1【教程贴】

用户:Water__Water__查看:0 回复:2 评论:0 创建时间:2020-06-04T21:12:10


【作品展示】

center_image

 

【作品介绍】

键盘上键或空格控制

 

【作品源代码】

import pygame
import random
import time

pygame.init()
keep_going = True
cnt = 0
failed = pygame.mixer.Sound("failed.wav")
timer = pygame.time.Clock()
font = pygame.font.Font("方正兰亭刊黑_GBK.TTF", 28)
failed_screen = pygame.image.load('失败.png')

class Cacti():
    def __init__(self):
        self.img = pygame.image.load('仙人掌.png')
        self.img = pygame.transform.scale(self.img, (self.img.get_width() // 4, self.img.get_height() // 4))                        
        self.x = 1000 + random.randint(200,500)
        self.y = 300 - self.img.get_height()
        self.speed_x = -20
        self.rect = self.img.get_rect()
        self.rect.center = (self.x + self.img.get_width() / 2, self.y + self.img.get_height() / 2)

    def get_position(self):
        self.x += self.speed_x
        if self.x < -1 * self.img.get_width():
            self.x = 1000 + random.randint(200,500)
        self.rect.center = (self.x + self.img.get_width() / 2, self.y + self.img.get_height() / 2)
        return (self.x, self.y)


class Cat():
    def __init__(self):
        self.pose_list = [pygame.image.load("cat_{}.png".format(i)) for i in range(29)]
        self.x = 100
        self.y = 100
        self.y_speed = 0
        self.jumpspeed = -12
        self.gravity = 0.8
        self.isJumping = False
        self.rect = self.pose_list[0].get_rect()
        self.rect.center = (self.x + self.pose_list[0].get_width() / 2, self.y + self.pose_list[0].get_height() / 2)
        self.score = 0
        self.isLand = False
        self.is_Crush = False

    def get_position(self):
        if self.y > 100:
            self.y = 100
            self.isJumping = False
            self.y_speed = 0
            print("落地了")
            self.isLand = True
        if self.isJumping:
            self.y += self.y_speed
            self.y_speed += self.gravity
            self.rect = self.pose_list[0].get_rect()
        self.rect.center = (self.x + self.pose_list[0].get_width() / 2, self.y +self.pose_list[0].get_height() / 2)
        return(self.x, self.y)

    def jump(self):
        if self.y == 100:
            self.isJumping = True
            self.y_speed = self.jumpspeed
            self.isLanded = False

    def Crush(self, Obj):
        if self.rect.colliderect(Obj.rect):
            failed.play()
            self.score = 0
            self.is_Crush = True
        return self.is_Crush

    def Pass(self, Obj):
        if (Obj.x < self.x  or Obj.x > 1000) and self.isLand == True and self.is_Crush == False:
            self.score += 1
            print("加分了!当前分数:", self.score)
            self.isLand = False

codemao = Cat()
cacti = Cacti()
while keep_going:
    screen = pygame.display.set_mode([1000, 300])
    bg_color = (255, 255, 255)
    screen.fill(bg_color)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
                print("编程猫跳")
                codemao.jump()

    screen.blit(codemao.pose_list[cnt % 29], codemao.get_position())
    screen.blit(cacti.img, cacti.get_position())
    if codemao.Crush(cacti):
        screen.blit(failed_screen, (500, 20))
        keep_going = False

    codemao.Pass(cacti)
    text = font.render("得分:" + str(codemao.score), True, (0, 0, 0))
    screen.blit(text, (800, 20))
    pygame.display.update()
    cnt += 1
    timer.tick(100)
time.sleep(3)
pygame.quit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
我叫zzt我叫zzt

收藏

点赞0


评论


玄离俊秀的狼王玄离俊秀的狼王

第七行要打开一个东西

点赞1


评论