Commit 7c4177e8 by BellCodeEditor

save project

parent 686c5f10
Showing with 46 additions and 27 deletions
File added
import pygame import pygame
from pygame import locals from pygame import locals,display,init
import random from pygame import event as _event
from pygame.key import get_pressed
from pygame.time import Clock as TIME
from pygame.image import load
from pygame.sprite import Sprite,Group,collide_rect
from pygame.font import Font
from random import choice,randint
pygame.init() # 初始化 init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类 class Block(Sprite): # 障碍物精灵类
'''
pygame.sprite.Sprite's lever up
'''
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__() super().__init__()
# 加载障碍物的图片 # 加载障碍物的图片
self.image = random.choice([image1, image2, image3]) self.image = choice([image1, image2, image3])
# 根据障碍物位图的宽高设置矩形 # 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
# 障碍物绘制坐标 # 障碍物绘制坐标
self.rect.x = 1000 self.rect.x = 1000
self.rect.y = 500 - self.rect.height self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite): # 悟空 class Player(Sprite): # 悟空
'''
pygame.sprite.Sprite's lever up
'''
def __init__(self, image): def __init__(self, image):
super().__init__() super().__init__()
# 加载悟空精灵图像 # 加载悟空精灵图像
...@@ -26,21 +39,22 @@ class Player(pygame.sprite.Sprite): # 悟空 ...@@ -26,21 +39,22 @@ class Player(pygame.sprite.Sprite): # 悟空
self.rect.y = 400 self.rect.y = 400
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑") display.set_caption("悟空酷跑")
# 载入图片 # 载入图片
background = pygame.image.load('bg.png') # 背景 background = load('bg.png') # 背景
road = pygame.image.load('road.png') # 路 road = load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头 stone = load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌 cacti = load('cacti.png') # 仙人掌
bush = pygame.image.load('bush.png') # 灌木丛 bush = load('bush.png') # 灌木丛
hero = [pygame.image.load('hero1.png'), hero = [load('hero1.png'),
pygame.image.load('hero2.png'), load('hero2.png'),
pygame.image.load('hero3.png'), load('hero3.png'),
pygame.image.load('hero4.png'), load('hero4.png'),
pygame.image.load('hero5.png')] load('hero5.png')]
basic_font = pygame.font.Font('STKAITI.TTF',32) gameover = load('gameover.png')
font = Font('STKAITI.TTF',32)
index = 0 index = 0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
...@@ -49,11 +63,12 @@ road_x = 0 ...@@ -49,11 +63,12 @@ road_x = 0
bg_x = 0 bg_x = 0
time = 0 time = 0
gamestate = True gamestate = True
score = 0
block_list =pygame.sprite.Group() # 创建精灵组 block_list = Group() # 创建精灵组
while True: while True:
for event in pygame.event.get(): for event in _event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
...@@ -102,7 +117,7 @@ while True: ...@@ -102,7 +117,7 @@ while True:
time += 1 time += 1
if time >= 60: # 创建障碍物精灵 if time >= 60: # 创建障碍物精灵
time = 0 time = 0
num = random.randint(0,50) num = randint(0,50)
if num > 20: if num > 20:
obstacle = Block(bush,cacti,stone) obstacle = Block(bush,cacti,stone)
block_list.add(obstacle) block_list.add(obstacle)
...@@ -112,12 +127,16 @@ while True: ...@@ -112,12 +127,16 @@ while True:
if sprite.rect.x <= 0-sprite.rect.width: if sprite.rect.x <= 0-sprite.rect.width:
sprite.kill() sprite.kill()
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测 if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
gameover = pygame.image.load('gameover.png') # 游戏结束 gameover = load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200)) screen.blit(gameover, (400, 200))
gamestate = False gamestate = False
scoreSurf = basic_font.render("分数:"+str(score),True,(255,0,0)) else:
if (sprite.rect.x + sprite.rect.width) < wukong.rect.x:
score += sprite.score
sprite.score = 0
scoreSurf = font.render("分数:"+str(score),True,(255,0,0))
screen.blit(scoreSurf,(850,20)) screen.blit(scoreSurf,(850,20))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) TIME().tick(60)
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment