質問編集履歴

2

soundのコード追加しました

2018/12/06 03:43

投稿

mee
mee

スコア16

test CHANGED
File without changes
test CHANGED
@@ -16,15 +16,21 @@
16
16
 
17
17
  ```python
18
18
 
19
-   # constants
19
+ import pygame
20
20
 
21
-   SCREENRECT = (1024, 600)
21
+ import os
22
22
 
23
23
 
24
24
 
25
- def load_image(file):
25
+ SCREENRECT = (1024, 600)
26
26
 
27
+ BUTTON_POSITION = (200,200)
28
+
29
+ main_dir = '.'
30
+
31
+
32
+
27
- # loads an image, prepares it for play
33
+ def load_image(file):
28
34
 
29
35
  file = os.path.join(main_dir, 'data', file)
30
36
 
@@ -40,9 +46,39 @@
40
46
 
41
47
 
42
48
 
49
+ class DummySound:
50
+
51
+ def kappa(self): pass
52
+
53
+
54
+
55
+
56
+
57
+ def load_sound(file):
58
+
59
+ if not pygame.mixer:
60
+
61
+ return DummySound()
62
+
63
+ file = os.path.join(main_dir, 'data', file)
64
+
65
+ try:
66
+
67
+ sound = pygame.mixer.Sound(file)
68
+
69
+ return sound
70
+
71
+ except pygame.error:
72
+
73
+ print('Warning, unable to load, %s' % file)
74
+
75
+ return DummySound()
76
+
77
+
78
+
79
+
80
+
43
81
  def main():
44
-
45
- #background
46
82
 
47
83
  pygame.init()
48
84
 
@@ -58,21 +94,31 @@
58
94
 
59
95
  button = load_image("B.png").convert_alpha()
60
96
 
61
- screen.blit(button, (button.get_width() / 2, button.get_height() / 2))
97
+ buttonRect = pygame.Rect(BUTTON_POSITION, button.get_rect().size)
62
98
 
63
- buttonPlace = button.get_rect()
99
+ screen.blit(button, buttonRect)
64
100
 
65
101
  pygame.display.flip()
66
102
 
67
103
 
68
104
 
105
+ running = True
106
+
69
- while True:
107
+ while running:
108
+
109
+
70
110
 
71
111
  for event in pygame.event.get():
72
112
 
73
- if button.event.type == MOUSEBUTTONDOWN and event.button == 1:
113
+ if event.type == pygame.QUIT:
74
114
 
115
+ running = False
116
+
117
+
118
+
119
+ if (event.type == pygame.MOUSEBUTTONDOWN) and (event.button == 1) :
120
+
75
- if event.pos in buttonPlace:
121
+ if buttonRect.collidepoint(event.pos):
76
122
 
77
123
  Csound = load_sound("C{}.wav".format(random.randrange(0, 19)))
78
124
 
@@ -82,6 +128,8 @@
82
128
 
83
129
  print("mekaabu")
84
130
 
85
- pygame.display.update()
131
+ pygame.display.update()
132
+
133
+ pygame.quit()
86
134
 
87
135
  ```

1

コード追加しました

2018/12/06 03:43

投稿

mee
mee

スコア16

test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,81 @@
7
7
 
8
8
 
9
9
  surfaceやdraw、rectなど自分なりに試してはみたのですが上手くいきません。ご教示ください!
10
+
11
+
12
+
13
+ 色々やってぐちゃぐちゃになってますが現状のコード追加してみました。
14
+
15
+ Aの上に、小さめの画像でBを置いてそれをクリックすると音が出る、という感じです。
16
+
17
+ ```python
18
+
19
+   # constants
20
+
21
+   SCREENRECT = (1024, 600)
22
+
23
+
24
+
25
+ def load_image(file):
26
+
27
+ # loads an image, prepares it for play
28
+
29
+ file = os.path.join(main_dir, 'data', file)
30
+
31
+ try:
32
+
33
+ surface = pygame.image.load(file)
34
+
35
+ except pygame.error:
36
+
37
+ raise SystemExit('Could not load image "%s" %s' % (file, pygame.get_error()))
38
+
39
+ return surface.convert()
40
+
41
+
42
+
43
+ def main():
44
+
45
+ #background
46
+
47
+ pygame.init()
48
+
49
+ screen = pygame.display.set_mode(SCREENRECT)
50
+
51
+ bck = load_image('A.jpg')
52
+
53
+ screen.blit(bck, (0, 0))
54
+
55
+
56
+
57
+ # tap button
58
+
59
+ button = load_image("B.png").convert_alpha()
60
+
61
+ screen.blit(button, (button.get_width() / 2, button.get_height() / 2))
62
+
63
+ buttonPlace = button.get_rect()
64
+
65
+ pygame.display.flip()
66
+
67
+
68
+
69
+ while True:
70
+
71
+ for event in pygame.event.get():
72
+
73
+ if button.event.type == MOUSEBUTTONDOWN and event.button == 1:
74
+
75
+ if event.pos in buttonPlace:
76
+
77
+ Csound = load_sound("C{}.wav".format(random.randrange(0, 19)))
78
+
79
+ return Csound.play()
80
+
81
+ else:
82
+
83
+ print("mekaabu")
84
+
85
+ pygame.display.update()
86
+
87
+ ```