介绍一款好玩的游戏的开发。该游戏名为高山滑雪,改编自《Computer Programming for Kids and other Beginners》的第25章。
游戏效果
游戏的基本界面如下:
滑雪者由高山上滑下,玩家通过键盘上的左右方向键控制滑雪者向左转或向右转。
动画效果如下:
python 代码如下:
#用于定义常量的类
class const:
class ConstError(TypeError):
pass
def _setattr__(self, name, value):
if self.__dict__.has_key(name):
raise self.ConstError("Can't rebind const (%s)" %name)
self.__dict__[name] = value
const.MaxSpeed = 10 #设置最大的下滑速度
import pygame, sys, random
# 滑雪者在不同的方向上应该显示的图像
skier_images = ["skier_down.png", "skier_right1.png", "skier_right2.png",
"skier_left2.png", "skier_left1.png"]
# 滑雪者角色类
class SkierClass(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("skier_down.png")
self.rect = self.image.get_rect()
self.rect.center = [320, 100]
self.angle = 0
def turn(self, direction):
# 当滑雪者转向时,加载对应的图片并且修改速度值
self.angle = self.angle + direction
if self.angle < -2: self.angle = -2
if self.angle > 2: self.angle = 2
center = self.rect.center
self.image = pygame.image.load(skier_images[self.angle])
self.rect = self.image.get_rect()
self.rect.center = center
speed = [self.angle, const.MaxSpeed - abs(self.angle) * 2]
return speed
def move(self, speed):
# move the skier right and left
self.rect.centerx = self.rect.centerx + speed[0]*2
if self.rect.centerx < 20: self.rect.centerx = 20
if self.rect.centerx > 620: self.rect.centerx = 620
# 表示障碍物的类定义,包括树和旗子
class ObstacleClass(pygame.sprite.Sprite):
def __init__(self, image_file, location, type):
pygame.sprite.Sprite.__init__(self)
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.center = location
self.type = type
self.passed = False
def update(self):
global speed
self.rect.centery -= speed[1]
if self.rect.centery < -32:
self.kill()
# create one "screen" of obstacles: 640 x 640
# use "blocks" of 64 x 64 pixels, so objects aren't too close together
def create_map():
global obstacles
locations = []
for i in range(10): # 10 obstacles per screen
row = random.randint(0, 9)
col = random.randint(0, 9)
location = [col * 64 + 32, row * 64 + 32 + 640] #center x, y for obstacle
if not (location in locations): # prevent 2 obstacles in the same place
locations.append(location)
type = random.choice(["tree", "flag"])
if type == "tree": img = "skier_tree.png"
elif type == "flag": img = "skier_flag.png"
obstacle = ObstacleClass(img, location, type)
obstacles.add(obstacle)
# 重绘显示区域,形成动画效果
def animate():
screen.fill([255, 255, 255])
obstacles.draw(screen)
screen.blit(skier.image, skier.rect)
screen.blit(score_text, [10, 10])
pygame.display.flip()
# 初始化各种对象
pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
speed = [0, const.MaxSpeed] # 速度值,包含方向与向下的速度
obstacles = pygame.sprite.Group() # group of obstacle objects
skier = SkierClass()
map_position = 0
points = 0
create_map() # create one screen full of obstacles
font = pygame.font.Font(None, 50)
# 事件处理循环
running = True
while running:
clock.tick(30) #设定每秒帧数
for event in pygame.event.get():
if event.type == pygame.QUIT: running = False
if event.type == pygame.KEYDOWN: # 如果按下了键盘上的键
if event.key == pygame.K_LEFT: # 如果按下了向左的方向键
speed = skier.turn(-1)
elif event.key == pygame.K_RIGHT: #如果按下了向右的方向键
speed = skier.turn(1)
skier.move(speed) # 滑雪者向左或向右移动
map_position += speed[1] # 修改地图的位置,使得障碍物向上移动,造成了滑雪者向下滑行的效果
# 如果地图的位置到达窗口底部,则生成一屏障碍物
if map_position >= 640:
create_map()
map_position = 0
# 检查滑雪者是否碰到了树或旗子
hit = pygame.sprite.spritecollide(skier, obstacles, False)
if hit:
if hit[0].type == "tree" and not hit[0].passed: #碰到了树,并且这个树还没有被“处理”过
points = points - 100
skier.image = pygame.image.load("skier_crash.png") # 显示滑雪者翻倒的图片
animate()
pygame.time.delay(1000)
skier.image = pygame.image.load("skier_down.png") # 显示滑雪者正常滑行的图片
skier.angle = 0
speed = [0, const.MaxSpeed]
hit[0].passed = True
elif hit[0].type == "flag" and not hit[0].passed: # 碰到了一个旗子
points += 10
hit[0].kill() # 清除点这个被碰到的旗子
obstacles.update()
score_text = font.render("Score: " +str(points), 1, (0, 0, 0)) #更新提示的文本信息
animate()
pygame.quit() #退出pygame
游戏中用到的图片资源如下:
skier_crash.png
skier_down.png
skier_flag.png
skier_left1.png
skier_left2.png
skier_right1.png
skier_right2.png
skier_tree.png
关于Python技术储备
Python越来越火了,离全民学Python的时代不远了,python应用场景那么多,不管是做主业还是副业或者别的都行,技多不压身,我这里有一份全套的 Python 学习资料,希望给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
五、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
这份完整版的Python全套学习资料已经上传优快云,朋友们如果需要可以微信扫描下方优快云官方认证二维码免费领取【保证100%免费
】
