Commit 848a19f5 by BellCodeEditor

auto save

parent e510a54a
Showing with 94 additions and 21 deletions
...@@ -2,6 +2,27 @@ import pygame ...@@ -2,6 +2,27 @@ import pygame
from pygame import locals from pygame import locals
import random # 1.4 import random # 1.4
pygame.init() # 初始化 pygame.init() # 初始化
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Block(pygame.sprite.Sprite): #障碍物精灵类 2.2
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 # 2.2
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Player(pygame.sprite.Sprite): # 悟空 2.4
def __init__(self,image):
super().__init__()
# 加载悟空精灵图像
self.image = image
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400 # 2.4
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
...@@ -12,7 +33,7 @@ background = pygame.image.load('bg.png') # 背景 ...@@ -12,7 +33,7 @@ background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路 road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头 stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌 cacti = pygame.image.load('cacti.png') # 仙人掌
apple = pygame.image.load('bush.png') # 灌木丛 bush = pygame.image.load('bush.png') # 灌木丛
#hero = pygame.image.load('hero1.png') #hero = pygame.image.load('hero1.png')
hero=[pygame.image.load('hero1.png'), hero=[pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'), pygame.image.load('hero2.png'),
...@@ -24,11 +45,17 @@ y = 400 # 1.2 ...@@ -24,11 +45,17 @@ y = 400 # 1.2
t = 30 # 1.3 t = 30 # 1.3
#stone_x = 1000 # 1.4 #stone_x = 1000 # 1.4
jumpState = 'runing' # 1.2 jumpState = 'runing' # 1.2
road_x = 0 # 2.1
background_x = 0 # 2.1
time = 0 # 2.3
gamestate = True # 2.4
#+++++++++++++++++++++++++++++++++++++++++++++ #+++++++++++++++++++++++++++++++++++++++++++++
obstacle = random.choice(([bush,stone,cacti])) # 1.4 # obstacle = random.choice(([bush,stone,cacti])) # 1.4
rect = obstacle.get_rect() # rect = obstacle.get_rect()
rect.x = 1000 # rect.x = 1000
rect.y = 500-rect.height # 1.4 # rect.y = 500-rect.height # 1.4
#obstacle = Block(bush,stone,cacti) # 创建障碍物 2.2
block_list = pygame.sprite.Group() # 创建精灵组 2.3
#+++++++++++++++++++++++++++++++++++++++++++++ #+++++++++++++++++++++++++++++++++++++++++++++
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -50,42 +77,88 @@ while True: ...@@ -50,42 +77,88 @@ while True:
# y+=5 # y+=5
# else: # else:
# jumpState='runing' # 1.2 # jumpState='runing' # 1.2
## 悟空造型
wukong = Player(hero[index]) # 2.4
if jumpState=='runing': # 2.4
index += 1 # 2.4
if index == 5: # 2.4
index = 0 # 2.4
if gamestate==True: # 2.4
#++++++++++++++++++++++++++++++ #++++++++++++++++++++++++++++++
if jumpState=='up': # 1.3 if jumpState=='up': # 1.3
if t>0: if t>0:
y-=t y-=t
wukong.rect.y = y # 2.4
t-=2 t-=2
else: else:
jumpState='down' jumpState='down'
if jumpState=='down': if jumpState=='down':
if t<=30: if t<=30:
y+=t y+=t
wukong.rect.y = y # 2.4
t+=2 t+=2
else: else:
jumpState='runing' jumpState='runing'
t = 30 # 1.3 t = 30 # 1.3
#++++++++++++++++++++++++++++++ #++++++++++++++++++++++++++++++
wukong = hero[index] # 1.1 # 悟空造型
if jumpState=='runing': # 1.3 #wukong = hero[index] # 1.1
index += 1 # 1.1 # wukong = Player(hero[index]) # 2.4
if index == 5: # 1.1 # if jumpState=='runing': # 1.3
index = 0 # 1.1 # index += 1 # 1.1
# if index == 5: # 1.1
# index = 0 # 1.1
#++++++++++++++++++++++++++++++ #++++++++++++++++++++++++++++++
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) #screen.blit(background, (0, 0))
screen.blit(road, (0, 500)) #+++++++++++++++++++++++++++++++++++++++++++++
background_x -= 1 # 2.1
if background_x <= -1000:
background_x=0
screen.blit(background, (background_x, 0))
#screen.blit(road, (0, 500))
road_x -= 8 # 2.1
if road_x <= -1000: # 2.1
road_x = 0 # 2.1
screen.blit(road, (road_x, 500)) #路 2.1
#++++++++++++++++++++++++++++++++++++++++++++++
#screen.blit(hero, (150, 400)) #screen.blit(hero, (150, 400))
screen.blit(wukong, (150, 400)) # 1.1 #screen.blit(wukong, (150, 400)) # 1.1
screen.blit(wukong, (150, y)) # 1.2 #screen.blit(wukong, (150, y)) # 1.2
screen.blit(wukong.image, (150, y)) # 2.4
#++++++++++++++++++++++++++++++++++++++++++++++++ #++++++++++++++++++++++++++++++++++++++++++++++++
#创建障碍物 1.4 #创建障碍物 1.4
if rect.x <= 0-rect.width: #if rect.x <= 0-rect.width:
obstacle=random.choice(([bush,stone,cacti])) # obstacle=random.choice(([bush,stone,cacti]))
rect = obstacle.get_rect() # rect = obstacle.get_rect()
rect.x = 1000 # rect.x = 1000
rect.y = 500-rect.height # rect.y = 500-rect.height
rect.x-=8 # 1.4 # if obstacle.rect.x <= 0-obstacle.rect.width: # 2.2
screen.blit(obstacle,(rect.x,rect.y)) # 1.4 # # 创建障碍物对象
# obstacle = Block(bush,stone,cacti) # 2.2
# obstacle.rect.x-=8 # 2.2
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
time += 1 # 2.3
if time>=60:
time=0
num=random.randint(0,50)
if num > 20:
obstacle = Block(bush,stone,cacti) # 创建障碍物精灵 2.3
block_list.add(obstacle)
for sprite in block_list:
sprite.rect.x -= 8
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y)) # 2.3
if sprite.rect.x <= 0-sprite.rect.width:
sprite.kill() # 2.3
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if pygame.sprite.collide_rect(wukong,sprite): #精灵碰撞检测 2.4
gameover = pygame.image.load('gameover.png') #游戏结束
screen.blit(gameover,(400,200)) # 2.4
gamestate = False
#rect.x-=8 # 1.4
#screen.blit(obstacle,(rect.x,rect.y)) # 1.4
#screen.blit(obstacle.image,(obstacle.rect.x,obstacle.rect.y)) # 2.2
#++++++++++++++++++++++++++++++++++++++++++++++++ #++++++++++++++++++++++++++++++++++++++++++++++++
# 刷新画面 # 刷新画面
# stone_x -= 5 # 1.4 # stone_x -= 5 # 1.4
......
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