Commit 90aa842a by BellCodeEditor

save project

parent cfc5fb1e
File added
bg.png

200 KB

bush.png

14.5 KB

cacti.png

5.58 KB

hero1.png

12.4 KB

hero2.png

10.9 KB

hero3.png

11.3 KB

hero4.png

11.5 KB

hero5.png

11.9 KB

import pygame
from pygame import locals
import random
pygame.init() # 初始化
class Zhangai(pygame.sprite.Sprite):#子类Zhangai继承父类精灵
def __init__ (self,image1,image2,image3):#形参,后面再用实参
super().__init__()#继承
self.image = random.choice([image1,image2,image3])#随机选,后面用实参
self.rect =self.image.get_rect()#前面要加self
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite):#子类Player继承父类精灵
def __init__ (self,image):#形参,后面再用实参
super().__init__()#继承
self.image = image#后面用实参
self.rect =self.image.get_rect()#前面要加self
self.rect.x = 150#初始x坐标
self.rect.y = 400#初始y坐标
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
bush = pygame.image.load('bush.png') # 灌木丛
hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
basic_font = pygame.font.Font('STKAITI.TTF',38)
score_sound = pygame.mixer.Sound('score.wav')#设置音效
index = 0
jumpstate = 'runing'#初始状态
y = 400
t = 30
road_x = 0
bg_x = 0
time = 0
gamestate = True#游戏状态,后面会变成False,只有True才会运行
score = 0
oldscore = 0
speed = 8#初始速度
#zhangai = Zhangai(stone,cacti,bush)#设置变量
zhangai_list = pygame.sprite.Group()#创建精灵组
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if jumpstate == 'runing':#只有在跑步状态才能跳
if event.key == locals.K_SPACE:
jumpstate = 'up'#按下空格,模式为跳跃
speed = 8 + score//3#分数每
wukong = Player(hero[index])#变向遍历列表
if jumpstate == 'runing':#只有在跑步才能切换造型
index += 1#索引增加
if index >= 5:#如果大于索引
index = 0#改为零
if gamestate == True:#只有在True状态下才能渲染
if jumpstate =='up':#起跳
if t >0:#小于起跳最大值
y -= t#起跳
wukong.rect.y = y#避免出现人在天上飞却死了的bug
t -= 2
else:
jumpstate = 'down'#落下
if jumpstate == 'down':#降落
if t <= 30:#大于落地最大值
y += t#落下
wukong.rect.y = y#同理
t += 2
else:
jumpstate = 'runing'#跑步
t = 30#重定义t
# 将背景图画上去
bg_x -= 1#移动
if bg_x <= -1000:#因为背景长2000,且为俩段长度为1000的相同图片合成
bg_x = 0#重滚
screen.blit(background, (bg_x, 0))
road_x -= speed
if road_x <= -1000:#与背景相同
road_x = 0
screen.blit(road, (road_x, 500))
screen.blit(wukong.image, (150,y))#渲染名要换一个
time+=1#每渲染一次加一
if time >=60:#如果渲染了一秒,因为帧数设置为60
r = random.randint(0,100)#随机选
if r >40:#这样只有随机选时达与40才能渲染,相当于只有60%的概率渲染
zhangai = Zhangai(stone,cacti,bush)#渲染
zhangai_list.add(zhangai)#添加进列表
time = 0#重新设为零
for prop in zhangai_list:#遍历
prop.rect.x -=speed#移动
screen.blit(prop.image, (prop.rect.x, prop.rect.y))#渲染
if prop.rect.x <= 0-prop.rect.width:#超出界限时
prop.kill()#杀掉精灵
if pygame.sprite.collide_rect(wukong,prop):#检测悟空是否与障碍碰撞
gameover = pygame.image.load('gameover.png')#展示失败
screen.blit(gameover,(400,200))#失败位置
gamestate = False#变为False
else:
if prop.rect.x + prop.rect.width <wukong.rect.x:
score += prop.score
prop.score = 0
if score > oldscore:
score_sound.play()
oldscore = score
#if zhangai.rect.x <= 0-zhangai.rect.width:#障碍物完全消失在窗口
# zhangai = Zhangai(stone,cacti,bush)#刷新
#zhangai.rect.x -=8 #左移
#screen.blit(zhangai.image, (zhangai.rect.x, zhangai.rect.y))#渲染
scoreSurf = basic_font.render('分数:'+str(score),True,(200,200,200))
screen.blit(scoreSurf,(880,20))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
{"第1名": 0, "第2名": 0, "第3名": 0}
\ No newline at end of file
road.png

34.7 KB

File added
stone.png

11.3 KB

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