python基础教程代码-这是python基础教程的项目十的源代码

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os, sys, pygame

from pygame.locals import *

import objects, config

class State:

def handle(self, event):

if event.type == QUIT:

sys.exit()

if event.type == KEYDOWN and event.key == K_ESCAPE:

sys.exit()

def firstDisplay(self, screen):

screen.fill(config.background_color)

pygame.display.flip()

def display(self, screen):

pass

class Level(State):

def __init__(self, number = 1):

self.number = number

self.remaining = config.weights_per_level

speed = config.drop_speed

speed += (self.number - 1) * config.speed_increase

self.weight = objects.Weight(speed)

self.banana = objects.Banana()

both = self.weight, self.banana

self.sprites = pygame.sprite.RenderUpdates(both)

def update(self, game):

self.sprites.update()

if self.banana.touches(self.weight):

game.nextState = GameOver()

elif self.weight.landed:

self.weight.reset()

self.remaining -= 1

if self.remaining == 0:

game.nextState = LevelCleared(self.number)

def display(self, screen):

screen.fill(config.background_color)

updates = self.sprites.draw(screen)

pygame.display.update(updates)

class Paused(State):

finished = 0

image = None

text = ''

def handle(self, event):

State.handle(self, event)

if event.type in [MOUSEBUTTONDOWN, KEYDOWN]:

self.finished = 1

def update(self, game):

if self.finished:

game.nextState = self.nextState()

def firstDisplay(self, screen):

screen.fill(config.background_color)

font = pygame.font.Font(None, config.font_size)

lines = self.text.strip().splitlines()

height = len(lines) * font.get_linesize()

center, top = screen.get_rect().center

top -= height // 2

if self.image:

image = pygame.image.load(self.image).convert_alpha()

r = image.get_rect()

top += r.height // 2

r.midbottom = center, top - 20

screen.blit(image, r)

antialias = 1

black = 0, 0, 0

for line in lines:

text = font.render(line.strip(), antialias, black)

r = text.get_rect()

r.midtop = center, top

screen.blit(text, r)

top += font.get_linesize()

pygame.display.flip()

class Info(Paused):

nextState = Level

text = """

In this game you are a banana,

trying to survice a course in

self-defense against fruit, where the

participants will "defend" themselves

against you with a 10 kg weight.

"""

class StartUp(Paused):

nextState = Info

image = config.splash_image

text = """

Welcome to Squish, the game of Fruit Self-Defense.

"""

class LevelCleared(Paused):

def __init__(self, number):

self.number = number

self.text = """

Level %i cleared

Click to start the next level

""" % self.number

def nextState(self):

return Level(self.number + 1)

class GameOver(Paused):

nextState = Level

text = """

GameOver

Click to Restart, Esc to Quit

"""

class Game:

def __init__(self, *args):

path = os.path.abspath(args[0])

dir = os.path.split(path)[0]

os.chdir(dir)

self.state = None

self.nextState = StartUp()

def run(self):

pygame.init()

flag = 0

if config.full_screen:

flag = FULLSCREEN

screen_size = config.screen_size

screen = pygame.display.set_mode(screen_size)

#screen = pygame.display.set_mode(screen_size, flag)

pygame.display.set_caption('Fruit Self Defense')

pygame.mouse.set_visible(False)

while True:

if self.state != self.nextState:

self.state = self.nextState

self.state.firstDisplay(screen)

for event in pygame.event.get():

self.state.handle(event)

self.state.update(self)

self.state.display(screen)

if __name__ == '__main__':

game = Game(*sys.argv)

game.run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python学习课件+python源码90个合集: 002用Python设计第一个游戏(课件+源代码) 004改进我们的小游戏(课件+源代码) 005闲聊之Python的数据类型(课件+源代码) 007了不起的分支和循环(课件) 008了不起的分支和循环2(课件+源代码) 009了不起的分支和循环3(课件+源代码) 017函数:Python的乐高积木(课件+源代码) 019函数:我的地盘听我的(课件+源代码) 022函数:递归是神马(课件+源代码) 023递归:这帮小兔崽子(课件+源代码) 024递归:汉诺塔(课件+源代码) 028文件:因为懂你,所以永恒(课件+源代码) 029文件:一个任务(课件+源代码) 031永久存储:腌制一缸美味的泡菜(课件+源代码) 034丰富的else语句及简洁的with语句(课件+源代码) 034丰富的else语句及简洁的with语句(课件+源代码)(1) 036类和对象:给大家介绍对象(课件 源代码) 037类和对象:面向对象编程(课件 源代码) 038类和对象:继承(课件 源代码) 039类和对象:拾遗(课件 源代码) 045魔法方法:属性访问(课件 源代码) 046魔法方法:描述符(Property的原理)(课件 源代码) 047魔法方法:定制序列(课件 源代码) 048魔法方法:迭代器(课件 源代码) 049乱入:生成器(课件) 050模块:模块就是程序(课件 源代码) 051模块:__name__=_'__main___'、搜索路径和包(课件 源代码) 052模块:像个极客一样去思考(课件) 053论一只爬虫的自我修养(课件) 054论一只爬虫的自我修养2:实战(课件 源代码) 055论一只爬虫的自我修养3:隐藏(课件 源代码) 056论一只爬虫的自我修养4:OOXX(源代码) 062论一只爬虫的自我修养10:安装Scrapy(课件+软件包) 063论一只爬虫的自我修养11:Scrapy框架之初窥门径(课件 源代码) 064GUI的终极选择:Tkinter(课件 源代码) 065GUI的终极选择:Tkinter2(源代码) 066GUI的终极选择:Tkinter3(源代码) 067GUI的终极选择:Tkinter4(源代码) 068GUI的终极选择:Tkinter5(源代码) 069GUI的终极选择:Tkinter6(源代码) 070GUI的终极选择:Tkinter7(源代码) 071GUI的终极选择:Tkinter8(源代码) 072GUI的终极选择:Tkinter9(源代码) 073GUI的终极选择:Tkinter10(源代码) 074GUI的终极选择:Tkinter11(源代码) 075GUI的终极选择:Tkinter12(源代码) 076GUI的终极选择:Tkinter13(源代码) 077GUI的终极选择:Tkinter14(源代码) 078Pygame:初次见面,请大家多多关照(源代码) 080Pygame:事件(源代码) 081Pygame:提高游戏的颜值1(源代码) 082Pygame:提高游戏的颜值2(源代码) 083Pygame:提高游戏的颜值3(源代码) 084Pygame:基本图形绘制(源代码) 085Pygame:动画精灵(源代码) 086Pygame:碰撞检测(源代码) 087Pygame:播放声音和音效(课件 源代码) 088Pygame:摩擦摩擦(源代码) 089Pygame:游戏胜利(源代码) 090Pygame:飞机大战1(源代码) 091Pygame:飞机大战2(源代码) 093Pygame:飞机大战4(源代码) 094Pygame:飞机大战5(源代码) 095Pygame:飞机大战6(源代码) 096Pygame:飞机大战7(源代码) easygui-docs-0.96 Pyhon之常用操作符(课件) 下载必看.txt 元组:戴上了枷锁的列表(课件) 函数:内嵌函数和闭包(课件) 函数:灵活即强大(课件) 列表:一个打了激素的数组2(课件) 列表:一个打了激素的数组3(课件) 列表:一个打了激素的数组(课件) 字典:当索引不好用时(课件) 字符串:各种奇葩的内置方法(课件) 字符串:格式化(课件) 小插曲之变量和字符串(课件) 序列!序列!(课件) 异常处理:你不可能总是对的(课件) 愉快的开始(课件)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值