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でゲームを作ってみる2<Spriteクラス編>

Last updated at Posted at 2021-12-30

#はじめに
限界を感じた私はSpriteクラスに手を出すことにした...

#進捗
Spriteクラスを使った処理になった。
###できること
・テキストを表示する
・残り時間をカウントし、表示する
・得点を計算し、表示する
・画像を表示する
・画像上をクリックしたときに「+1」を表示する
・画像上をクリックしたときに1点加える

###できなくなったこと
・画像を一方向に動かす

###コード全文

import pygame
from pygame.locals import *
import sys

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

class MySprite(pygame.sprite.Sprite):
    def __init__(self, filename, x, y, vx, vy):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(filename).convert_alpha()
        width = self.image.get_width()
        height = self.image.get_height()
        self.rect = Rect(x,y,width,height) 
        self.vx = vx
        self.vy = vy
    
    def draw(self, screen):
        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))

    #Spriteを作成
    ob1 = MySprite("Game\dg.jpg",50,100,0,0)
    
    click = 0
    score = 0
    n_frames = 0
    TIME_END = 15
    
    while (1):
        screen.fill((102, 204, 194)) 
        screen.blit(text1, [170, 30])
        
        rest = TIME_END - (n_frames / 30)
        text4 = font3.render('time: {}'.format(round(rest,1)), True, (0, 0, 0))            
        screen.blit(text4,[350,60])

        #Spriteを更新
        ob1.update()

        #Spriteを描画
        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.rect.collidepoint(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

        pygame.display.update()
       
        if n_frames < 30 * TIME_END:
             n_frames += 1

        cl.tick(30)

if __name__ == "__main__":
    main()

#おわりに
Spriteクラスを使って、できることを増やしていく。

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?