时间:2020-08-13 python教程 查看: 1207
引言
这是一个用pygame写的滑雪者的游戏。
skier从上向下滑,途中会遇到树和旗子,捡起一个旗子得10分,碰到一颗树扣100分,可以用左右箭头控制skier方向。
安装pygame
pip install pygame
用pip或设置界面安装,可自行百度
以下是主界面代码,每一个类都是一个py文件,需要导包
import pygame
import random
from settings import Settings
from skier import Skier
from treeFlag import TreeFlagClass
# pygame.display.set_icon()
# background_image = pygame.image.load('timg.jpg').convert()#背景图
# 画图
# pygame.draw.circle(window,[0,0,0],[50,50],50,0)
my_set = Settings() # 用的是设置类 系统外观设置
skier_images = ['./skier_crash.png', './skier_down.png', './skier_left1.png', './skier_left2.png',
'./skier_right1.png', './skier_right2.png', ] #滑雪者的图
if __name__ == '__main__':
# 初始化
pygame.init()
pygame.mixer.init()
# 游戏窗口初始化
window = pygame.display.set_mode((my_set.screen_width, my_set.screen_height)) # 窗口大小
screen = pygame.display.get_surface()
pygame.display.set_caption(my_set.name) # 窗口名字
clock = pygame.time.Clock() # 帧率显示,先定义一个时间对象
treeFlag_sprites = pygame.sprite.RenderUpdates() # 创建sprite容器 树
# 添加树或旗子创建自定义事件
AddEnemy = pygame.USEREVENT + 1
pygame.time.set_timer(AddEnemy, 20)
# 音乐
# pygame.mixer.muisc.load('bg_music.mp3')
skier = Skier(1) # 创建滑雪小人
# 左上角计算分数
# countObj = pygame.font.SysFont('方正兰亭超细黑简体',30)
countObj = pygame.font.Font(None, 60)
# countObj.set_bold(True) #加粗
print(pygame.font.get_fonts())
textObj = countObj.render('SCORE:0', True, (255, 0, 0))
textRectObj = textObj.get_rect()
# 这个是计算分数
count_num = 0
# 计算碰撞次数,10次场景加一次速度,最多叠加10次
hit_count = 0
# 显示当前速度
speedObj = pygame.font.Font(None, 40)
speed_text = speedObj.render('SPEED:1', True, (255, 0, 0))
speedRectObj = speed_text.get_rect()
speedRectObj.top = 60
#运行游戏
while True:
# 先加载背景图
clock.tick(60)
window.fill(my_set.bg_color) # 填充,参数填写的是rgb值
# window.blit(background_image,(0,0))
ret = pygame.event.get() # 事件
# 退出游戏判断
for obj in ret:
if obj.type == pygame.QUIT:
print('关闭窗口')
exit()
# 获取键盘状态
pressed_keys = pygame.key.get_pressed()
#调用方法更新
skier.update(pressed_keys)
#判断事件,生成场景
for event in pygame.event.get():
if event.type == AddEnemy:
if random.randint(1,10) %2:
treeFlag_sprites.add(TreeFlagClass(1,1))
else:
treeFlag_sprites.add(TreeFlagClass(1,0))
#判断与树或者旗子碰撞
hit = pygame.sprite.spritecollide(skier, treeFlag_sprites, False)
if hit:
if hit[0].type == 'tree':
count_num -= 100
hit_count += 1
hit[0].kill()
skier.image = pygame.image.load(skier_images[0]).convert()
# skier.image = pygame.image.load(skier_images[1]).convert()
else:
count_num += 10
hit_count += 1
hit[0].kill()
#判断累计碰撞次数:
if hit_count <50 and hit_count > 10:
hit_num = hit_count//10
for obj in treeFlag_sprites:
obj.speed = hit_num
skier.speed = hit_num
elif hit_count >= 50:
for obj in treeFlag_sprites:
obj.speed = 5
skier.speed = 5
#场景动画更新
treeFlag_sprites.update()
tree_updates = treeFlag_sprites.draw(window)
pygame.display.update(tree_updates)
#添加画面以及帧率
window.blit(skier.image, skier.rect) # 添加小人画面
textObj = countObj.render('SCORE:%d' % count_num, False, (255, 0, 0)) #显示得分内容
textRectObj = textObj.get_rect()
window.blit(textObj, textRectObj) #这是得分
speed_text = speedObj.render('SPEED:%d'%skier.speed, True, (255, 0, 0)) #显示速度内容
speedRectObj = speed_text.get_rect()
speedRectObj.top = 60
window.blit(speed_text,speedRectObj) #显示速度
pygame.display.update() # 必须要更新显示的内容
以下是滑雪者类代码
# 定义一个雪人类
import pygame
from settings import Settings
my_set = Settings()
skier_images = ['./skier_crash.png', './skier_down.png', './skier_left1.png', './skier_left2.png',
'./skier_right1.png', './skier_right2.png', ]
down_image = pygame.image.load(skier_images[1])
# 雪人初始位置
begin_top = 0
begin_left = my_set.screen_width / 2 - down_image.get_width() / 2
class Skier(pygame.sprite.Sprite):
def __init__(self,speed):
pygame.sprite.Sprite.__init__(self)
'''
:param image:指的是图片路径
:param rect:位置
:param down_speed:是往下的速度
'''
self.image = pygame.image.load(skier_images[1])
self.rect = self.image.get_rect()
self.rect.top = begin_top # 雪人初始位置
self.rect.left = begin_left
# 移动速度
self.speed = speed
#场景生成时间 也就是速度
self.time_set = 40
#根据按键事件来移动
def update(self, pressed_keys):
# 按键事件
if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]:
if self.rect.left > 0:
self.image = pygame.image.load(skier_images[2]).convert()
self.rect.left -= self.speed
elif pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]:
if self.rect.left < my_set.screen_width - self.image.get_width():
self.image = pygame.image.load(skier_images[4]).convert()
self.rect.left += self.speed
if pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w]:
if self.rect.top > 0 :
self.image = pygame.image.load(skier_images[1]).convert()
self.rect.top -= self.speed
elif pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]:
if self.rect.top < my_set.screen_height - self.image.get_height():
self.image = pygame.image.load(skier_images[1]).convert()
self.rect.top += self.speed
if not (pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a] or pressed_keys[pygame.K_RIGHT] or
pressed_keys[pygame.K_d] or pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w] or
pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]):
self.image = pygame.image.load(skier_images[1]).convert()
树和旗子类
import pygame
from random import randrange
from settings import Settings
my_set = Settings()
# 定义一个场景类
class TreeFlagClass(pygame.sprite.Sprite):
def __init__(self,speed,treeFlag): #传入两个参数,初始速度和bool值树或旗子
pygame.sprite.Sprite.__init__(self) #父类的初始化方法要调用
'''
:param image =图片
:param type 判断是树还是旗子
:param speed 速度
'''
if treeFlag:
self.image = pygame.image.load('./skier_tree.png')
self.type = 'tree'
else:
self.image = pygame.image.load('./skier_flag.png')
self.type = 'flag'
self.rect = self.image.get_rect()
self.rect.top = self.rect.height + my_set.screen_height
self.rect.centerx = randrange(my_set.screen_width - self.rect.width) + self.rect.width / 2
self.speed = speed
def update(self, *args):
self.rect.top -= self.speed
if self.rect.top < -self.rect.height:
self.kill()
设置类
class Settings(object):
def __init__(self):
#这是一个设置类,用来管理游戏的外观
self.screen_width = 600
self.screen_height = 800
self.bg_color = [255,255,255]
self.name = 'skier'
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。