第十章

游戏时间:Skier

首先安装pygame,直接在cmd命令控制框里键入pip install pygame就可以了

代码:

import pygame, sys, random

skier_image = ["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_image[self.angle])
self.rect = self.image.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, image_file, location, type):
pygame.sprite.Sprite.__init__(self)
self.image_file = image_file
self.image = pygame.image.load(image_file)
self.location = location
self.rect = self.image.get_rect()
self.rect.center = location
self.type = type
self.passed = False
def scroll(self, t_ptr):
self.rect.centery = self.location[1] - t_ptr def create_map(start, end):
obstacles = pygame.sprite.Group()
locations = []
gates = pygame.sprite.Group()
for i in range(10):
row = random.randint(start, end)
col = random.randint(0, 9)
location = [col * 64 + 20, row * 64 + 20]
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)
return obstacles def animate():
screen.fill([255, 255, 255])
pygame.display.update(obstacles.draw(screen))
screen.blit(skier.image, skier.rect)
screen.blit(score_text, [10, 10])
pygame.display.flip() def updateObstacleGroup(map0, map1):
obstacles = pygame.sprite.Group()
for ob in map0: obstacles.add(ob)
for ob in map1: obstacles.add(ob)
return obstacles pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
skier = SkierClass()
speed = [0, 6]
map_position = 0
points = 0
map0 = create_map(20, 29)
map1 = create_map(10, 19)
activeMap = 0
obstacles = updateObstacleGroup(map0, map1)
font = pygame.font.SysFont('arial', 50) while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
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 and activeMap == 0:
activeMap = 1
map0 = create_map(20, 29)
obstacles = updateObstacleGroup(map0, map1)
if map_position >=1280 and activeMap == 1:
activeMap = 0
for ob in map0:
ob.location[1] = ob.location[1] - 1280
map_position = map_position -1280
map1 = create_map(10, 19)
obstacles = updateObstacleGroup(map0, map1) for obstacle in obstacles:
obstacle.scroll(map_position) 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, 6]
hit[0].passed = True
elif hit[0].type == "flag" and not hit[0].passed:
points += 10
obstacles.remove(hit[0]) score_text = font.render("Score: " +str(points), 1, (0, 0, 0))
animate()

自己照着书上的代码敲了半天...

结果敲出来一堆的bug..

这就很气了,然后改了半天

对着程序看了又看。。字母好多敲错顺序了。。

最后还是没有把自己的程序调通,很难受

最后只能参考别人的程序了qaq

但发现程序都好像是2.7版的

好像一条控制字体显示的语句在3.6改了。。。如下

font = pygame.font.SysFont('arial', 50)#这是调好了的!

#原文中它是这样的 :
font = pygame.font.Font(None, 50)

也百度找了好久。。还好找出来了。。不然就真的要崩溃了

参考博客:http://blog.sina.com.cn/s/blog_466af1830101e6d3.html

【和孩子一起学编程】 python笔记--第三天的更多相关文章

  1. 教孩子学编程 Python

    教孩子学编程   Python 目录 第1 章 Python 基础:认识环境 111 认识Python 312 用Python 编写程序 513 运行Python 程序 514 本章小结 615 编程 ...

  2. 教孩子学编程 python语言版PDF高清完整版免费下载|百度云盘|Python入门

    百度云盘:教孩子学编程 python语言版PDF高清完整版免费下载 提取码:mnma 内容简介 本书属于no starch的经典系列之一,英文版在美国受到读者欢迎.本书全彩印刷,寓教于乐,易于学习:读 ...

  3. 【和孩子一起学编程】 python笔记--第一天

    [该随笔记录本人在阅读过程写的笔记和一些问题,格式比较随意,不定时更新] 由于该书使用的python版本为2.5,本人使用的为3.6. 第一章: 遇到的第一个问题: 1.3节 输出指令: print ...

  4. 【和孩子一起学编程】 python笔记--第四天

    第十一章: 可变循环 newStars = int(input("how many stars do you want?")) for i in range(newStars): ...

  5. 【和孩子一起学编程】 python笔记--第二天

    第六章 GUI:用户图形界面(graphical user interface) 安装easygui:打开cmd命令窗口,输入:pip install easygui 利用msgbox()函数创建一个 ...

  6. 【和孩子一起学编程】 python笔记--第五天

    关于python2在python3中的改动: https://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5MDEyMDk4Mw==&appmsgid=1 ...

  7. 【Python】Python-skier游戏[摘自.与孩子一起学编程]

    这是一个滑雪者的游戏. skier从上向下滑,途中会遇到树和旗子,捡起一个旗子得10分,碰到一颗树扣100分,可以用左右箭头控制skier方向. 准备素材 一 准备python环境:我下载的pytho ...

  8. 转【Python】Python-skier游戏[摘自.与孩子一起学编程]

    http://www.cnblogs.com/zhaoxd07/p/4914818.html 我遇到的问题 问题1 self.image=pygame.image.load("skier_d ...

  9. [Python笔记]第三篇:深浅拷贝、函数

    本篇主要内容:深浅拷贝,自定义函数,三目运算,lambda表达式, 深浅拷贝 一.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import ...

随机推荐

  1. boost propertyTree

    Boost PropertyTree provides a tree structure to store key/value pairs. Tree structures means that a ...

  2. 关系型数据库MySQL(三)_触发器

    简介 用来给保证数据完整性的一种方法,经常用于加强数据的完整性: 是与表事件相关的特殊的存储过程,与存储过程的唯一区别是触发器不能执行execute语句调用,而是在用户执行SQL语句时自动触发执行 执 ...

  3. CET-6 分频周计划生词筛选(Week 1)

    Week 1 2016.09.03 p17 bias = prejudice / prejudge p18 diminish p19 distinguish/extinguish + majority ...

  4. Redis初阶

  5. JS实现上传图片的三种方法并实现预览图片功能

    地址:http://www.jb51.net/article/118660.htm js HTML5拖拽图片预览 地址:http://www.jb51.net/article/88803.htm js ...

  6. luoguP1313 计算系数 题解(NOIP2011)

    P1313 计算系数 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<cm ...

  7. [fw]LINUX中断描述符初始化

    LINUX中断描述符初始化 @CopyLeft by ICANTH,I Can do ANy THing that I CAN THink!~ Author: WenHui, WuHan Univer ...

  8. Elasticsearch7.3开启x-pack验证

    原文 Elasticsearch7开启x-pack验证 前言 在Elasticsearch7.3,x-pack已经作为默认的插件集成在Elasticsearch里面了,所以无需在bin/elastic ...

  9. Java中的HashMap的2种遍历方式比较

    首先我们准备数据,准备一个map Map<String, String> map = new HashMap<String, String>(); for (int i = 0 ...

  10. MAC如何与linux服务器传递文件

    scp命令可以从本地拷贝文件到Linux服务器,也可以将Linux服务器文件下载到本地 将远程/root/articaleFetch/dist目录下文件和文件夹拷贝到dist文件夹 scp root@ ...