跑酷游戏源代码

您所在的位置:网站首页 unity3d游戏代码 跑酷游戏源代码

跑酷游戏源代码

2023-03-01 11:43| 来源: 网络整理| 查看: 265

import pygame, sys import random

class Person():  # 人物     def __init__(self, surf=None, y=None):         self.surface = surf         self.y = y  # y坐标         self.w = (surf.get_width()) / 12  # 宽度         self.h = surf.get_height() / 2  # 高度         self.cur_frame = -1  # 当前的运动状态帧         self.state = 0  # 0代表跑步状态,1代表跳跃状态,2代表连续跳跃         self.gravity = 1  # 重力加速度         self.velocity_y = 0  # y方向的速度         self.vy_start = -20  # 起跳开始速度

    def getPos(self):   # 获取当前的位置信息,用于碰撞检测         return (0, self.y + 12, self.w, self.h)

class Obstacle(object):  # 障碍物     def __init__(self, surf, x=0, y=0):         self.surface = surf         self.x = x         self.y = y         self.w = surf.get_width()         self.h = surf.get_height()         self.cur_frame = random.randint(0, 6)  # 随机获取一种障碍物的类型         self.w = 100         self.h = 100

    def getPos(self):  # 当前的坐标信息         return (self.x, self.y, self.w, self.h)

    def judgeCollision(self, rect1, rect2):  # 碰撞检测         if (rect2[0] >= rect1[2] - 20) or (rect1[0] + 40 >= rect2[2]) or (rect1[1] + rect1[3] < rect2[1] + 20) or (                 rect2[1] + rect2[3] < rect1[1] + 20):             return False         return True

class BackGround(object):  # 背景     def __init__(self, surf):         self.surface = surf  # 初始化一个Surface 对象         self.dx = -10         self.w = surf.get_width()    # 返回 Surface 对象的宽度,以像素为单位。         self.rect = surf.get_rect()  # 获取 Surface 对象的矩形区域

class PaoKu(object):     def __init__(self):         pygame.init()         pygame.mixer.init()

        self.width = 1200  # 窗口宽度         self.height = 500  # 窗口高度         self.size = (self.width, self.height)         self.screen = pygame.display.set_mode(self.size)         pygame.display.set_caption('跑酷运动')         self.score = 0  # 分数         self.font1 = pygame.font.Font("resource/simkai.ttf", 32)         self.font2 = pygame.font.Font("resource/simkai.ttf", 64)  # 字体         self.obstacle_pic = pygame.image.load("resource/obstacles.png").convert_alpha()  # 障碍物图片         self.game_over = pygame.image.load("resource/gameover.bmp").convert_alpha()  # 游戏结束图片         self.bg = BackGround(pygame.image.load("resource/bg.png").convert_alpha())  # 背景对象         self.person = Person(pygame.image.load("resource/person.png").convert_alpha(), 500 - 85)  # 人物对象         self.screen.blit(self.bg.surface, [0, 0])  # 初始化游戏背景

        self.obstacle_list = []  # 障碍物对象数组         self.game_state = 0  # 游戏状态,0表示游戏中,1表示游戏结束         self.life = 3        # 初始的生命值         self.clock = pygame.time.Clock()  # 时钟         self.bg_music = pygame.mixer.Sound(r"resource\bgm.wav").play(-1, 0)  # 循环播放北京音乐

    def startGame(self, screen):   # 开始游戏界面         gameStart = pygame.image.load("resource/start1new.png")         screen.blit(gameStart, (0, 0))         font = pygame.font.SysFont("resource/simkai.ttf", 70)         tip = font.render("Press Any Key To Start!, Press Esc To Quit", True, (65, 105, 225))         screen.blit(tip, (self.width / 2 - 550, self.height / 2 + 150))         pygame.display.update()         while True:             for event in pygame.event.get():  # 关闭窗口                 if event.type == pygame.QUIT:                     self.terminate()                 elif event.type == pygame.KEYDOWN:                     if (event.key == pygame.K_ESCAPE):  # 按下ESC键                         self.terminate()                     else:                         return     def addObstacle(self):   # 添加障碍物         rate = 4         # 是否生成障碍物         if not random.randint(0, 300) < rate:             return         y = random.choice([self.height - 100, self.height - 200, self.height - 300, self.height - 400])         obstacle = Obstacle(self.obstacle_pic, self.width + 40, y)         self.obstacle_list.append(obstacle)     # 监听键盘事件,并做处理     def ListenKeyBoard(self):  # 键盘事件处理

        for event in pygame.event.get():             if event.type == pygame.QUIT:                 sys.exit()             elif event.type == pygame.KEYDOWN:                 # 空格键跳跃                 if self.game_state == 0:                     if event.key == pygame.K_SPACE:                         pygame.mixer.Sound(r"resource\jump.wav").play()                         if self.person.state == 0:                             self.person.state = 1                             self.person.velocity_y = self.person.vy_start                         elif self.person.state == 1:                             self.person.state = 2                             self.person.velocity_y = self.person.vy_start                 elif self.game_state == 1:                     if event.key == pygame.K_RETURN:   # 重新开始游戏                         self.bg_music.stop()                         self.__init__()

        if self.game_state == 0:             # BackGorund的运动             self.bg.dx += 10             if self.bg.dx == 1200:                 self.bg.dx = 0

                # Person的移动             if self.person.state == 0:                 self.person.cur_frame += 1                 if self.person.cur_frame == 12:                     self.person.cur_frame = 0             else:                 self.person.y += self.person.velocity_y                 self.person.velocity_y += self.person.gravity                 if self.person.y >= 500 - 85:                     self.person.y = 500 - 85                     self.person.state = 0             # bstacle的操作             self.addObstacle()

            for obstacle in self.obstacle_list:                 obstacle.x -= 10# obstacle向左移动十个像素

                if obstacle.x + obstacle.w



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3