2017年3月6日月曜日

開発環境

Python Crash Course (Eric Matthes (著)、No Starch Press)のPART 2(PROJECTS)、PROJECT 1(ALIEN INVASION)、Chapter 12.(A SHIP THAT FIRES BULLETS)のTRY IT YOURSELF 12-4.(Keys)(No.6848)を取り組んでみる。

TRY IT YOURSELF 12-4.(Keys)(No.6848)

コード(Emacs)

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

import sys
import pygame


class Settings:

    def __init__(self):
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)
        self.ship_speed_factor = 10


class Ship:

    def __init__(self, ai_settings, screen):
        self.screen = screen
        self.ai_settings = ai_settings


def check_events(ship):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            print('event.key: {0}'.format(event.key))


def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    ship = Ship(ai_settings, screen)
    while True:
        check_events(ship)

if __name__ == '__main__':
    run_game()

入出力結果(Terminal, IPython)

$ ./sample4.py
event.key: 97
event.key: 98
event.key: 99
event.key: 100
event.key: 101
$

0 コメント:

コメントを投稿