首页 » 技术分享 » python学习之开发小游戏---skier(滑雪的人)

python学习之开发小游戏---skier(滑雪的人)

 

滑雪的人(skier)这个小游戏的编写,是在学习python更多功能的时候学习到的,这里参照的是图灵图书里面的内容,这里首先要安装pygame模块

下载地址:Pygame网站(www.pygame.org)

下面是一些程序中所需要的图片地址:

https://www.manning.com/search

程序代码如下:

#!/usr/bin/env python
# coding:utf-8
# __author__ = 'ptallrights'

"""
@version: Python 2.7.11
@author: ptallrights
@license: Apache Licence 
@file: Skier.py.py
@time: 2016/7/24 17:46
"""

import pygame,sys,random

skier_p_w_picpaths = ["skier_down.png","skier_right1.png",
                "skier_right2.png","skier_left1.png",
                "skier_left2.png"]

class SkierClass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.p_w_picpath = pygame.p_w_picpath.load("skier_down.png")
        self.rect = self.p_w_picpath.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.p_w_picpath = pygame.p_w_picpath.load(skier_p_w_picpaths[self.angle])
        self.rect = self.p_w_picpath.get_rect()
        self.rect.center = center
        speed = [self.angle,6 - abs(self.angle) * 2]
        return speed
    def move(self,speed):
        self.rect.centerx = self.rect.centerx + speed[0]
        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,p_w_picpath_file,location,type):
        pygame.sprite.Sprite.__init__(self)
        self.p_w_picpath_file = p_w_picpath_file
        self.p_w_picpath = pygame.p_w_picpath.load(p_w_picpath_file)
        self.rect = self.p_w_picpath.get_rect()
        self.rect.center = location
        self.type = type
        self.passwd = False

    def update(self):
        global speed
        self.rect.centery -= speed[1]
        if self.rect.centery < -32:
            self.kill()

def create_map():
    global obstacles
    locations = []
    for i in range(10):
        row = random.randint(0,9)
        col = random.randint(0,9)
        location = [col * 64 + 20,row * 64 + 20 + 640]
        if not (location in locations):
            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.p_w_picpath,skier.rect)
    pygame.display.flip()

pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
skier = SkierClass()
speed = [0,6]
obstacles = pygame.sprite.Group()
map_position = 0
points = 0
create_map()
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].passwd:
            points = points - 100
            skier.p_w_picpath = pygame.p_w_picpath.load("skier_crash.png")
            animate()
            pygame.time.delay(1000)
            skier.p_w_picpath = pygame.p_w_picpath.load("skier_down.png")
            skier.angle = 0
            speed = [0,6]
            hit[0].passwd = True
        elif hit[0].type == "flag" and not hit[0].passwd:
            points += 10
            hit[0].kill()

    obstacles.update()
    score_txt = font.render("Score: " + str(points),1,(0,0,0))
    animate()

pygame.quit()

程序演示结果如下所示:

wKioL1eUssKxBJgDAACKhlMqAvo692.png

转载自原文链接, 如需删除请联系管理员。

原文链接:python学习之开发小游戏---skier(滑雪的人),转载请注明来源!

0
相关推荐