0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pygameでゲームを作ってみる4<アニメーション編 Part.2>

Last updated at Posted at 2021-12-30

#はじめに
アニメーションと得点システムの改善を目指す旅に出た...

#進捗
###できること
・テキストを表示する
・残り時間をカウントし、表示する
・得点を計算し、表示する
・画像を表示する
・画像上をクリックしたときに「+1」を表示する
・画像上をクリックしたときに1点加える
・画像を上下に動かし続ける 

:sparkles:NEW:sparkles:
・残り時間が0になったときに画像の動きを止める
・プレーリードッグが出現して消える間は1点しか獲得できないようにする

###コード全文


import pygame
from pygame.locals import *
import sys

Scr_rect = Rect(0, 0, 500, 500)

class DogSprite(pygame.sprite.Sprite):
    move_height = 100

    def __init__(self, filename, x, y, vx, vy):
        self.image = pygame.image.load(filename).convert()
        width = self.image.get_width()
        height = self.image.get_height()
        self.rect = Rect(x, y, width, height)
        self.vx = vx
        self.vy = vy
        self.up = self.move_height
        self.down = self.up + self.move_height
        self.enabled = True

    def update(self, rest):
        self.rect.move_ip(0, self.vy)
        if self.rect.top < self.up or self.rect.top > self.down:
            self.vy = -self.vy
        if rest == 0:
            self.vy = 0
    
    def is_clicked(self, pos):
        if self.enabled and self.rect.collidepoint(pos):
            self.enabled = False
            return True

        return False
    
    def reset(self):
        if self.rect.top > self.down:
            self.enabled = True
    
    def draw(self, screen):
        colorkey = self.image.get_at((0,0))
        self.image.set_colorkey(colorkey,RLEACCEL)
        screen.blit(self.image, self.rect)

def main():
    pygame.init()
    cl = pygame.time.Clock()
    screen = pygame.display.set_mode(Scr_rect.size)
    pygame.display.set_caption("Game")

    font = pygame.font.SysFont(None, 78)
    font2 = pygame.font.SysFont(None, 45)
    font3 = pygame.font.SysFont(None, 30)
    text1 = font.render("Click!", True, (204, 102, 112))
    text2 = font2.render("+1", True, (0, 0, 0))
    click = 0
    score = 0
    n_frames = 0
    TIME_END = 10
    line = pygame.image.load("Game\line.png").convert()
    colorkey = line.get_at((0,0))
    line.set_colorkey(colorkey,RLEACCEL)
    #Spriteを作成
    ob1 = DogSprite("Game/dg.jpg", 50, 200, 0, -2)
    
    while (1):
        screen.fill((102, 204, 194))
        screen.blit(text1, [170, 30])
        screen.blit(line,(50, 180))
        rest = TIME_END - (n_frames / 30)
        text4 = font3.render('time: {}'.format(round(rest,1)), True, (0, 0, 0))
        screen.blit(text4,[350,60])
        ob1.update(rest)
        ob1.draw(screen)

        for event in pygame.event.get():
            if event.type == QUIT:
               pygame.quit()
               sys.exit()

            if n_frames < 30 * TIME_END and event.type == MOUSEBUTTONDOWN and event.button == 1:
                if ob1.is_clicked(event.pos):
                    click = 9
                    score += 1

        text3 = font3.render('score : {}'.format(score), True, (0, 0, 0))
        screen.blit(text3, [350, 40] )

        if click > 0:
            pos = pygame.mouse.get_pos()
            screen.blit(text2, pos)
            click -= 1
        
        ob1.reset()

        pygame.display.update()

        if n_frames < 30 * TIME_END:
             n_frames += 1

        cl.tick(30)

if __name__ == "__main__":
    main()

#おわりに
大雑把に形になってきたので嬉しい。(大いなる力をお借りした)(ありがとう!)
空行含めだけど行が100超えてちょっと感動(?)
次回は、画像(プレーリードッグ)をランダムに出現させる...!?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?