程序部分
只需要一张序列图便可以畅玩整个游戏(在结尾),所以非常方便。废话少说,代码全部奉上!!!
import pygame
import sys,random
from pygame.locals import *
#------------------------------------------------------------------------------#
# rgb
black=0,0,0
white=255,255,255
pink=255,0,255
gold=255,204,0
green=0,128,0
blue=0,0,128
gray=230,230,230
blue_s=0,250,255
red=200,0,0
sky=100,255,255
#定义字体
def print_t(screen,font,x,y,text,color=(255,255,255)):
imgtext=font.render(text,True,color)
screen.blit(imgtext,(x,y))
#定义方块类
class Myrect(object):
def __init__(self):
self.pos=[0,0]
self.w=0
self.h=0
self.vel=5
self.point=0
self.barrier=False
self.squr=None
# self.rect=pygame.Rect(self.pos[0],self.pos[1],self.w,self.h)
#刷新位置
def shuaxin(self):
nice_w=[0,10,12,14,16,18,20]
nice_h=[0,20,25,30,35,40,45]
self.w=nice_w[random.randint(1,6)]
self.h=nice_h[random.randint(1,6)]
real=random.randint(-50,50)
self.pos[0]=850+real
self.pos[1]=self.point-self.h
#产生方块类
def yunxing(self,screen):
global score
self.pos[0]-=self.vel
rect=self.pos[0],self.pos[1],self.w,self.h
pygame.draw.rect(screen,black,rect,0)
if self.pos[0]<=0:
self.shuaxin()
score+=1
self.squr=pygame.Rect(self.pos[0],self.pos[1],self.w,self.h)
#精灵类
class MySprite(pygame.sprite.Sprite):
def __init__(self, target):
pygame.sprite.Sprite.__init__(self)#值:图像、主图、矩形为空;图像新旧、高低、首尾、列、时间
self.image = None
self.master_image = None
self.rect = None#矩形刷新代替
self.frame = 0
self.old_frame = 0
self.frame_width = 0
self.frame_height = 0
self.first_frame = 0
self.last_frame = 0
self.columns = 0
self.last_time = 0
self.pos=[0,0]
self.jump=False
self.jump_v=0
self.start=0
self.squr=None
# self.rect=pygame.Rect(self.pos[0],self.pos[1],self.frame_width,self.frame_height)
def load(self, filename, col=1,row=1, width=0, height=0):#告知程序每一帧的大小,其次要告诉有多少列
self.master_image = pygame.image.load(filename).convert_alpha()
rect = self.master_image.get_rect()
if width==0 and height==0:
w=rect.width//col
h=rect.height//row
self.frame_width = w
self.frame_height = h
self.rect = self.pos[0],self.pos[1],w,h
self.columns = col
self.last_frame = col*row - 1
def update(self, current_time, rate=60):#帧+1,首尾交替,记录时间
self.squr=pygame.Rect(self.pos[0],self.pos[1],self.frame_width,self.frame_height)
if current_time > self.last_time + rate:
self.frame += 1
if self.frame > self.last_frame:
self.frame = self.first_frame
self.last_time = current_time
if self.frame != self.old_frame:
frame_x = (self.frame % self.columns) * self.frame_width#找到每一帧的左上角的X和Y位置
frame_y = (self.frame // self.columns) * self.frame_height
rect = ( frame_x, frame_y, self.frame_width, self.frame_height )
self.image = self.master_image.subsurface (rect)# 截取不同片段
self.old_frame = self.frame
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# want_num设置关卡数,作者没设任何限制,建议不要超过5关
want_num=2
game_run=True
lives=2
score=-want_num
max_score=10
# 创建组
player=[]
rect=[]
ground=[]
keygroup=[K_1,K_2,K_3,K_4,K_5]
flag=[0 for i in range(0,want_num)]
#初始化程序
def game_init():
global w,h ,font,font3,font4
pygame.init()
size=w,h=400,700
screen = pygame.display.set_mode(size,0,32)
pygame.display.set_caption("精灵类测试")
font = pygame.font.Font(None, 18)
font3=pygame.font.SysFont("SimHei",24)
font4=pygame.font.SysFont('SimHei',40)
return screen
#主程序
def game(screen):
global ticks
framerate = pygame.time.Clock()
set_player()
while True:
framerate.tick(40)#按照帧速率来运行 对于某些游戏,40或者60更好
ticks = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if game_run:
screen.fill(white)
press_key()
show_palyer()
event_run()
game_font()
else:
game_over()
pygame.display.update()
#========================================================================================================================#
#开始界面,由于导入图片太多你们玩不了,所以拆了
def game_begin():
screen.fill(blue)
print ("it's begin interface")
#结束界面
def game_over():
screen.fill(black)
print_t(screen,font4,50,250,"游戏结束!!")
print_t(screen,font4,50,300,"你的最终得分:"+str(score))
print_t(screen,font3,300,0,"最高纪录:")
print_t(screen,font3,330,20,"<"+str(max_score)+">")
#按键事件
def press_key():
keys= pygame.key.get_pressed()
if keys[K_ESCAPE]:
exit()
for n in range(0,want_num):
k=keygroup[n]
if keys[k]:
if not player[n].jump:
player[n].jump=True
player[n].jump_v=-10
print("按键",n)
#按键响应,包括玩家的动作
def event_run():
# 降落
global flag,lives,game_run,max_score
for n in range(0,want_num):
# 碰撞检测,分组化,不容易跳bug
flag[n]=pygame.Rect.colliderect(player[n].squr,rect[n].squr)
# print("rect",rect[0].squr)
# print("palyer",player[0].squr)
if player[n].jump:
player[n].pos[1]+=player[n].jump_v
player[n].jump_v+=0.5
if player[n].pos[1]>player[n].start:
player[n].jump=False
player[n].pos[1]=player[n].start
player[n].jump_v
# 碰撞检测
for i,fg in enumerate(flag):
if fg==True:
lives-=1
rect[i].shuaxin()
print(flag)
# 游戏结束检测
if lives<=0:
game_run=0
# 最高分检测
if max_score<score:
max_score=score
#界面中文字,当你分数超过5,自动消除提示
def game_font():
if score<5:
text1="<press number 1,2,3 to play>"
print_t(screen,font3,20,350,text1,sky)
text2="score:"+str(score)
print_t(screen,font,350,0,text2,black)
text3="lives:"+str(lives)
print_t(screen,font,350,20,text3,black)
#========================================================================================================================#
#导入玩家组,方块组,地面组
def set_player():
global player_group
player_group=pygame.sprite.Group()
for n in range(0,want_num):
player.append(n)
player[n]= MySprite(screen)
space=h/(want_num+1)
player[n].pos=[20,space*(n+1)-60]
player[n].start=space*(n+1)-60
player_group.add(player[n])
rect.append(n)
rect[n]=Myrect()
rect[n].point=space*(n+1)
ground.append(n)
y=space*(n+1)
ground[n]=(0,y,800,20)
#显示玩家组,方块组,地面组,因为界面刷新过程一直显示,所以与导入组分开
def show_palyer():
for n in range(0,want_num):
player[n].load("player序列图.png",3,2)
pygame.draw.rect(screen,black,ground[n],0)
rect[n].yunxing(screen)
player_group.update(ticks,50)
player_group.draw(screen)
#------------------------------------------------------------------------------#
if __name__=='__main__':
screen=game_init()
game(screen)
序列图
将此图与代码放一个文件夹,就可以运行