<토이프로젝트>/[Python 프로젝트]

[python 게임] 파이썬 공룡 게임 만들기

BlockDMask 2020. 9. 4. 00:20
반응형

안녕하세요. BlockDMask 입니다.
오늘은 파이썬을 이용해서 구글 공룡게임을 만들어 보았습니다.
제 채널에서 보셨겠지만 C++ SFML 로 만들었던 구글 공룡 게임을 언어 파이썬 + 파이게임으로 바꿔서 만들어봤습니다.

파이게임은 저도 처음 접해봐서, 부족한 부분이 많습니다. 감안하셔서 코드를 봐주시면 감사하겠습니다.

<목차>

1. 게임 영상
2. 게임 소스 코드


1. 파이썬으로 만든 구글 공룡 게임 영상

영상 주소 https://youtu.be/ok_8mvQ8CiY

2. 파이게임으로 만든 구글 공룡 게임 소스 코드

소스코드 

코드 하단에 깃 주소가 존재합니다.
해당 주소에 소스코드와 이미지도 같이 존재하니 다운받으셔서 실습 해 보시길 추천드립니다.
단, 어딘가에서 사용하실때는 꼭 출처를 남겨주시면 감사하겠습니다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# python game with pygame : Jumping dino
# by. BlockDMask
import pygame
import sys
 
# step1 : set screen, fps
# step2 : show dino, jump dino
# step3 : show tree, move tree
 
pygame.init()
pygame.display.set_caption('Jumping dino')
MAX_WIDTH = 800
MAX_HEIGHT = 400
 
 
def main():
    # set screen, fps
    screen = pygame.display.set_mode((MAX_WIDTH, MAX_HEIGHT))
    fps = pygame.time.Clock()
 
    # dino
    imgDino1 = pygame.image.load('images/dino1.png')
    imgDino2 = pygame.image.load('images/dino2.png')
    dino_height = imgDino1.get_size()[1]
    dino_bottom = MAX_HEIGHT - dino_height
    dino_x = 50
    dino_y = dino_bottom
    jump_top = 200
    leg_swap = True
    is_bottom = True
    is_go_up = False
 
    # tree
    imgTree = pygame.image.load('images/tree.png')
    tree_height = imgTree.get_size()[1]
    tree_x = MAX_WIDTH
    tree_y = MAX_HEIGHT - tree_height
 
    while True:
        screen.fill((255255255))
 
        # event check
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if is_bottom:
                    is_go_up = True
                    is_bottom = False
 
        # dino move
        if is_go_up:
            dino_y -= 10.0
        elif not is_go_up and not is_bottom:
            dino_y += 10.0
 
        # dino top and bottom check
        if is_go_up and dino_y <= jump_top:
            is_go_up = False
 
        if not is_bottom and dino_y >= dino_bottom:
            is_bottom = True
            dino_y = dino_bottom
 
        # tree move
        tree_x -= 12.0
        if tree_x <= 0:
            tree_x = MAX_WIDTH
 
        # draw tree
        screen.blit(imgTree, (tree_x, tree_y))
 
        # draw dino
        if leg_swap:
            screen.blit(imgDino1, (dino_x, dino_y))
            leg_swap = False
        else:
            screen.blit(imgDino2, (dino_x, dino_y))
            leg_swap = True
 
        # update
        pygame.display.update()
        fps.tick(30)
 
 
if __name__ == '__main__':
    main()
 
cs


깃 주소 : https://github.com/BlockDMask/Python_dinosaur_game

감사합니다. 이상으로 파이썬으로 구글 공룡 게임 만들기 포스팅을 마치겠습니다.

반응형