pygameでインベーダーゲームもどき
大砲、大砲から発射する玉、打つ対象のトンボ、障壁
のそれぞれの座標管理が必要です。
それぞれの座標は玉とトンボの衝突、玉と障壁の衝突
の検出に使います。
さあゲームクリア出来る人はいるのでしょうか?
制限時間は15秒です。
from pygame.locals import *
import sys
import pygame
import random
import time
WIDTH = 640 # 幅
HEIGHT = 400 # 高さ
WALL_COUNT = 2
BALL_COUNT = 10
TARGET_COUNT = 10
class top_app():
def __init__(self):
pygame.init()
self.main_surface = pygame.display.set_mode((500, 500))
self.by0 =400
self.by1 =400
self.by2 =400
self.x3=300
self.y3 =400
self.target_size=8
self.wall_size=40
self.xy0state=0
self.khit=0
self.state = 0
self.hit_count=0
self.stop=1
self.hit=[0,1,2,3,4,5,6,7,8,9]
self.state = 0
self.kstate = 0
self.xstate=[0,1,2,3,4,5,6,7,8,9]
self.ystate=[0,1,2,3,4,5,6,7,8,9]
self.x_wall=100
self.y_wall=100
self.xwall_state=0
self.wall_hit = 0
self.dummy_hit = 0
self.bxt=[0,1,2,3,4,5,6,7,8,9]
self.byt=[0,1,2,3,4,5,6,7,8,9]
self.xk=[0,1,2,3,4,5,6,7,8,9]
self.yk=[0,1,2,3,4,5,6,7,8,9]
self.x=[0,1,2,3,4,5,6,7,8,9]
self.y=[0,1,2,3,4,5,6,7,8,9]
self.dummy_x=[0,1,2,3,4,5,6,7,8,9]
self.dummy_y=[0,1,2,3,4,5,6,7,8,9]
self.xkstate=[0,1,2,3,4,5,6,7,8,9]
self.ykstate=[0,1,2,3,4,5,6,7,8,9]
self.scenario_state=0
self.timeout=0
self.loop_count=0
for i in range(BALL_COUNT):
self.bxt[i]=300
self.byt[i]=400
self.xk[i]=150
self.yk[i]=30
self.x[i]=150
self.y[i]=30
self.dummy_x[i]=150
self.dummy_y[i]=100
self.xkstate[i]=0
self.ykstate[i]=0
self.xstate[i]=0
self.ystate[i]=0
self.hit[i]=0
self.dummy_x[i]=self.x_wall
self.dummy_y[i]=self.y_wall
def main(self):
self.game_loop()
pygame.quit()
sys.exit()
def hit_check(self):
for j in range(BALL_COUNT):
for i in range(TARGET_COUNT):
if(((self.bxt[j]>(self.x[i]-self.target_size-10))and(self.bxt[j]<(self.x[i]+self.target_size+10)))
and((self.byt[j]>(self.y[i]-10))and(self.byt[j]<(self.y[i]+10)))):
self.hit[i]=1
def wall_col_check(self):
for j in range(BALL_COUNT):
if(((self.bxt[j]>(self.x_wall-self.target_size))and(self.bxt[j]<(self.x_wall+self.target_size+50)))
and((self.byt[j]>(self.y_wall-10))and(self.byt[j]<(self.y_wall+10)))):
self.wall_hit = 1
def dummy_col_check(self):
for j in range(BALL_COUNT):
if(((self.dummy_x[j]>(self.x3-50))and(self.dummy_x[j]<(self.x3)))
and((self.dummy_y[j]>(400-10))and(self.dummy_y[j]<(400+10)))):
self.dummy_hit = 1
def all_hit(self):
for i in range(TARGET_COUNT):
if (self.hit[i]==0):
return 0
else:
pass
return 1
def make_wall(self):
pygame.draw.rect(self.main_surface, (100,0,255), (self.x_wall+10, self.y_wall-10,10,10))
pygame.draw.rect(self.main_surface, (100,0,255), (self.x_wall+20, self.y_wall-20,10,10))
pygame.draw.rect(self.main_surface, (100,0,255), (self.x_wall+20, self.y_wall-10,10,10))
pygame.draw.rect(self.main_surface, (100,0,255), (self.x_wall+30, self.y_wall-10,10,10))
pygame.draw.rect(self.main_surface, (100,0,255), (self.x_wall, self.y_wall,50,10))
def make_target(self):
for i in range(TARGET_COUNT):
if(self.hit[i]==0):
pygame.draw.circle(self.main_surface, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (self.x[i], self.y[i]), self.target_size)
pygame.draw.circle(self.main_surface, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (self.x[i]-8, self.y[i]-8), self.target_size)
pygame.draw.circle(self.main_surface, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (self.x[i]-8, self.y[i]+8), self.target_size)
pygame.draw.circle(self.main_surface, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (self.x[i]+8, self.y[i]-8), self.target_size)
pygame.draw.circle(self.main_surface, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (self.x[i]+8, self.y[i]+8), self.target_size)
pygame.draw.circle(self.main_surface, (0,0,0), (self.x[i], self.y[i]+16), self.target_size)
pygame.draw.circle(self.main_surface, (0,255,0), (self.x[i]+8, self.y[i]+16), self.target_size)
pygame.draw.circle(self.main_surface, (0,255,0), (self.x[i]-8, self.y[i]+16), self.target_size)
pygame.draw.circle(self.main_surface, (0,255,0), (self.x[i]+16, self.y[i]+16), self.target_size)
pygame.draw.circle(self.main_surface, (0,255,0), (self.x[i]-16, self.y[i]+16), self.target_size)
pygame.draw.circle(self.main_surface, (0,255,0), (self.x[i]+24, self.y[i]+16), self.target_size)
pygame.draw.circle(self.main_surface, (0,255,0), (self.x[i]-24, self.y[i]+16), self.target_size)
pygame.draw.circle(self.main_surface, (0,0,0), (self.x[i], self.y[i]+24), self.target_size)
pygame.draw.circle(self.main_surface, (0,0,0), (self.x[i], self.y[i]+32), self.target_size)
else:
pass
def make_target2(self):
for i in range(TARGET_COUNT):
if(self.hit[i]==0):
pygame.draw.rect(self.main_surface,(247, 247, 247) , (self.x[i]+0*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(246, 247, 249) , (self.x[i]+1*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(255, 243, 253) , (self.x[i]+2*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(235, 195, 46) , (self.x[i]+3*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(240, 219, 68) , (self.x[i]+4*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(243, 213, 67) , (self.x[i]+5*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(245, 210, 64) , (self.x[i]+6*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(227, 181, 36) , (self.x[i]+7*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(233, 218, 161) , (self.x[i]+8*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(246, 246, 244) , (self.x[i]+9*5, self.y[i],5,5))
pygame.draw.rect(self.main_surface,(248, 246, 247) , (self.x[i]+0*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(245, 245, 243) , (self.x[i]+1*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(234, 191, 50) , (self.x[i]+2*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(239, 218, 67) , (self.x[i]+3*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(239, 213, 64) , (self.x[i]+4*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(241, 206, 60) , (self.x[i]+5*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(241, 203, 58) , (self.x[i]+6*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(231, 200, 57) , (self.x[i]+7*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(248, 238, 106) , (self.x[i]+8*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(244, 231, 179) , (self.x[i]+9*5, self.y[i]+10/2,5,5))
pygame.draw.rect(self.main_surface,(244, 250, 240) , (self.x[i]+0*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(236, 197, 60) , (self.x[i]+1*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(239, 215, 67) , (self.x[i]+2*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(240, 213, 64) , (self.x[i]+3*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(241, 210, 60) , (self.x[i]+4*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(238, 202, 54) , (self.x[i]+5*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(241, 201, 54) , (self.x[i]+6*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(238, 198, 51) , (self.x[i]+7*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(237, 194, 56) , (self.x[i]+8*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(241, 208, 92) , (self.x[i]+9*5, self.y[i]+20/2,5,5))
pygame.draw.rect(self.main_surface,(255, 244, 248) , (self.x[i]+0*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(239, 201, 54) , (self.x[i]+1*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(238, 211, 62) , (self.x[i]+2*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(240, 205, 59) , (self.x[i]+3*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(241, 201, 54) , (self.x[i]+4*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(240, 200, 50) , (self.x[i]+5*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(239, 201, 56) , (self.x[i]+6*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(233, 192, 48) , (self.x[i]+7*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(234, 191, 50) , (self.x[i]+8*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(252, 241, 100) , (self.x[i]+9*5, self.y[i]+30/2,5,5))
pygame.draw.rect(self.main_surface,(242, 249, 242) , (self.x[i]+0*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(239, 211, 65) , (self.x[i]+1*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(240, 205, 59) , (self.x[i]+2*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(238, 200, 53) , (self.x[i]+3*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(241, 199, 51) , (self.x[i]+4*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(242, 201, 57) , (self.x[i]+5*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(232, 191, 47) , (self.x[i]+6*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(233, 188, 45) , (self.x[i]+7*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(228, 185, 44) , (self.x[i]+8*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(230, 183, 41) , (self.x[i]+9*5, self.y[i]+40/2,5,5))
pygame.draw.rect(self.main_surface,(242, 246, 255) , (self.x[i]+0*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(236, 191, 46) , (self.x[i]+1*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(242, 201, 51) , (self.x[i]+2*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(242, 197, 52) , (self.x[i]+3*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(238, 196, 52) , (self.x[i]+4*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(231, 189, 45) , (self.x[i]+5*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(230, 185, 42) , (self.x[i]+6*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(230, 183, 41) , (self.x[i]+7*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(229, 182, 42) , (self.x[i]+8*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(225, 178, 36) , (self.x[i]+9*5, self.y[i]+50/2,5,5))
pygame.draw.rect(self.main_surface,(255, 245, 233) , (self.x[i]+0*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(234, 189, 44) , (self.x[i]+1*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(216, 188, 44) , (self.x[i]+2*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(231, 186, 43) , (self.x[i]+3*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(231, 184, 44) , (self.x[i]+4*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(231, 184, 44) , (self.x[i]+5*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(228, 181, 41) , (self.x[i]+6*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(227, 178, 37) , (self.x[i]+7*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(229, 178, 37) , (self.x[i]+8*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(234, 196, 53) , (self.x[i]+9*5, self.y[i]+60/2,5,5))
pygame.draw.rect(self.main_surface,(247, 246, 244) , (self.x[i]+0*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(252, 244, 208) , (self.x[i]+1*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(226, 193, 52) , (self.x[i]+2*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(226, 179, 39) , (self.x[i]+3*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(227, 178, 37) , (self.x[i]+4*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(229, 178, 37) , (self.x[i]+5*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(227, 176, 35) , (self.x[i]+6*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(228, 174, 39) , (self.x[i]+7*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(225, 173, 38) , (self.x[i]+8*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(221, 172, 31) , (self.x[i]+9*5, self.y[i]+70/2,5,5))
pygame.draw.rect(self.main_surface,(247, 247, 247) , (self.x[i]+0*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(250, 246, 243) , (self.x[i]+1*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(227, 204, 136) , (self.x[i]+2*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(223, 185, 58) , (self.x[i]+3*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(228, 175, 35) , (self.x[i]+4*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(227, 173, 38) , (self.x[i]+5*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(225, 171, 37) , (self.x[i]+6*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(226, 170, 33) , (self.x[i]+7*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(224, 163, 13) , (self.x[i]+8*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(245, 246, 241) , (self.x[i]+9*5, self.y[i]+80/2,5,5))
pygame.draw.rect(self.main_surface,(247, 247, 247) , (self.x[i]+0*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(247, 247, 247) , (self.x[i]+1*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(247, 248, 243) , (self.x[i]+2*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(241, 242, 255) , (self.x[i]+3*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(228, 177, 49) , (self.x[i]+4*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(234, 173, 22) , (self.x[i]+5*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(227, 171, 16) , (self.x[i]+6*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(249, 244, 240) , (self.x[i]+7*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(250, 246, 245) , (self.x[i]+8*5, self.y[i]+90/2,5,5))
pygame.draw.rect(self.main_surface,(247, 247, 247) , (self.x[i]+9*5, self.y[i]+90/2,5,5))
else:
pass
def make_dummy(self):
for i in range(TARGET_COUNT):
pygame.draw.circle(self.main_surface, (255,0,0), (self.dummy_x[i]+25, self.dummy_y[i]), self.target_size)
pygame.draw.circle(self.main_surface, (0,0,0), (self.dummy_x[i]+25, self.dummy_y[i]+8), self.target_size)
def make_gun(self):
pygame.draw.rect(self.main_surface, (255,0,255), (self.x3-10, self.y3-10,20,20))
pygame.draw.rect(self.main_surface, (255,0,255), (self.x3-25, self.y3,50,20))
def make_gun_out(self):
pygame.draw.rect(self.main_surface, (200,200,200), (self.x3-10, self.y3-10,20,20))
pygame.draw.rect(self.main_surface, (200,200,200), (self.x3-25, self.y3,50,20))
def make_ball(self):
for i in range(TARGET_COUNT):
pygame.draw.circle(self.main_surface, (random.randint(0,255),random.randint(0,255),random.randint(0,255)), (self.bxt[i], self.byt[i]), 10)
def target_move(self):
#的移動
for i in range(TARGET_COUNT):
if(self.xstate[i]==0):
self.x[i] += (1 +i)
if(self.x[i]>400):
self.xstate[i]=1
if(self.xstate[i]==1):
self.x[i] -= (1 +i)
if(self.x[i]<150):
self.xstate[i]=0
if(self.ystate[i]==0):
self.y[i] += (1 +i)
if(self.y[i]>300):
self.ystate[i]=1
if(self.ystate[i]==1):
self.y[i] -= (1 +i)
if(self.y[i]<150):
self.ystate[i]=0
def dummy_move(self):
for i in range(TARGET_COUNT):
if(self.xwall_state ==0):
self.dummy_x[i] += (1+self.scenario_state)
if(self.xwall_state==1):
self.dummy_x[i] -= (1+self.scenario_state)
self.dummy_y[i] += (10)
if(self.dummy_y[i] >400):
self.dummy_y[i]=100
def wall_move(self):
if(self.xwall_state ==0):
self.x_wall += (1+self.scenario_state)
if(self.x_wall>400):
self.xwall_state=1
if(self.xwall_state==1):
self.x_wall -= (1+self.scenario_state)
if(self.x_wall<150):
self.xwall_state=0
def game_loop(self):
pygame.display.set_caption("SHOOTING TEST")
clock = pygame.time.Clock()
stop_button = pygame.Rect(30, 30, 80, 50)
start_button = pygame.Rect(30, 130, 80, 50)
reset_button = pygame.Rect(30, 230, 80, 50)
font = pygame.font.SysFont(None, 25)
text1 = font.render("STOP", True, (0,0,0))
text2 = font.render("START", True, (0,0,0))
text3 = font.render("SHOOT", True, (0,0,0))
text4 = font.render("CURSOL:SHOOTER MOVE / SPACE ball start ", True, (0,0,0))
texthit = font.render(" ", True, (0,0,0))
start = time.time()
going = True
while going:
if (self.stop==1):
start = time.time()
t = time.time() - start
if(t>15):
self.timeout=1
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
#大砲移動 左
self.x3=self.x3-10
if pressed[pygame.K_RIGHT]:
#大砲移動 右
self.x3=self.x3+10
for event in pygame.event.get():
if event.type == pygame.QUIT:
going = False
if event.type == KEYDOWN:
if event.key == K_LEFT:
#大砲移動 左
self.x3=self.x3-10
if event.key == K_RIGHT:
#大砲移動 右
self.x3=self.x3+10
if event.key == K_SPACE:
#弾発射
self.bxt[self.xy0state]=self.x3
self.byt[self.xy0state]=400
self.xy0state += 1
if(self.xy0state==9):
self.xy0state=0
self.stop=0
if event.type == pygame.MOUSEBUTTONDOWN:
if stop_button.collidepoint(event.pos):
self.stop=1
if start_button.collidepoint(event.pos):
self.stop=0
if reset_button.collidepoint(event.pos):
#弾発射
self.bxt[self.xy0state]=self.x3
self.byt[self.xy0state]=400
self.xy0state += 1
if(self.xy0state==9):
self.xy0state=0
self.stop=0
#的衝突
self.hit_check()
#self.wall_col_check()
self.dummy_col_check()
self.main_surface.fill((220, 220, 220))
pygame.draw.rect(self.main_surface, (255, 0, 0), stop_button)
pygame.draw.rect(self.main_surface, (255, 255, 0), start_button)
pygame.draw.rect(self.main_surface, (0, 0, 255), reset_button)
self.by0 -=10
self.by1 -=10
self.by2 -=10
if(self.stop==1):
pass
else:
for i in range(BALL_COUNT):
self.byt[i]-=10
#的移動
self.target_move()
self.make_wall()
self.wall_move()
#弾
self.make_ball()
#大砲
self.make_gun()
self.make_dummy()
self.dummy_move()
#的
if(self.scenario_state==0):
texthit = font.render("STAGE1", True, (0,0,0))
self.make_target()
if (self.all_hit()==1):
self.scenario_state = 1
for i in range(BALL_COUNT):
self.hit[i]=0
if(self.scenario_state==1):
texthit = font.render("STAGE2", True, (0,0,0))
self.make_target2()
if (self.all_hit()==1):
self.scenario_state = 2
for i in range(BALL_COUNT):
self.hit[i]=0
if(self.scenario_state==2):
texthit = font.render("GAME CLEAR !", True, (0,0,0))
self.stop=1
self.main_surface.blit(text1, (40, 45))
self.main_surface.blit(text2, (40,145))
self.main_surface.blit(text3, (40,245))
self.main_surface.blit(text4, (40,430))
if(self.dummy_hit==1):
self.make_gun_out()
texthit = font.render("BALL HIT TO GUN GAME OVER!", True, (255,0,0))
self.stop=1
if(self.wall_hit==1):
texthit = font.render("WALL HIT GAME OVER!", True, (255,0,0))
self.stop=1
if(self.timeout==1):
texthit = font.render("TIMEOUT!", True, (255,0,0))
self.stop=1
if(self.khit==1):
texthit = font.render("game over count="+str(self.hit_count), True, (0,0,0))
else:
pass
self.main_surface.blit(texthit, (200,45))
pygame.display.update()
clock.tick(50)
self.loop_count+=1
if __name__ == '__main__':
app = top_app()
app.main()
pythonでテキストファイルを読みだし自動スクロール
ソースは以下です
### インポート
import tkinter
from tkinter import *
from tkinter.scrolledtext import ScrolledText
import threading
import time
### インポート
import tkinter
import glob
from tkinter import *
from PIL import ImageTk, Image
import os
import sys
import time
import tkinter
from PIL import Image, ImageTk
import threading
import time
import glob
import os,sys
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image, ImageTk
import shutil
import os
import glob
from tkinter import filedialog as tkFileDialog
import tkinter as tk
from tkinter import font
import tkinter.font as tkFont
from chardet import detect
class main_class():
def __init__(self, main):
self.h=30
self.w=100
print ("__init__")
self.interval=0.5
self.kill = 0
self.font=10
button1= Button(root, text=u'終了', command=self.quit)
button1.grid(row=0, column=1)
button1.place(x=900, y=30)
self.textExample=ScrolledText(root, width=self.w,height=self.h, wrap=tkinter.CHAR)
self.textExample.pack()
self.textExample.place(x=90, y=70)
button3= Button(root, text=u'ファイル 選択', command=self.button3_clicked)
button3.grid(row=0, column=1)
button3.place(x=100, y=30)
button4= Button(root, text=u'中断', command=self.button4_clicked)
button4.grid(row=0, column=1)
button4.place(x=250, y=30)
button7= Button(root, text=u'再開', command=self.button7_clicked)
button7.grid(row=0, column=1)
button7.place(x=300, y=30)
button5= Button(root, text=u'遅く', command=self.button5_clicked)
button5.grid(row=0, column=1)
button5.place(x=350, y=30)
button6= Button(root, text=u'早く', command=self.button6_clicked)
button6.grid(row=0, column=1)
button6.place(x=400, y=30)
button8= Button(root, text=u'フォント大', command=self.button8_clicked)
button8.grid(row=0, column=1)
button8.place(x=450, y=30)
button9= Button(root, text=u'フォント小', command=self.button9_clicked)
button9.grid(row=0, column=1)
button9.place(x=520, y=30)
button10= Button(root, text=u'枠大', command=self.button10_clicked)
button10.grid(row=0, column=1)
button10.place(x=600, y=30)
button11= Button(root, text=u'枠小', command=self.button11_clicked)
button11.grid(row=0, column=1)
button11.place(x=650, y=30)
def method0(self,message):
print ("method0")
self.textExample.insert(tkinter.END,message)
def thread_method(self):
loop_flag=loop_v.get()
print ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
print(loop_flag)
if(loop_flag==1):
self.text_main()
else:
while(1):
if(self.kill==1):
break
self.text_main()
def text_main(self):
if(self.stop==1):
while(1):
time.sleep(1)
if(self.stop==0):
break
self.textExample.delete("1.0",tkinter.END)
for name in self.filenames:
if(self.kill==1):
break
if(self.stop==1):
while(1):
time.sleep(1)
if(self.kill==1):
break
if(self.stop==0):
break
with open(name, 'rb') as f: # バイナリファイルとしてファイルをオープン
b = f.read() # ファイルの内容を全て読み込む
enc = detect(b)
self.encode_type=enc['encoding']
with open(name,encoding=self.encode_type) as f:
if(self.stop==1):
while(1):
time.sleep(1)
if(self.kill==1):
break
if(self.stop==0):
break
lines = f.readlines()
for line in lines:
if(self.kill==1):
break
if(self.stop==1):
while(1):
time.sleep(1)
if(self.kill==1):
break
if(self.stop==0):
break
print(line, end='')
self.textExample.insert(tkinter.END,str(line)+"\n")
self.textExample.configure(font=("Courier", self.font),height=self.h,width=self.w)
time.sleep(self.interval)
self.textExample.yview_moveto(1)
def button3_clicked(self):
self.kill = 1
fTyp = [('', '*')]
iDir = os.path.abspath(os.path.dirname(__file__))
self.filenames = tkFileDialog.askopenfilenames(filetypes= [("Text file", ".txt")], initialdir=iDir)
print(self.filenames)
self.stop = 0
self.kill = 0
self.textExample.delete("1.0",tkinter.END)
thread1 = threading.Thread(target=self.thread_method)
thread1.start()
def button4_clicked(self):
self.stop = 1
def button7_clicked(self):
self.stop = 0
def button5_clicked(self):
self.interval=self.interval + 0.1
def button6_clicked(self):
if( self.interval > 0.1):
self.interval=self.interval - 0.1
def button8_clicked(self):
self.font=self.font + 1
def button9_clicked(self):
if( self.font > 8):
self.font=self.font - 1
def button10_clicked(self):
self.w=self.w + 2
self.h=self.h + 1
def button11_clicked(self):
self.w=self.w - 2
self.h=self.h - 1
def quit(self):
print ("quit")
root.destroy()
root= tkinter.Tk()
c=main_class(root)
root.title("テキストスクロール")
root.geometry("1100x600")
loop_v = tkinter.IntVar(value=1)
px_radio_1 = tkinter.Radiobutton(
root,
text="繰り返し無し",
value=1,
variable=loop_v
)
px_radio_1.place(x=700, y=30)
px_radio_2 = tkinter.Radiobutton(
root,
text="繰り返しあり",
value=2,
variable=loop_v
)
px_radio_2.place(x=800, y=30)
root.mainloop()
python SQLite3メモ
ソースは以下です。
############################################
from tkinter.scrolledtext import ScrolledText
import datetime
import tkinter
import sys
from tkinter import messagebox
import sqlite3
from contextlib import closing
import requests
from bs4 import BeautifulSoup
from tkinter import ttk
import webbrowser
import os
dbname = '../memo.db'
fontsize =10
root = tkinter.Tk()
def getTextInput():
result=textExample.get("1.0","end")
print(result)
textExample=ScrolledText(root, height=40,width=80, wrap=tkinter.CHAR)
textExample.pack()
textExample.place(x=90, y=70)
btnRead=tkinter.Button(root, height=1, width=10, text="Clear",
command=getTextInput)
def execute():
func =function.get()
if(func==1):
btn_click2()
if(func==2):
btn_click10()
if(func==3):
btn_click8()
if(func==4):
btn_click9()
if(func==5):
btn_click4()
if(func==6):
btn_click2_sc()
if(func==7):
btn_click5()
def textclear():
textExample.delete("1.0",tkinter.END)
def btn_click6():
global fontsize
fontsize = fontsize + 1
textExample.configure(font=("Courier", fontsize))
def btn_click7():
global fontsize
fontsize = fontsize - 1
textExample.configure(font=("Courier", fontsize))
#検索
def btn_click():
textExample.configure(font=("Courier", 10))
data_exist =0
get_data =txt2.get()
match_word = get_data
if match_word=="":
return
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
select_sql = 'select * from items where mean like '+'"%'+str(match_word)+'%"'
data=[]
print (select_sql )
try:
for row in c.execute(select_sql):
data_exist = 1;
print(row)
print(type(row))
text = "-".join(map(str, row))
print(text)
print(type(text))
data.append(text)
text2="".join(map(str, data))
data.append("----------------------------------------------------------------\n")
conn.commit()
web_out(text2)
except:
print("data exception")
textExample.delete("1.0",tkinter.END)
textExample.insert(tkinter.END,text2)
return data_exist
def btn_click10():
textExample.configure(font=("Courier", 10))
data_exist =0
get_data =txt2.get()
match_word = ""
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
select_sql = 'select * from items where mean like '+'"%'+str(match_word)+'%"'
data=[]
print (select_sql )
try:
for row in c.execute(select_sql):
data_exist = 1;
print(row)
print(type(row))
text = "-".join(map(str, row))
if(header_only.get()==2):
#if combovalue =='header':
text=text[:80]
print(text)
print(type(text))
data.append(text)
text2="".join(map(str, data))
data.append("----------------------------------------------------------------\n")
conn.commit()
web_out(text2)
except:
print("data exception")
textExample.delete("1.0",tkinter.END)
textExample.insert(tkinter.END,text2)
return data_exist
combovalue = "all"
def web_out(message):
SAMPLE_DIR = "C:\\html_link"
if not os.path.exists(SAMPLE_DIR):
# ディレクトリが存在しない場合、ディレクトリを作成する
os.makedirs(SAMPLE_DIR)
web_site=SAMPLE_DIR+"\\scraping_result.html"
f = open(web_site, 'w',encoding='utf-8', errors='ignore')
message=str(message)
datalist = []
datalist.append('<html>\n')
datalist.append('<head>\n')
datalist.append('<title>from python</title>\n')
datalist.append('</head>\n')
datalist.append('<body>\n')
datalist.append(message)
datalist.append('\n')
datalist.append('</body>\n')
datalist.append('</html>\n')
f.writelines(datalist)
f.close()
webbrowser.open(web_site)
def show_selected(event): #eventを引数に
global combovalue
combovalue=test_combobox.get()
print(combovalue) #選択した値を表示
#指定キー表示
def btn_click8():
textExample.configure(font=("Courier", 10))
data_exist =0
get_data =txt.get()
match_word = get_data
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
select_sql = 'select * from items where item_id like '+'"%'+str(match_word)+'%"'
data=[]
print (select_sql )
try:
for row in c.execute(select_sql):
data_exist = 1;
print(row)
print(type(row))
text = "-".join(map(str, row))
print(text)
print(type(text))
data.append(text)
text2="".join(map(str, data))
data.append("----------------------------------------------------------------\n")
conn.commit()
web_out(text2)
except:
print("data exception")
textExample.delete("1.0",tkinter.END)
textExample.insert(tkinter.END,text2)
return data_exist
#追加
def btn_click2():
txt.delete(0,tkinter.END)
now = datetime.datetime.now()
txt.insert(tkinter.END,now)
txt.insert(tkinter.END,"\n")
get_data =txt.get()
get_mean =textExample.get('1.0', 'end')
#if btn_click() == 0:
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
create_table = '''create table items (item_id INTEGER PRIMARY KEY,word TEXT,mean TEXT)'''
try:
c.execute(create_table)
except:
print("database already exist")
insert_sql = 'insert into items (word, mean) values (?,?)'
items = [
(get_data, get_mean)
]
c.executemany(insert_sql, items )
conn.commit()
#else:
# print("既に登録済")
# textExample.delete("1.0",tkinter.END)
# textExample.insert(tkinter.END,"既に登録済")
def data_print():
import requests
get_url =txt_url.get()
site = requests.get(get_url)
data = BeautifulSoup(site.text, 'html.parser')
textExample.insert(tkinter.END,data.find_all("p"))
SAMPLE_DIR = "C:\\html_link"
if not os.path.exists(SAMPLE_DIR):
# ディレクトリが存在しない場合、ディレクトリを作成する
os.makedirs(SAMPLE_DIR)
web_site=SAMPLE_DIR+"\\scraping_result.html"
f = open(web_site, 'w',encoding='utf-8', errors='ignore')
message=str(data.find_all("a"))
web_out(message)
def btn_click2_sc():
txt.delete(0,tkinter.END)
now = datetime.datetime.now()
txt.insert(tkinter.END,now)
txt.insert(tkinter.END,"\n")
data_print()
get_data =txt.get()
get_mean =textExample.get('1.0', 'end')
#if btn_click() == 0:
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
create_table = '''create table items (item_id INTEGER PRIMARY KEY,word TEXT,mean TEXT)'''
try:
c.execute(create_table)
except:
print("database already exist")
insert_sql = 'insert into items (word, mean) values (?,?)'
items = [
(get_data, get_mean)
]
c.executemany(insert_sql, items )
conn.commit()
#else:
# print("既に登録済")
# textExample.delete("1.0",tkinter.END)
# textExample.insert(tkinter.END,"既に登録済")
#テキストボックスクリア
def btn_click3():
textExample.delete("1.0",tkinter.END)
#キー指定削除
def btn_click4():
get_data =txt.get()
match_word = get_data
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
select_sql = 'delete from items where item_id = '+'"'+str(match_word)+'"'
data=[]
print (select_sql )
try:
for row in c.execute(select_sql):
print(row)
data.append(row)
conn.commit()
except:
print("data not found")
#指定キー更新
def btn_click9():
get_data =txt.get()
match_word = get_data
get_mean =textExample.get('1.0', 'end')
#if btn_click() == 0:
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
insert_sql = 'update items set mean ='+'"'+str(get_mean)+'"'+ 'where item_id = '+'"'+str(match_word)+'"'
print(insert_sql)
c.execute(insert_sql)
conn.commit()
def btn_click5():
ret = messagebox.askyesno('確認', '全削除やめますか?')
if ret == True:
pass
else:
get_data =txt.get()
match_word = get_data
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
select_sql = 'delete from items'
data=[]
print (select_sql )
try:
for row in c.execute(select_sql):
print(row)
data.append(row)
conn.commit()
except:
print("data not found")
textExample.delete("1.0",tkinter.END)
textExample.insert(tkinter.END,"削除しました")
# ボタン
btn = tkinter.Button(root, text='キーワード検索', command=btn_click)
btn.place(x=300, y=10)
"""
btn2 = tkinter.Button(root, text='追加', command=btn_click2)
btn2.place(x=10, y=110)
btn3 = tkinter.Button(root, text='入力クリア', command=btn_click3)
btn3.place(x=10, y=570)
btn4 = tkinter.Button(root, text='キー指定削除', command=btn_click4)
btn4.place(x=10, y=150)
btn5 = tkinter.Button(root, text='全削除', command=btn_click5)
btn5.place(x=10, y=180)
"""
btn6 = tkinter.Button(root, text='フォント大', command=btn_click6)
btn6.place(x=10, y=210)
btn7 = tkinter.Button(root, text='フォント小', command=btn_click7)
btn7.place(x=10, y=240)
"""
btn8 = tkinter.Button(root, text='キー指定表示', command=btn_click8)
btn8.place(x=10, y=270)
btn9 = tkinter.Button(root, text='キー指定更新', command=btn_click9)
btn9.place(x=10, y=300)
btn10 = tkinter.Button(root, text='全record表示', command=btn_click10)
btn10.place(x=10, y=330)
btn11 = tkinter.Button(root, text='scraping追加', command=btn_click2_sc)
btn11.place(x=10, y=360)
"""
# 画面サイズ
root.geometry('1000x700')
# 画面タイトル
root.title('メモデータベース')
# ラベル
lbl = tkinter.Label(text='キー')
lbl.place(x=10, y=10)
lbl3 = tkinter.Label(text='Scraping URL')
lbl3.place(x=10, y=40)
lbl2 = tkinter.Label(text='メモ')
lbl2.place(x=10, y=70)
# テキストボックス
txt = tkinter.Entry(width=30)
txt.place(x=90, y=10)
txt.insert(tkinter.END,"")
# テキストボックス
txt2 = tkinter.Entry(width=42)
txt2.place(x=400, y=10)
txt2.insert(tkinter.END,"")
txt_url = tkinter.Entry(width=80)
txt_url.place(x=120, y=40)
txt_url.insert(tkinter.END,"")
#root = tkinter.Tk()
"""
item_list = ['all', 'header']
test_combobox = ttk.Combobox(
master=root,
values=item_list,
)
#値選択時に発生するイベントと関数を紐づけ
test_combobox.bind(
'<<ComboboxSelected>>', #選択時に発生するイベント
show_selected, #呼び出す関数
)
test_combobox.current(0)
test_combobox.pack()
"""
header_only = tkinter.IntVar(value=1)
px_radio_1 = tkinter.Radiobutton(
root,
text="全て",
value=1,
variable=header_only
)
px_radio_1.place(x=700, y=30)
px_radio_2 = tkinter.Radiobutton(
root,
text="ヘッダーのみ",
value=2,
variable=header_only
)
px_radio_2.place(x=800, y=30)
function = tkinter.IntVar(value=1)
func_radio_1 = tkinter.Radiobutton(
root,
text="追加",
value=1,
variable=function
)
func_radio_1.place(x=700, y=60)
func_radio_2 = tkinter.Radiobutton(
root,
text="全表示",
value=2,
variable=function
)
func_radio_2.place(x=700, y=80)
func_radio_3 = tkinter.Radiobutton(
root,
text="キー指定表示",
value=3,
variable=function
)
func_radio_3.place(x=700, y=100)
func_radio_4 = tkinter.Radiobutton(
root,
text="キー指定更新",
value=4,
variable=function
)
func_radio_4.place(x=700, y=120)
func_radio_5 = tkinter.Radiobutton(
root,
text="キー指定削除",
value=5,
variable=function
)
func_radio_5.place(x=700, y=140)
func_radio_6 = tkinter.Radiobutton(
root,
text="スクレーピング",
value=6,
variable=function
)
func_radio_6.place(x=700, y=160)
func_radio_7 = tkinter.Radiobutton(
root,
text="全削除",
value=7,
variable=function
)
func_radio_7.place(x=700, y=180)
btn12 = tkinter.Button(root, text='実行', command=execute)
btn12.place(x=700, y=250)
btn13 = tkinter.Button(root, text='クリア', command=textclear)
btn13.place(x=700, y=300)
# 表示
root.mainloop()