0
0

More than 1 year has passed since last update.

pygameでゲームを作ってみる1

Last updated at Posted at 2021-12-30

はじめに

Pythonの基本的な文法を学んだところで、何かを作ってみたいと思い、3月5日からpygameでもぐらたたきのようなゲーム(もぐらではなくプレーリードッグ)を作り始めた。
(プレーリードッグが巣穴に住むかわいい姿を見たときに思いついた。)
せっかくなので、作成過程を記録していきたいと思う。
です・ます調が柔らかくて好きだけど、サクッと書きやすいし、なんか恥ずかしいから、だ・である調にしてみたり。(たりって2回使わないといけないよね~と思いながらレポートじゃないときは間違った方法で使ってしまう)

進捗

できること

・テキストを表示する
・残り時間をカウントし、表示する
・得点を計算し、表示する
・画像を表示する
・画像を一方向に動かす
・画像上をクリックしたときに「+1」を表示する
・画像上をクリックしたときに1点加える

コード全文

Game.py
import pygame
from pygame.locals import *
import sys
from pygame.time import Clock

def main():
    pygame.init()
    (w,h) = (500, 500)
    (x,y) = (w/2, h/2)
    pygame.display.set_mode((w,h), 0, 32)
    cl = pygame.time.Clock()
    screen = pygame.display.get_surface()
    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))
    ob1 = pygame.image.load("Game\dg.jpg")
    rect_ob1 = ob1.get_rect() 
    rect_ob1.center = (x, y)
    a = 50
    b = 200
    c = 0
    score = 0
    n_frames = 0
    TIME_END = 15

    while (1):
        screen.fill((102, 204, 194)) 
        screen.blit(text1, [170, 30])
        screen.blit(ob1, (a, b))
        ob1Rect = pygame.Rect((a, b), ob1.get_rect().size)

        rest = TIME_END - (n_frames / 30)
        text4 = font3.render('time: {}'.format(round(rest,1)), True, (0, 0, 0))            
        screen.blit(text4,[350,60])

        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 ob1Rect.collidepoint(event.pos):
                    c = 9
                    score += 1
        text3 = font3.render('score : {}'.format(score), True, (0, 0, 0))
        screen.blit(text3, [350, 40] )

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

        pygame.display.update()

        if n_frames < 30 * TIME_END:
             n_frames += 1

        b -= 2

        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