-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_weapon_keyevent.py
118 lines (78 loc) · 3.29 KB
/
2_weapon_keyevent.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import pygame
import os
#################################################
# Basic Initialization (have to)
pygame.init() # Initialize
# Screen Set
screen_width = 640 # 가로
screen_height = 480 # 세로
screen = pygame.display.set_mode((screen_width, screen_height))
# Title
pygame.display.set_caption("Pang") # Game title
# FPS
clock = pygame.time.Clock()
##################################################
# 1. User Game Initialization (BackGround, Images, Graph, Speed, Font etc..)
current_path = os.path.dirname(__file__) # return current file path
image_path = os.path.join(current_path, "Images") # return Images folder file
# background
background = pygame.image.load(os.path.join(image_path, "background.png"))
stage = pygame.image.load(os.path.join(image_path, "stage.png"))
stage_size = stage.get_rect().size
stage_height = stage_size[1]
character = pygame.image.load(os.path.join(image_path, "character.png"))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width - character_width) // 2
character_y_pos = screen_height - character_height - stage_height
weapon = pygame.image.load(os.path.join(image_path, "weapon.png"))
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]
# 무기는 한번에 여러발 발사 가능
weapons = []
weapon_speed = 10
balloon1 = pygame.image.load(os.path.join(image_path, "balloon1.png"))
balloon2 = pygame.image.load(os.path.join(image_path, "balloon2.png"))
balloon3 = pygame.image.load(os.path.join(image_path, "balloon3.png"))
balloon4 = pygame.image.load(os.path.join(image_path, "balloon4.png"))
character_to_x = 0
character_speed = 5
running = True
while running:
dt = clock.tick(60) # Set Screen FPS
# print("FPS : " + str(clock.get_fps()))
# 2. Event Process (keyboard or mouse)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
character_to_x -= character_speed
elif event.key == pygame.K_RIGHT:
character_to_x += character_speed
elif event.key == pygame.K_SPACE:
weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
weapon_y_pos = character_y_pos
weapons.append([weapon_x_pos, weapon_y_pos])
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
character_to_x = 0
# 3. Character Position Def
character_x_pos += character_to_x
if character_x_pos < 0:
character_x_pos = 0
elif character_x_pos > screen_width - character_width:
character_x_pos = screen_width - character_width
# weapon position
weapons = [ [w[0], w[1] - weapon_speed] for w in weapons]
weapons = [ [w[0], w[1]] for w in weapons if w[1] > 0]
# 4. Collision Check
# 5. Print on Screen
screen.blit(background, (0, 0))
for weapon_x_pos, weapon_y_pos in weapons:
screen.blit(weapon, (weapon_x_pos, weapon_y_pos))
screen.blit(character, (character_x_pos, character_y_pos))
screen.blit(stage, (0, screen_height - stage_height))
pygame.display.update()
pygame.quit()