楽しく学ぶパイソンゲームでの怖い家族が追いかけてくるゲームを作る際の参考にしました。

 #1 .ゲームの準備をする
import pygame as pg, sys
import random
pg.init()
screen = pg.display.set_mode((800,600))

imageR = pg.image.load("images/playerR.png")
imageR = pg.transform.scale(imageR,(40,50))
imageL = pg.transform.flip(imageR,True,False)
myrect = pg.Rect(50,200,40,50) #罠データ 
trapimg = pg.image.load("images/uni.png")
trapimg = pg.transform.scale(trapimg,(30,30))
traps = []
for i in range(20):
    wx = 150 + i * 30
    wy = random.randint(20,550)
    traps.append(pg.Rect(wx,wy,30,30)) #鬼データ 
oniimgR = pg.image.load("images/obake.png")
oniimgR = pg.transform.scale(oniimgR,(50,50))
oniimgL = pg.transform.flip(oniimgR,True,False)
enemyrect = pg.Rect(650,100,50,50)
 #障害データ  #boxrect  = pg.Rect(300,200,100,100)
walls = [pg.Rect(0,0,800,20),
         pg.Rect(0,0,20,600),
         pg.Rect(780,0,20,600),
         pg.Rect(0,580,800,20)
        ] #ボタンデータ 
replay_img = pg.image.load("images/replaybtn.png")
rightFlag = True
pushFlag = False
page = 1
 #btmを押したらnewpageにジャンプする 
def button_to_jump(btn, newpage):
    global page, pushFlag
    #ユーザからの入力を調べる 
    mdown = pg.mouse.get_pressed()
    (mx,my) = pg.mouse.get_pos()
    if mdown [0]:
       if btn.collidepoint(mx,my) and pushFlag == False:
        page = newpage
        pushFlag = True
    else:
        pushFlag = False #ずっとループ  #while  True:
def gamestage():
    global rightFlag
    global page
    #画面初期化 
    screen.fill(pg.Color("BLACK"))
    vx= 0
    vy= 0
    #入力情報を調べる 
    key = pg.key.get_pressed()
    #絵をかいたり判定する 
    if(key [pg.K_RIGHT]):
        vx = 5
        rightFlag = True
    if(key [pg.K_LEFT]):
        vx = -5
        rightFlag = False
    if(key [pg.K_UP]):
        vy = -5
    if(key [pg.K_DOWN]):
        vy = 5
    #絵をかいたり判定したり 
    myrect.x = myrect.x + vx
    myrect.y = myrect.y + vy
    if myrect.collidelist(walls) != -1:
       myrect.x = myrect.x - vx
       myrect.y = myrect.y - vy
    if rightFlag:
       screen.blit(imageR,myrect)
    else:
       screen.blit(imageL,myrect)
    #障害物の処理 
    #pg .draw.rect(screen,pg.color("DARKGREEN"),boxrect)
    #壁の処理 
    for wall in walls:
        pg.draw.rect(screen,pg.Color("RED"),wall)
    #罠の処理 
    for trap in traps:
        screen.blit(trapimg,trap)
    if myrect.collidelist(traps) != -1:
        page = 2
    #おばけの処理 
    ovx = 0
    ovy = 0
    if enemyrect.x < myrect.x :
        ovx = 2
    else:
        ovx = -2
    if enemyrect.y < myrect.y :
        ovy = 2
    else:
        ovy = -2
    enemyrect.x = enemyrect.x + ovx
    enemyrect.y = enemyrect.y + ovy
    if ovx > 0:
        screen.blit(oniimgR,enemyrect)
    else:
        screen.blit(oniimgL,enemyrect)
    if myrect.colliderect(enemyrect):
        page = 2
def gamereset():
    myrect.x = 50
    myrect.y = 100
    for d in range(20):
        traps [d].x = 150 + d *30
        traps [d].y = random.randint(20,550)
def gameover():
    gamereset()
    screen.fill(pg.Color("RED"))
    font = pg.font.Font(None,200)
    text = font.render("ONIOVER",True,pg.Color("WHITE"))
    screen.blit(text,(100,200))

    btn1 = screen.blit(replay_img,(320,480))
    button_to_jump(btn1,1) #ずっとループ 
while True:
    if page == 1:
       gamestage()
    if page == 2:
       gameover()

    #画面を表示する 
    pg.display.update()
    pg.time.Clock().tick(60)
    #閉じるボタンが押されたら終了する 
    for event in pg.event.get():
      if event.type == pg.QUIT:
         pg.quit()
         sys.exit()

         

この記事が参加している募集

このデザインが好き

この経験に学べ

この記事が気に入ったらサポートをしてみませんか?