import random,pygame,sys
import time
from pygame.locals import *
#定义玩家的类
class PLAYER(pygame.sprite.Sprite):
def __init__(self,WIDTH,HEIGHT):
pygame.sprite.Sprite.__init__(self)
#导入玩家图片用于设置行走动画
self.PLAYER_walk=[pygame.image.load(r'PLAYER_stand.png').convert_alpha(),
pygame.image.load(r'PLAYER_walk1.png').convert_alpha(),
pygame.image.load(r'PLAYER_walk2.png').convert_alpha(),
pygame.image.load(r'PLAYER_walk3.png').convert_alpha(),
pygame.image.load(r'PLAYER_walk4.png').convert_alpha(),
pygame.image.load(r'PLAYER_walk5.png').convert_alpha(),
pygame.image.load(r'PLAYER_walk6.png').convert_alpha(),
pygame.image.load(r'PLAYER_walk7.png').convert_alpha(),
pygame.image.load(r'PLAYER_walk8.png').convert_alpha()]
将图片相关信息存入字典,键left和right分别对应导入图片的2~5、6~9张,-1和1代表方向
self.data_walk={'left':[[1,2,3,4],-1],'right':[[5,6,7,8],1]}
self.x,self.y=WIDTH/2,HEIGHT*3/4
self.speed=1
self.frame=-1
self.direction='left'
self.life=100
self.move()
def move(self,direction='left'):
#默认方向向左,当键盘按下的方向与人物当前方向不同时,调整人物方向为键盘按下方向,而后增加frame值来转换人物运动状态图片
if self.direction!=direction:
self.direction=direction
else:
self.frame+=1
#使人物移动
self.x+=self.speed*self.data_walk[self.direction][1]
#调整人物移动时的状态造成行走的效果
self.image=self.LYW_walk[self.data_walk[self.direction][0][self.frame%4]]
self.rect=self.image.get_rect(topleft=(self.x,self.y))
#避免走出界面
self.rect.left=0 if self.rect.left<0 else self.rect.x
self.rect.right=WIDTH if self.rect.right>WIDTH else self.rect.x
def draw(self,DISPLAYSURF):
DISPLAYSURF.blit(self.image,self.rect)
#定义掉落物的类
class Drop(pygame.sprite.Sprite):
def __init__(self,WIDTH,HEIGHT):
pygame.sprite.Sprite.__init__(self)
self.type=[pygame.image.load(r'fish.png').convert_alpha(),
pygame.image.load(r'fox.png').convert_alpha(),
pygame.image.load(r'horse.png').convert_alpha(),
pygame.image.load(r'squirrel.png').convert_alpha(),
pygame.image.load(r'rabbit.png').convert_alpha(),
pygame.image.load(r'bear.png').convert_alpha()]
self.kind=random.randint(0,5)
#随机生成以上6种掉落物
self.image=self.type[self.kind]
#如果掉落物是第一种,即self.kind=0,就算基础分为1,其他的掉落物基础分都为-2
self.basevalue=1 if self.kind==0 else -2
#设置奖励分为0
self.awardvalue=0
#先默认每个掉落物的分值为基础分
self.value=self.basevalue
self.speed=3
self.rect=self.image.get_rect()
self.x=random.randint(0,WIDTH-self.rect.width)#掉落物的横坐标随意位置,但是不出界
self.y=0
self.rect.left,self.rect.top=self.x,self.y
#设置字典存储掉落物的次序和种类,即键为第i个掉落,值为第i个掉落物的类型,先默认第0个掉落物是fish
self.drops={0:'fish'}
def move(self):
self.y+=self.speed
self.rect.top=self.y
#设置掉落物的分值
def value_set(self,player):
#drop_get是代表接到的掉落物的个数,先默认为0
drop_get=0
if pygame.sprite.collide_rect(self,player):#如果掉落物和玩家碰到
drop_get+=1#接到掉落物的个数加1
self.drops.update({drop_get:self.kind})#把接到的这个掉落物的次数和类型存到字典里
if drop_get==1:#当第一次接到掉落物时,设奖励分为基础分值,即fish为1分,其他为-2分
self.awardvalue=self.basevalue
else:#如果不是第一次接到,判断这一次接到的掉落物和上一次的是不是一样
if self.drops[drop_get-1]!=self.drops[drop_get]:#如果不一样的话,就重置drop_get为0,下一轮重新开始判断
drop_get=0
else:#如果一样的话,奖励分就这样计算,然后最后每次接到的掉落物的分值就设置为此
self.awardvalue=self.awardvalue*drop_get
self.value=self.awardvalue
#如果连续9次接到同一种掉落物,生命值发生改变,实际上为如果接到fish,生命值就加10%,如果接到其他的,生命值就减20%
if drop_get%9==0:
player.life+=self.awardvalue*10
def draw(self,DISPLAYSURF):
DISPLAYSURF.blit(self.image,self.rect)
FPS=30
WIDTH=800
HEIGHT=600
WHITE=(255,255,255)
RED=(255,0,0)
BLICK=(0,0,0)
#设置掉落物的初始个数为0个
drop_count=0
def main():
global drop_count
pygame.init()
FPSCLOCK=pygame.time.Clock()
DISPLAYSURF=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('game1')
image_background1=pygame.image.load(r'image_background1.png')
rect_background1=image_background1.get_rect(topleft=(0,0))
rect_background1.width,rect_background1.height=WIDTH,HEIGHT
pygame.mixer.music.load('music_background.mid')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play(-1)
music_get=pygame.mixer.Sound('music_get.wav')
music_get.set_volume(0.3)
music_antiget=pygame.mixer.Sound('music_antiget.wav')
music_antiget.set_volume(0.3)
player=LYW(WIDTH,HEIGHT)
dropGroup=pygame.sprite.Group()
score=0
pygame.key.set_repeat(10)
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==pygame.K_LEFT or event.type==pygame.K_a:
direction='left'
player.move(direction)
elif event.key==pygame.K_RIGHT or event.type==pygame.K_d:
direction='right'
player.move(direction)
DISPLAYSURF.fill(WHITE)
DISPLAYSURF.blit(image_background1,rect_background1)
player.draw(DISPLAYSURF)
while drop_count>=0:#当掉落物个数不为负数时,每次掉落物个数都加1
drop_count+=1
drop=Drop(WIDTH,HEIGHT)
dropGroup.add(drop)
drop.value_set(player)
for drop in dropGroup:
drop.move()
if pygame.sprite.collide_rect(drop,player):
score+=drop.value
if drop.kind==0:
music_get.play(1)
else:
music_antiget.play(1)
dropGroup.remove(drop)
if drop.rect.top>HEIGHT:
dropGroup.remove(drop)
music_antiget.play(1)
continue
drop.draw(DISPLAYSURF)
if player.life<0 or score>304:
DISPLAYSURF.fill(WHITE)#游戏结束页面还没写,就用空白代替了
pygame.display.flip()
pygame.display.update()
FPSCLOCK.tick(FPS)
if __name__ == '__main__':
main()