2015年12月17日木曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Python 3.5 (プログラミング言語)

Python for Kids: A Playful Introduction to Programming (Jason R. Briggs (著) 、No Starch Press)のPart Ⅲ.(Mr. Stick Man Races for the Exit)、Chapter 16.(Developing the Mr. Stick Man Game)、Programming Puzzles #2: Two-Image Checkerboard(No. 4652)を解いてみる。

Programming Puzzles #2: Two-Image Checkerboard(No. 4652)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import tkinter
import random
import time

class Game:
    def __init__(self):
        self.tk = tkinter.Tk()
        self.tk.title('Mr. Stick Man Races for the Exit')
        self.tk.resizable(0, 0)
        width = 500
        height = 500
        self.tk.wm_attributes('-topmost', 1)
        self.canvas =  tkinter.Canvas(self.tk, width=width, heigh=height,
                                      highlightthickness=0)
        self.bg = tkinter.PhotoImage(file='background1.gif')
        self.bg2 = tkinter.PhotoImage(file='background2.gif')
        w = self.bg.width()
        h = self.bg.height()
        for x in range(0, 5):
            for y in range(0, 5):
                # Checkboard                
                if ((x + y) % 2) == 1:
                    self.canvas.create_image(x * w, y * h,
                                             image=self.bg, anchor='nw')
                else:
                    self.canvas.create_image(x * w, y * h,
                                             image=self.bg2, anchor='nw')

        self.canvas.pack()
        self.tk.update()                
        self.sprites = []
        self.running = True
        self.canvas_height = width
        self.canvas_width = height

    def mainloop(self):
        while True:
            if self.running == True:
                for sprite in self.sprites:
                    sprite.move()
                    self.tk.update_idletasks()
                    self.tk.update()
            time.sleep(0.01)

class Coords:
    def __init__(self, x1=0, y1=0, x2=0, y2=0):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2

class Sprite:
    def __init__(self, game):
        self.game = game
        self.endgame = False
        self.coordinates = None
    def move(self):
        pass

    def coords(self):
        return self.coodinates

class PlatformSprite(Sprite):
    def __init__(self, game, photo_image, x, y, width, height):
        Sprite.__init__(self, game)
        self.photo_image = photo_image
        self.image = game.canvas.create_image(x, y, image=self.photo_image,
                                              anchor='nw')
        self.coordinates = Coords(x, y, x + width, y + height)
        
def within_x(co1, co2):
    if (co2.x1 < co1.x1 < co2.x2) or \
       (co2.x1 < co1.x2 < co2.x2) or \
       (co1.x1 < co2.x1 < co1.x2) or \
       (co1.x1 < co2.x2 < co1.x2):
        return True
    return False

def within_y(co1, co2):
    if (co2.y1 < co1.y1 < co2.y2) or \
       (co2.y1 < co1.y2 < co2.y2) or \
       (co1.y1 < co2.y1 < co1.y2) or \
       (co1.y1 < co2.y2 < co1.y2):
        return True
    return False

def collided_left(co1, co2):
    if within_y(co1, co2):
        if co2.x1 <= co1.x1 <= co2.x2:
            return True
    return False

def colided_right(co1, co2):
    if within_y(co1, co2):
        if co2.x1 <= co1.x2 <= co2.x2:
            return True
    return False

def collided_top(co1, co2):
    if within_x(co1, co2):
        if co2.y1 <= co1.y1 <= co2.y2:
            return True
    return False

def collided_bottom(y, co1, co2):
    if within_x(co1, co2):
        y_calc = co1.y2 + y
        if co2.y1 <= y_calc <= co2.y2:
            return True
    return False
        

        
if __name__ == '__main__':
    game = Game()
    platform1 = PlatformSprite(game, tkinter.PhotoImage(file='platform1.gif'),
                               0, 480, 100, 10)
    platform2 = PlatformSprite(game, tkinter.PhotoImage(file='platform1.gif'),
                               150, 440, 100, 10)
    platform3 = PlatformSprite(game, tkinter.PhotoImage(file='platform1.gif'),
                               300, 400, 100, 10)
    platform4 = PlatformSprite(game, tkinter.PhotoImage(file='platform1.gif'),
                               30, 160, 100, 10)
    platform5 = PlatformSprite(game, tkinter.PhotoImage(file='platform2.gif'),
                               175, 350, 66, 10)
    platform6 = PlatformSprite(game, tkinter.PhotoImage(file='platform2.gif'),
                               50, 380, 66, 10)
    platform7 = PlatformSprite(game, tkinter.PhotoImage(file='platform2.gif'),
                               170, 120, 66, 10)
    platform8 = PlatformSprite(game, tkinter.PhotoImage(file='platform2.gif'),
                               45, 60, 66, 10)
    platform9 = PlatformSprite(game, tkinter.PhotoImage(file='platform3.gif'),
                               170, 250, 32, 10)
    platform10 = PlatformSprite(game, tkinter.PhotoImage(file='platform3.gif'),
                               230, 200, 32, 10)
    game.sprites.append(platform1)
    game.sprites.append(platform2)
    game.sprites.append(platform3)
    game.sprites.append(platform4)
    game.sprites.append(platform5)
    game.sprites.append(platform6)
    game.sprites.append(platform7)
    game.sprites.append(platform8)
    game.sprites.append(platform9)
    game.sprites.append(platform10)
    game.mainloop()

0 コメント:

コメントを投稿

Comments on Google+: