Skip to content

Instantly share code, notes, and snippets.

@gomta777
Created October 20, 2019 17:02
Show Gist options
  • Select an option

  • Save gomta777/faea2969d8d1a66afa72e89682e20bf1 to your computer and use it in GitHub Desktop.

Select an option

Save gomta777/faea2969d8d1a66afa72e89682e20bf1 to your computer and use it in GitHub Desktop.
import pygame
from pygame.locals import *
import sys
import random
import copy
window_size = 640, 480 # 画面サイズの指定
nminocell = 7 # tetriminoの種類の数
piece_size = 4, 4
cell_image_size = 40, 40
cell_size = 20, 20
# ピースの定義 4x4の配列の色番号が入ってるところの座標が収められている
def generate_piece(plist, number): # 4x4の配列と、色番号を淹れるとピース配列を生成する
for i in range(piece_size[0]):
for j in range(piece_size[1]):
plist[i][j] = 0
for n in range(4): # def_piece[0]~[6]の中身に[x, y]が4つづつ入っている
tmp_xy = def_piece[number-1][n]
plist[tmp_xy[0]][tmp_xy[1]] = number # 指定された座標に色番号を仕込む
def main():
pygame.init() # pygameの初期化(おまじない)
screen = pygame.display.set_mode(window_size) # pygameの画面サイズを設定
pygame.display.set_caption("Ahoris") # タイトルバーに表示されるゲームタイトル(どうでもいいやつ)
image = pygame.image.load("./tile.png") # 画像の読み込み
cellimage = []
for i in range(nminocell):
temp_surface = pygame.Surface(cell_image_size) # 40x40の領域を読み込むための画像
temp_surface_dest = pygame.Surface(cell_size) # リサイズして20x20の画像を保存
temp_surface.blit(image, (0, 0), (cell_image_size[0]*i, 0, *cell_image_size)) # 40x40の領域を順に切り出す
pygame.transform.scale(temp_surface, cell_size, temp_surface_dest) # 20x20にリサイズ
cellimage.append(temp_surface_dest) # 7種類分の20x20の領域を順に切り出してリストに保存
del temp_surface # いらない作業領域は削除
nnumber = 0 # tetriminoの色番号
tetrimino = [[0 for i in range(piece_size[0])] for i in range(piece_size[1])] # 4x4の2次元リストを作る
while True: # 画面やイベントの更新処理
for event in pygame.event.get(): # イベントを取得
if event.type == QUIT: # ×ボタンや、終了イベントが発生した時の処理
pygame.quit() # これを書いていないと、例外やエラーで終了する
sys.exit()
if event.type == KEYDOWN: # イベントの種類がキーダウンだったら
if event.key == K_SPACE: # 押されたキーがスペースキーだったら
screen.blit(cellimage[nnumber], (0, 0, *cell_size)) # nminocell[nnumber]を指定座標に表示
nnumber = (nnumber + 1) % nminocell # 1~7の繰り返し
pygame.display.update() # 画面の更新処理(設定したFPSで更新される)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment