teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

soundのコード追加しました

2018/12/06 03:43

投稿

mee
mee

スコア16

title CHANGED
File without changes
body CHANGED
@@ -7,11 +7,14 @@
7
7
  色々やってぐちゃぐちゃになってますが現状のコード追加してみました。
8
8
  Aの上に、小さめの画像でBを置いてそれをクリックすると音が出る、という感じです。
9
9
  ```python
10
+ import pygame
10
-   # constants
11
+ import os
11
-   SCREENRECT = (1024, 600)
12
12
 
13
+ SCREENRECT = (1024, 600)
14
+ BUTTON_POSITION = (200,200)
15
+ main_dir = '.'
16
+
13
- def load_image(file):
17
+ def load_image(file):
14
- # loads an image, prepares it for play
15
18
  file = os.path.join(main_dir, 'data', file)
16
19
  try:
17
20
  surface = pygame.image.load(file)
@@ -19,8 +22,23 @@
19
22
  raise SystemExit('Could not load image "%s" %s' % (file, pygame.get_error()))
20
23
  return surface.convert()
21
24
 
25
+ class DummySound:
26
+ def kappa(self): pass
27
+
28
+
29
+ def load_sound(file):
30
+ if not pygame.mixer:
31
+ return DummySound()
32
+ file = os.path.join(main_dir, 'data', file)
33
+ try:
34
+ sound = pygame.mixer.Sound(file)
35
+ return sound
36
+ except pygame.error:
37
+ print('Warning, unable to load, %s' % file)
38
+ return DummySound()
39
+
40
+
22
41
  def main():
23
- #background
24
42
  pygame.init()
25
43
  screen = pygame.display.set_mode(SCREENRECT)
26
44
  bck = load_image('A.jpg')
@@ -28,17 +46,23 @@
28
46
 
29
47
  # tap button
30
48
  button = load_image("B.png").convert_alpha()
31
- screen.blit(button, (button.get_width() / 2, button.get_height() / 2))
49
+ buttonRect = pygame.Rect(BUTTON_POSITION, button.get_rect().size)
32
- buttonPlace = button.get_rect()
50
+ screen.blit(button, buttonRect)
33
51
  pygame.display.flip()
34
52
 
53
+ running = True
35
- while True:
54
+ while running:
55
+
36
56
  for event in pygame.event.get():
57
+ if event.type == pygame.QUIT:
58
+ running = False
59
+
37
- if button.event.type == MOUSEBUTTONDOWN and event.button == 1:
60
+ if (event.type == pygame.MOUSEBUTTONDOWN) and (event.button == 1) :
38
- if event.pos in buttonPlace:
61
+ if buttonRect.collidepoint(event.pos):
39
62
  Csound = load_sound("C{}.wav".format(random.randrange(0, 19)))
40
63
  return Csound.play()
41
64
  else:
42
65
  print("mekaabu")
43
- pygame.display.update()
66
+ pygame.display.update()
67
+ pygame.quit()
44
68
  ```

1

コード追加しました

2018/12/06 03:43

投稿

mee
mee

スコア16

title CHANGED
File without changes
body CHANGED
@@ -2,4 +2,43 @@
2
2
 
3
3
  背景画像の上に置いた画像の範囲をクリックすると反応が返ってくる、という風にするにはどうすればいいでしょうか。
4
4
 
5
- surfaceやdraw、rectなど自分なりに試してはみたのですが上手くいきません。ご教示ください!
5
+ surfaceやdraw、rectなど自分なりに試してはみたのですが上手くいきません。ご教示ください!
6
+
7
+ 色々やってぐちゃぐちゃになってますが現状のコード追加してみました。
8
+ Aの上に、小さめの画像でBを置いてそれをクリックすると音が出る、という感じです。
9
+ ```python
10
+   # constants
11
+   SCREENRECT = (1024, 600)
12
+
13
+ def load_image(file):
14
+ # loads an image, prepares it for play
15
+ file = os.path.join(main_dir, 'data', file)
16
+ try:
17
+ surface = pygame.image.load(file)
18
+ except pygame.error:
19
+ raise SystemExit('Could not load image "%s" %s' % (file, pygame.get_error()))
20
+ return surface.convert()
21
+
22
+ def main():
23
+ #background
24
+ pygame.init()
25
+ screen = pygame.display.set_mode(SCREENRECT)
26
+ bck = load_image('A.jpg')
27
+ screen.blit(bck, (0, 0))
28
+
29
+ # tap button
30
+ button = load_image("B.png").convert_alpha()
31
+ screen.blit(button, (button.get_width() / 2, button.get_height() / 2))
32
+ buttonPlace = button.get_rect()
33
+ pygame.display.flip()
34
+
35
+ while True:
36
+ for event in pygame.event.get():
37
+ if button.event.type == MOUSEBUTTONDOWN and event.button == 1:
38
+ if event.pos in buttonPlace:
39
+ Csound = load_sound("C{}.wav".format(random.randrange(0, 19)))
40
+ return Csound.play()
41
+ else:
42
+ print("mekaabu")
43
+ pygame.display.update()
44
+ ```