Commit bfb06e11 by BellCodeEditor

auto save

parent 022ec8df
File added
import pygame
from pygame import locals
import random
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
self.image = random.choice([image1, image2, image3])
self.rect = self.image.get_rect()
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = 100
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑")
# 载入图片
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')]
index = 0
y = 400
jumpState = "runing"
t = 32
# +++++++++++++++++++++++
bg_x = 0
road_x = 0
# +++++++++++++++++++++++
block_list = pygame.sprite.Group() #创建一个精灵组
time = 0
gamestate = True
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"
if gamestate == True:
# 悟空造型
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 32:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t =32
# 将背景图画上去
# +++++++++++++++++++++++
bg_x -= 1
if bg_x<=-1000:
bg_x = 0
screen.blit(background, (bg_x, 0))
road_x -= 10
if road_x<=-1000:
road_x = 0
screen.blit(road, (road_x, 500))
# +++++++++++++++++++++++
screen.blit(wukong.image, (wukong.rect.x, y)) # 悟空
time += 1
if time > 60:
time = 0
num = random.randint(0,50)
if num > 20 :
obstacle = Block(bush,stone,cacti)
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x -= 10
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width:
sprite.kill()
if pygame.sprite.collide_rect(wukong,sprite):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate = False
# print(len(block_list))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame
from pygame import locals
import random
pygame.init() # 初始化
# 创建一个窗口
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')]
index = 0
jumpState='running'
y=400
t=30
obstacle=random.choice([bush,stone,cacti])
rect= obstacle.get_rect()
print(rect[3])
rect_x=1000
rect_y=500-rect.height
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type==locals.KEYDOWN:
if jumpState=='running':
if event.key==locals.K_SPACE:
jumpState='up'
if jumpState=='up':
if t>0:
y-=t
t-=2
else:
jumpState='down'
if jumpState=='down':
if t<=30:
y+=t
t+=2
else:
jumpState='running'
t=30
wukong=hero[index]
index+=1
if index==5:
index=0
# 将背景图画上去
screen.blit(background,( 0, 0))
screen.blit(road,(0, 500))
screen.blit(wukong,(150, y))
# if rect.x <= 0-rect.width:
if rect_x < 0-rect.width:
obstacle=random.choice([bush,stone,cacti])
rect= obstacle.get_rect()
rect_x=1000
rect=500-rect.height
rect_x -= 8
screen.blit(obstacle,(rect_x,rect_y))
# 刷新画面
pygame.display.update()
FPS.tick(30)
import pygame
from pygame import locals
import random
pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类
def __init__(self,image1,image2,image3):
super().__init__()
# 加载障碍物的图片
self.image = random.choice([image1, image2, image3])
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite): # 悟空
def __init__(self, image):
super().__init__()
# 加载悟空精灵图像
self.image = image
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑")
# 载入图片
background = pygame.image.load('bg.png') # 背景
my_font = pygame.font.Font('STKAITI.TTF',20) #字体
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')]
index = 0
y = 400
jumpState = "runing"
t = 30
road_x = 0
bg_x = 0
time = 0
gamestate = True
block_list =pygame.sprite.Group() # 创建精灵组
score = 0 #分数
speed = 8
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:
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t =30
# 将背景图画上去
bg_x -= 1
if bg_x<=-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: # 创建障碍物精灵
time = 0
num = random.randint(0,50)
if num > 20:
obstacle = Block(bush,cacti,stone)
block_list.add(obstacle)
for sprite in block_list: # 遍历、展示障碍物精灵
sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width:
sprite.kill()
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200))
gamestate = False
#++++++++++++++++++++++++++++++++++++++++++
#设置加分功能
else:
if sprite.rect.x+sprite.rect.width < wukong.rect.x:
score += sprite.score
sprite.score = 0
print(score)
info = '分数:'+str(score)
test = my_font.render(info,True,(0,0,0))
screen.blit(test,(900,20))
#++++++++++++++++++++++++++++++++++++++++++++
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame import pygame
from pygame import locals from pygame import locals
import json
pygame.init() # 初始化 pygame.init() # 初始化
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
...@@ -14,12 +14,13 @@ apple = pygame.image.load('bush.png') # 灌木丛 ...@@ -14,12 +14,13 @@ apple = pygame.image.load('bush.png') # 灌木丛
hero = pygame.image.load('hero1.png') hero = pygame.image.load('hero1.png')
index = 0 index = 0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
screen.blit(road, (0, 500)) screen.blit(road, (0, 500))
......
import pygame
import pygame
from pygame import locals
from random import *
import time
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
# 加载障碍物的图片
self.image = choice([image1, image2, image3])
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite): #悟空
def __init__(self,image1):
super().__init__()
# 加载障碍物的图片
self.image = image1
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑")
# 载入图片
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')]
my_font = pygame.font.Font('STKAITI.TTF',25)
music = pygame.mixer.Sound('score.wav') #加载音乐
old_score = 0 #旧分数
index = 0
y = 400
jumpState = "runing"
t = 30
bg_x = 0
road_x = 0
# 创建障碍物
obstacle = Block(bush,cacti,stone)
#创建一个精灵组
block_list = pygame.sprite.Group()
time1 = 0
gamestate = True
score = 0
speed = 10
with open('score.txt','r',encoding='utf-8')as file:
content = file.read()
record= json.loads(content)
print(type(record))
one = record["第1名"]
two = record["第2名"]
three = record["第3名"]
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 = 10 + score // 3
# print(speed)
if gamestate == True:
# 悟空造型
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t =30
# 将背景图画上去
bg_x -= 1
if bg_x<=-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)) # 悟空
# ++++++++++++++++++++++
time1 += 1
if time1 >= 60:
time1 = 0
num = randint(10,50)
if num >= 20:
obstacle = Block(bush,cacti,stone)
block_list.add(obstacle)
#遍历精灵组
for sprite in block_list:
sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0 - sprite.rect.width:
sprite.kill() #删除精灵
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200))
gamestate = False
if score > three :
if score > one :
record['第1名'] = score
record['第2名'] = one
record['第3名'] = two
elif score > two :
record['第2名'] = score
record['第3名'] = two
else:
record['第3名'] = score
record = json.dumps(record,ensure_ascii=False) #把字典转化成字符串
# print(record)
with open('score.txt','w',encoding='utf-8')as file:
file.write(record)
pygame.display.update()
time.sleep(1)
exit()
else:
if sprite.rect.x + sprite.rect.width < wukong.rect.x:
score += sprite.score
sprite.score = 0
# print(score)
if score > old_score:
music.play()
old_score = score
# print(len(block_list))
test = my_font.render('分数:'+str(score),True,(255,255,255))
screen.blit(test,(880,20))
test = my_font.render('第1名'+str(one),True,(255,255,255))
screen.blit(test,(880,55))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
{"第1名": 2, "第2名": 1, "第3名": 1}
\ No newline at end of file
{"第1名": 0, "第2名": 0, "第3名": 0} {"第1名": 1, "第2名": 1, "第3名": 1}
\ No newline at end of file \ No newline at end of file
File added
import pygame
import pygame
from pygame import locals
import random
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
# 加载障碍物的图片
self.image = random.choice([image1,image2,image3])
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
#设置x,y坐标
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
# 加载障碍物的图片
self.image = image
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
#设置x,y坐标
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption('悟空酷跑') #设置窗口标题
# 载入图片
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')]
index = 0
jumpState = 'running'
y = 400
t = 30
road_x = 0
bg_x = 0
block_list = pygame.sprite.Group() #创建一个精灵组
time = 0
gamestate = True
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE and jumpState == 'running':
jumpState = 'up'
wukong = Player(hero[index])
if jumpState == 'running':
index+=1
if index > 4 :
index = 0
if gamestate == True:
if jumpState == 'up' : #起跳
if t > 0 :
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down': #落下
if t <= 30 :
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -1000:
bg_x = 0
screen.blit(background, (bg_x, 0))
road_x -= 8
if road_x <= -1000:
road_x = 0
screen.blit(road, (road_x, 500)) #路
screen.blit(wukong.image, (150, y))
time += 1
if time >= 60:
time = 0
num = random.randint(0,60)
if num > 20:
obstacle = Block(stone,cacti,bush)
block_list.add(obstacle) #添加到精灵组中
for sprite in block_list:
sprite.rect.x -= 8
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y)) #把障碍物绘制到窗口上
if sprite.rect.x <= 0 - sprite.rect.width:
sprite.kill() #删除精灵
if pygame.sprite.collide_rect(wukong,sprite): #判断是否发生碰撞
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate = False
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame
import pygame
from pygame import locals
import random
pygame.init() # 初始化
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('悟空酷跑')
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')]
index = 0
y = 400
jumpState = 'running'
t = 30
#生成障碍物图片获去 x,y以及宽高
obstacle = random.choice([stone,cacti,bush])
rect = obstacle.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN: #按下键盘
if jumpState == 'running' :
if event.key == locals.K_SPACE: #按下空格键
jumpState = 'up' #状态 起跳
if jumpState == 'up': #起跳
if t > 0 :
y -= t
t -= 2
else:
jumpState = 'down'
if jumpState == 'down': # 落下
if t <= 30:
y += t
t += 2
else:
jumpState = 'running'
t = 30
wukong = hero[index]
if jumpState == 'running':
index += 1
if index > 4:
index = 0
# 将背景图画上去
screen.blit(background, (0, 0))
screen.blit(road, (0, 500))
screen.blit(wukong, (150, y))
if rect.x <= 0 -rect.width: #生成障碍物
obstacle = random.choice([stone,cacti,bush])
rect = obstacle.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
rect.x -= 8 #障碍物移动
screen.blit(obstacle,(rect.x,rect.y)) #把障碍物渲染到窗口
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame
import pygame
from pygame import locals
import random
pygame.init() # 初始化
class Block(pygame.sprite.Sprite): #障碍物
def __init__(self, image1, image2, image3):
super().__init__()
# 生成一个障碍物
self.image = random.choice([image1, image2, image3])
self.rect = self.image.get_rect()
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite): #悟空
def __init__(self, image1):
super().__init__()
# 生成一个障碍物
self.image = image1
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption('悟空酷跑') # 窗口设置
# 载入图片
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')]
index = 0
y = 400
jumpState = 'running'
t = 30
road_x = 0
bg_x = 0
# 创建障碍物
obstacle = Block(stone, cacti, bush)
block_list = pygame.sprite.Group()
time = 0
gameState = True
score = 0
font = pygame.font.Font('STKAITI.TTF',20) #加载字体
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE and jumpState == 'running':
jumpState = 'up'
wukong = Player(hero[index])
if jumpState == 'running':
index += 1
if index > 4:
index = 0
if gameState == True:
if jumpState == 'up':
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down':
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -1000:
bg_x = 0
screen.blit(background, (bg_x, 0))
road_x -= 12
if road_x <= -1000:
road_x = 0
screen.blit(road, (road_x, 500)) # 路
screen.blit(wukong.image, (150, y))
time += 1
if time >= 60:
time = 0
num = random.randint(0,100)
if num > 40:
obstacle = Block(stone, cacti, bush)
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x -= 12
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0- sprite.rect.width: #障碍物移除窗口
score += 1 #分数加1
sprite.kill() #删除障碍物
if pygame.sprite.collide_rect(wukong,sprite): #检测两个精灵是否发生碰撞
gameover = pygame.image.load('gameover.png') #加载图片
screen.blit(gameover,(400,200)) #绘制到窗口上
gameState = False
text = font.render('分数'+str(score),True,(0,0,0))
screen.blit(text,(900,20))
# 刷新画面
pygame.display.update()
FPS.tick(60)
import pygame
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
self.image = random.choice([image1,image2,image3])
self.rect = self.image.get_rect()
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite): #悟空
def __init__(self,image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('悟空酷跑')
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')]
index = 0
y = 400
jumpState = 'running'
t = 30
road_x = 0
bg_x = 0
block_list = pygame.sprite.Group() #创建一个精灵组
time = 0
gameover=True
score = 0
font = pygame.font.Font('STKAITI.TTF',20) #字体
speed = 10
with open('score.txt','r',encoding='utf-8')as file:
content = file.read()
recode = json.loads(content)
one = recode['第1名']
two = recode['第2名']
three = recode['第3名']
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN: #按下键盘
if jumpState == 'running' :
if event.key == locals.K_SPACE: #按下空格键
jumpState = 'up' #状态 起跳
wukong = Player(hero[index])
if jumpState == 'running':
index += 1
if index > 4:
index = 0
speed = 10 + score // 3
if gameover == True:
if jumpState == 'up': #起跳
if t > 0 :
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down': # 落下
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -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:
time = 0
num = random.randint(0,80)
if num > 20:
obstacle = Block(stone,cacti,bush)
block_list.add(obstacle)
#遍历精灵组实现多个障碍物出现
for sprite in block_list:
sprite.rect.x -= speed #障碍物移动
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y)) #把障碍物渲染到窗口
if sprite.rect.x < 0-sprite.rect.width:
score += 1
sprite.kill()
#判断两个精灵是否发生碰撞
if pygame.sprite.collide_rect(wukong,sprite):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gameover = False
text = font.render('分数:'+str(score),True,(0,0,0))
screen.blit(text,(900,10))
text1 = font.render('第1名:'+str(one),True,(0,0,0))
screen.blit(text1,(900,40))
text2 = font.render('第2名:'+str(two),True,(0,0,0))
screen.blit(text2,(900,70))
text3 = font.render('第3名:'+str(three),True,(0,0,0))
screen.blit(text3,(900,100))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
#加载图片
self.image=random.choice([image1,image2,image3])
#通过图片设置矩形的宽高
self.rect = self.image.get_rect()
#设置x,y坐标
self.rect.x = 1000
self.rect.y = 500-self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
#加载图片
self.image=image
#通过图片设置矩形的宽高
self.rect = self.image.get_rect()
#设置x,y坐标
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('悟空酷跑') #设置窗口标题
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')]
index = 0
y = 400
jumpState = 'running'
t = 30
block_list = pygame.sprite.Group() #创建精灵组
road_x = 0
bg_x = 0
time = 0
state = True
score = 0
font = pygame.font.Font('STKAITI.TTF',20) #字体
speed = 10
with open('score.txt','r',encoding='utf-8')as file:
content = file.read()
record = json.loads(content)
one = record['第1名']
two = record['第2名']
three = record['第3名']
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN: #按下键盘
if event.key == locals.K_SPACE and jumpState == 'running': #按下空格键
jumpState = 'up'
speed = 10 + score // 3
wukong = Player(hero[index])
if jumpState == 'running':
index += 1
if index > 4:
index = 0
if state == True:
if jumpState == 'up': #起跳
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down': #落下
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -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:
time = 0
num = random.randint(0,50)
if num > 20:
obstacle = Block(stone,cacti,bush)
block_list.add(obstacle) #把精灵添加到精灵组中
for sprite in block_list: #遍历精灵组
sprite.rect.x -= speed
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x < 0-sprite.rect.width:
sprite.kill() #删除精灵
if pygame.sprite.collide_rect(wukong,sprite): #判断两个精灵是否发生碰撞
gameover = pygame.image.load('gameover.png') #加载图片
screen.blit(gameover,(400,200)) #渲染到窗口上
state = False
if score > three:
if score > one:
record['第1名'] = score
record['第2名'] = one
record['第3名'] = two
elif score > two:
record['第2名'] = score
record['第3名'] = two
else:
record['第3名'] = score
record = json.dumps(record,ensure_ascii=False) #把字典转成JSON字符串
#写入到文本中
with open('score.txt','w',encoding='utf-8')as file:
file.write(record)
#悟空跳过障碍物进行加分
if sprite.rect.x + sprite.rect.width < wukong.rect.x:
score += sprite.score
sprite.score = 0
#分数渲染
text = font.render('分数:'+str(score),True,(255,255,255))
screen.blit(text,(900,20))
#排名渲染
text1 = font.render('第1名:'+str(one),True,(255,255,255))
screen.blit(text1,(900,50))
text2 = font.render('第2名:'+str(two),True,(255,255,255))
screen.blit(text2,(900,80))
text3 = font.render('第3名:'+str(three),True,(255,255,255))
screen.blit(text3,(900,110))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self, image1, image2, image3):
super().__init__()
# 加载图片
self.image = random.choice([image1, image2, image3])
# 通过图片设置矩形的宽高
self.rect = self.image.get_rect()
# 设置x,y坐标
self.rect.x = 1000
self.rect.y = 500-self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
# 加载图片
self.image = image
# 通过图片设置矩形的宽高
self.rect = self.image.get_rect()
# 设置x,y坐标
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('悟空酷跑') # 设置窗口标题
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')]
index = 0
y = 400
jumpState = 'running'
t = 30
block_list = pygame.sprite.Group() # 精灵组
road_x = 0
bg_x = 0
time = 0
gamestate = True
score = 0 #分数
font = pygame.font.Font('STKAITI.TTF',20) #加载字体
speed = 10
music = pygame.mixer.Sound('score.wav')
old_score = 0
with open('score.txt','r',encoding='utf-8')as file:
center = file.read()
# print(center)
recode = json.loads(center)
one = recode['第1名']
two = recode['第2名']
three = recode['第3名']
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN: # 按下键盘
if event.key == locals.K_SPACE and jumpState == 'running': # 按下空格键
jumpState = 'up'
wukong = Player(hero[index])
if jumpState == 'running':
index += 1
if index > 4:
index = 0
speed = 10 + score // 3 #分数每3分速度增加1
if gamestate == True:
if jumpState == 'up': # 起跳
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down': # 落下
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -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:
time = 0
num = random.randint(0,60)
if num > 20:
obstacle = Block(stone, cacti, bush)
block_list.add(obstacle)
#遍历精灵组渲染到窗口上
for sprite in block_list:
sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
#判断障碍物是否移除窗口 删除
if sprite.rect.x < 0- sprite.rect.width:
sprite.kill()
#判断悟空是否碰到障碍物
if pygame.sprite.collide_rect(wukong,sprite):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate = False
if score>three:
if score>one:
record["第1名"] = score
record["第2名"] = one
record["第3名"] = two
elif score > two:
record["第2名"] = score
record["第3名"] = two
else:
record["第3名"] = score
record = json.dumps(record, ensure_ascii=False)
with open("score.txt","w", encoding='utf-8') as f:
f.write(record)
#跳过障碍物分数增加1
if sprite.rect.x + sprite.rect.width < wukong.rect.x :
score += sprite.score
sprite.score = 0
# if score > old_score:
# music.play()
# old_score = score
#把分数绘制到窗口上
text = font.render('分数:'+str(score),True,(255,255,255))
screen.blit(text,(900,20))
text = font.render('第1名:'+str(one),True,(255,255,255))
screen.blit(text,(900,50))
text = font.render('第2名:'+str(two),True,(255,255,255))
screen.blit(text,(900,80))
text = font.render('第3名:'+str(three),True,(255,255,255))
screen.blit(text,(900,110))
# 刷新画面
pygame.display.update()
FPS.tick(60)
import pygame
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类
def __init__(self,image1,image2,image3):
super().__init__()
# 加载障碍物的图片
self.image = random.choice([image1, image2, image3])
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite): # 障碍物精灵类
def __init__(self,image):
super().__init__()
# 加载障碍物的图片
self.image = image
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption('悟空酷跑') #设置标题
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
apple = 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')]
index = 0
y = 400
jumpState = 'running'
t = 30
block_list = pygame.sprite.Group() #创建一个精灵组
road_x = 0
bg_x = 0
time = 0
gamestate = True
score = 0 #分数
font = pygame.font.Font('STKAITI.TTF',20) #加载字体
speed = 10 #速度
old_score = 0 #设置旧分数
music = pygame.mixer.Sound('score.wav') #加载音频文件
with open('score.txt','r',encoding='utf-8')as file:
content = file.read()
# print(content)
record = json.loads(content)
one = record['第1名']
two = record['第2名']
three = record['第3名']
# print(one,two,three)
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type ==locals.KEYDOWN:
if jumpState == 'running':
if event.key == locals.K_SPACE:
jumpState = 'up'
speed = 10 + score // 3 #分数增加3 速度增加1
#悟空跑步
wukong = Player(hero[index])
if jumpState == 'running':
index += 1
if index > 4:
index = 0
if gamestate == True:
if jumpState == 'up': #起跳
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down': #起跳
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x < -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, wukong.rect.y))
time += 1
if time >= 60:
time = 0
num = random.randint(0,60)
if num >20:
obstacle = Block(apple,stone,cacti)
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x < 0-sprite.rect.width:
sprite.kill()
if pygame.sprite.collide_rect(wukong,sprite):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate = False
if score > three:
if score > one:
record['第1名'] = score
record['第2名'] = one
record['第3名'] = two
elif score > two:
record['第2名'] = score
record['第3名'] = two
else:
record['第3名'] = score
record = json.dumps(record,ensure_ascii=False)
with open('score.txt','w',encoding='utf-8')as file:
file.write(record)
else:
if sprite.rect.x + sprite.rect.width < wukong.rect.x: #判断障碍物是否在悟空的后面
score += sprite.score
sprite.score = 0
#分数加1 播放音效
if score > old_score:
music.play()
old_score = score
text = font.render('分数:'+str(score),True,(255,255,255))
screen.blit(text,(900,20))
text1 = font.render('第1名:'+str(one),True,(255,255,255))
screen.blit(text1,(900,50))
text2 = font.render('第2名:'+str(two),True,(255,255,255))
screen.blit(text2,(900,80))
text3 = font.render('第3名:'+str(three),True,(255,255,255))
screen.blit(text3,(900,110))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self, image1, image2, image3):
super().__init__()
self.image = random.choice([image1, image2, image3])
self.rect = self.image.get_rect()
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite):
def __init__(self, image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
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')]
block_list = pygame.sprite.Group()
index = 0
y = 400
jumpState = 'running'
t = 30
road_x = 0
bg_x = 0
time = 0
gamestate = True
score = 0
font = pygame.font.Font('STKAITI.TTF', 20) # 字体
speed = 10
with open('score.txt', 'r', encoding='utf-8')as file:
content = file.read()
record = json.loads(content)
one = record['第1名']
two = record['第2名']
three = record['第3名']
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
# 按下空格键起跳
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE and jumpState == 'running':
jumpState = 'up'
speed = 10 + score // 3
wukong = Player(hero[index])
if jumpState == 'running':
index += 1
if index >= 5:
index = 0
if gamestate == True:
if jumpState == 'up': # 起跳
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down': # 落下
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -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:
time = 0
num = random.randint(0, 50)
if num > 20:
obstacle = Block(stone, cacti, bush)
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, obstacle.rect.y))
if sprite.rect.x <= 0 - sprite.rect.width:
score += 1
sprite.kill()
if pygame.sprite.collide_rect(wukong, sprite):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover, (400, 200))
gamestate = False
if score > three:
if score > one:
record['第1名'] = score
record['第2名'] = one
record['第3名'] = two
elif score > two:
record['第2名'] = score
record['第3名'] = two
else:
record['第3名'] = score
record = json.dumps(record,ensure_ascii=False)
with open('score.txt','w',encoding='utf-8')as file:
file.write(record)
text = font.render('分数:'+str(score), True, (0, 0, 0))
screen.blit(text, (900, 20))
text1 = font.render('第1名:'+str(one), True, (0, 0, 0))
screen.blit(text1, (900, 50))
text2 = font.render('第2名:'+str(two), True, (0, 0, 0))
screen.blit(text2, (900, 80))
text3 = font.render('第3名:'+str(three), True, (0, 0, 0))
screen.blit(text3, (900, 110))
# 刷新画面
pygame.display.update()
FPS.tick(60)
import pygame
import pygame
from pygame import locals
import random
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self, image1, image2, image3):
super().__init__()
self.image = random.choice([image1, image2, image3])
self.rect = self.image.get_rect()
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite): #悟空
def __init__(self, image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('悟空酷跑') # 设置窗口的标题
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')]
index = 0
# 生成障碍物
obstacle = Block(stone, cacti, bush)
jumpState = 'running'
y = 400
t = 30
road_x = 0
bg_x = 0
block_list = pygame.sprite.Group() # 创建一个精灵组
time = 0
gamestate = True
score = 0
font = pygame.font.Font('STKAITI.TTF',20) #字体
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE and jumpState == 'running':
jumpState = 'up'
wukong = Player(hero[index])
if jumpState == 'running':
index += 1
if index > 4:
index = 0
if gamestate == True:
if jumpState == 'up': #起跳
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down':
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -1000:
bg_x = 0
screen.blit(background, (bg_x, 0))
road_x -= 12
if road_x <= -1000:
road_x = 0
screen.blit(road, (road_x, 500)) # 路
screen.blit(wukong.image, (150, y))
time += 1
if time >= 60:
time = 0
num = random.randint(0,50)
if num > 20:
obstacle = Block(stone, cacti, bush)
block_list.add(obstacle)
# 遍历精灵组,取出每个精灵向左移动把障碍物渲染到窗口上
for sprite in block_list:
sprite.rect.x -= 12
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
#如果障碍物移除屏幕就删除
if sprite.rect.x <= 0 - sprite.rect.width:
score += 1
sprite.kill()
#检测两个精灵是否发生碰撞
if pygame.sprite.collide_rect(wukong,sprite):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate = False
text = font.render('分数:'+str(score),True,(255,255,255))
screen.blit(text,(900,20))
# 刷新画面
pygame.display.update()
FPS.tick(60)
import pygame
import pygame
from pygame import locals
import random
pygame.init()
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
self.image=random.choice([image1,image2,image3])
self.rect=self.image.get_rect()
self.rect.x=1000
self.rect.y=500-self.rect.height
class Player(pygame.sprite.Sprite):
def __init__(self,image1):
super().__init__()
self.image=image1
self.rect=self.image.get_rect()
self.rect.x=150
self.rect.y=400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption('悟空酷跑') #窗口设置
# 载入图片
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')]
index = 0
y = 400
jumpState = 'running'
t = 30
road_x=0
bg_x=0
obstacle=Block(stone,cacti,bush)
Block_list=pygame.sprite.Group()
time=0
gamestate=True
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE and jumpState=='running' :
jumpState = 'up'
#if gamestate==True:
if jumpState == 'up':
if t > 0:
y -= t
t -= 2
else:
jumpState = 'down'
if jumpState == 'down':
if t <= 30:
y += t
t += 2
else:
jumpState='running'
t = 30
wukong =Player(hero[index])
if jumpState == 'running':
index += 1
if index > 4:
index = 0
# 将背景图画上去
screen.blit(background, (0, 0))
screen.blit(road,(0, 500))
screen.blit(wukong.image, (150, y))
time+=1
if time>=60:
time=0
num=random.randint(0,50)
if num>40:
obstacle=Block(stone,cacti,bush)
Block_list.add(obstacle)
for sprite in Block_list:
sprite.rect.x-=8
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x<=0-sprite.rect.width:
sprite.kill()
if pygame.sprite.collide_rect(wukong,sprite):
gameover=pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate=False
#print(len(Block_list))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
self.image = random.choice([image1,image2,image3])
self.rect = self.image.get_rect()
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('悟空酷跑') #设置窗口的标题
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')]
index = 0
#生成障碍物
obstacle = Block(stone,cacti,bush)
jumpState = 'running'
y = 400
t = 30
road_x = 0
bg_x = 0
block_list = pygame.sprite.Group()
time = 0
gamestate = True
score = 0
font = pygame.font.Font('STKAITI.TTF',20)
with open('score.txt','r',encoding='utf-8')as file:
ruselt = file.read()
recode = json.loads(ruselt)
one = recode['第1名']
two = recode['第2名']
three = recode['第3名']
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE and jumpState == 'running':
jumpState = 'up'
if gamestate == True:
wukong =Player(hero[index])
if jumpState == 'running':
index += 1
if index > 4:
index = 0
if jumpState == 'up':
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = 'down'
if jumpState == 'down':
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = 'running'
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -1000:
bg_x = 0
screen.blit(background, (bg_x, 0))
road_x -= 10
if road_x <= -1000:
road_x = 0
screen.blit(road, (road_x, 500)) #路
screen.blit(wukong.image, (wukong.rect.x,y))
time += 1
if time >= 60:
time=0
num = random.randint(0,50)
print(num)
if num > 20:
obstacle = Block(stone,cacti,bush)
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x -= 8
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x <= 0 - sprite.rect.width:
score += 1
sprite.kill()
if pygame.sprite.collide_rect(wukong,sprite):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate = False
if score > three:
if score > one:
recode['第1名'] = score
recode['第2名'] = one
recode['第3名'] = two
elif score > two:
recode['第2名'] = score
recode['第3名'] = two
else:
recode['第3名'] = score
recode = json.dumps(recode,ensure_ascii=False)
with open('score.txt','w',encoding='utf-8')as file:
file.write(recode)
text = font.render('分数'+str(score),True,(255,255,255))
screen.blit(text,(900,20))
text1 = font.render('第1名'+str(one),True,(255,255,255))
screen.blit(text1,(900,50))
text2 = font.render('第2名'+str(two),True,(255,255,255))
screen.blit(text2,(900,80))
text3 = font.render('第3名'+str(three),True,(255,255,255))
screen.blit(text3,(900,110))
# 刷新画面
pygame.display.update()
FPS.tick(60)
import pygame
import pygame
from pygame import locals
from random import *
import time
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
# 加载障碍物的图片
self.image = choice([image1, image2, image3])
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite): #悟空
def __init__(self,image1):
super().__init__()
# 加载障碍物的图片
self.image = image1
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑")
# 载入图片
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')]
my_font = pygame.font.Font('STKAITI.TTF',25)
music = pygame.mixer.Sound('score.wav')
index = 0
y = 400
jumpState = "runing"
t = 30
bg_x = 0
road_x = 0
# 创建障碍物
obstacle = Block(bush,cacti,stone)
#创建一个精灵组
block_list = pygame.sprite.Group()
time1 = 0
gamestate = True
score = 0
speed = 10
old_score = 0
with open('score.txt','r',encoding='utf-8')as file:
content = file.read()
record= json.loads(content)
one = record["第1名"]
two = record["第2名"]
three = record["第3名"]
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 = 10 + score // 3
if gamestate == True:
# 悟空造型
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t =30
# 将背景图画上去
bg_x -= 1
if bg_x<=-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)) # 悟空
# ++++++++++++++++++++++
time1 += 1
if time1 >= 60:
time1 = 0
num = randint(10,50)
if num >= 20:
obstacle = Block(bush,cacti,stone)
block_list.add(obstacle)
#遍历精灵组
for sprite in block_list:
sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0 - sprite.rect.width:
sprite.kill() #删除精灵
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200))
gamestate = False
# pygame.display.update()
# time.sleep(5)
# exit()
if score > three :
if score > one :
record['第1名'] = score
record['第2名'] = one
record['第3名'] = two
elif score > two :
record['第2名'] = score
record['第3名'] = two
else:
record['第3名'] = score
record = json.dumps(record,ensure_ascii=False)
# print(record)
with open('score.txt','w',encoding='utf-8')as file:
file.write(record)
else :
if sprite.rect.x + sprite.rect.width < wukong.rect.x:
score += sprite.score
sprite.score = 0
# print(score)
if score > old_score:
music.play()
old_score = score
text = my_font.render('分数:'+str(score),True,(255,255,255))
screen.blit(text,(880,10))
text = my_font.render('第1名:'+str(one),True,(255,255,255))
screen.blit(text,(880,45))
text = my_font.render('第2名:'+str(two),True,(255,255,255))
screen.blit(text,(880,75))
text = my_font.render('第3名:'+str(three),True,(255,255,255))
screen.blit(text,(880,105))
# print(len(block_list))
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
import pygame
import pygame
from pygame import locals
import random
import json
pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 障碍物精灵类
def __init__(self, image1, image2, image3):
super().__init__()
# 加载障碍物的图片
self.image = random.choice(
[
image1, image2, image3])
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite): # 悟空
def __init__(self, image):
super().__init__()
# 加载悟空精灵图像
self.image = image
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑")
# 载入图片
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')]
index = 0
y = 400
jumpState = "runing"
t = 30
road_x = 0
bg_x = 0
time = 0
gamestate = True
block_list = pygame.sprite.Group() # 创建精灵组
score = 0
font = pygame.font.Font('STKAITI.TTF', 20)
with open('record.txt', 'r', encoding='utf-8')as file:
content = file.read()
record = json.loads(content)
one = record['第1名']
two = record['第2名']
three = record['第3名']
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 = 15+score//3
# 悟空造型
wukong = Player(hero
[
index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
if gamestate == True:
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t = 30
# 将背景图画上去
bg_x -= 1
if bg_x <= -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: # 创建障碍物精灵
time = 0
num = random.randint(0, 50)
if num > 20:
obstacle = Block(bush, cacti, stone)
block_list.add(obstacle)
for sprite in block_list: # 遍历、展示障碍物精灵
sprite.rect.x -= speed
screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width:
score += 1
sprite.kill()
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
gameover = pygame.image.load('gameover.png') # 游戏结束
screen.blit(gameover, (400, 200))
gamestate = False
if score > three:
if score > one:
record["第1名"] = score
record["第2名"] = two
record["第3名"] = three
elif score > two:
record["第2名"] = score
record["第3名"] = three
else:
record["第3名"] = score
record = json.dumps(record, ensure_ascii=False)
with open('record.txt', 'w', encoding='utf-8')as file:
file.write(record)
text = font.render('分数:'+str(score), True, (0, 0, 250))
screen.blit(text, (880, 20))
text1 = font.render('第1名:'+str(one), True, (0, 10, 250))
screen.blit(text1, (880, 50))
text2 = font.render('第2名:'+str(two), True, (0, 20, 250))
screen.blit(text2, (880, 80))
text3 = font.render('第3名:'+str(three), True, (0, 30, 250))
screen.blit(text3, (880, 110))
# 刷新画面
pygame.display.update()
FPS.tick(60)
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