-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
大川猫を表示するようにした😸 タイマーを実装した
- Loading branch information
Showing
11 changed files
with
293 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
PumpkinSmash/Assets.xcassets/Ookawa_Pumpkin.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "Ookawa_Pumpkin.png", | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+28 KB
PumpkinSmash/Assets.xcassets/Ookawa_Pumpkin.imageset/Ookawa_Pumpkin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
PumpkinSmash/Assets.xcassets/TapSound.dataset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"data" : [ | ||
{ | ||
"filename" : "TapSound.m4a", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// | ||
// GameFieldView.swift | ||
// PumpkinSmash | ||
// | ||
// Created by cranoo3 on 2023/10/05. | ||
// | ||
|
||
import SwiftUI | ||
import AVFoundation | ||
|
||
struct GameFieldView: View { | ||
// グリッドの設定 | ||
let grids = Array(repeating: GridItem(.fixed(UIScreen.main.bounds.width / 5 - 10)), count: 5) | ||
// difficulty → 難易度。0: 簡単, 1: 普通 2: 難しい | ||
// TODO: Bindingしよう | ||
let difficulty: Int = 2 // switchケースに使用する | ||
var showHole: [Int] { | ||
// 難易度によって表示する穴の数を変更する | ||
switch difficulty { | ||
case 0: | ||
return [2, 6, 8, 10, 12, 14, 16, 18, 22] | ||
|
||
case 1: | ||
return [2, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 22] | ||
|
||
case 2: | ||
return Array(0...24) | ||
|
||
default: | ||
return [] | ||
} | ||
} | ||
// ランダムでボタンを表示する | ||
@State var buttonPosition = 0 | ||
// ゲームがスタートしているか | ||
@State var isGameStarted = false | ||
// タイマーを作成 | ||
@StateObject var timerManager = TimerManager() | ||
|
||
// タップサウンド | ||
let tapSound = try! AVAudioPlayer(data: NSDataAsset(name: "TapSound")!.data) | ||
func playSound() { | ||
// 連打した時に音が重ならないようにする | ||
tapSound.stop() | ||
tapSound.currentTime = 0 | ||
tapSound.play() | ||
} | ||
var body: some View { | ||
ZStack { | ||
//TODO: カスタム画像を作成(今週締め切り) | ||
// タブメニューとグリッド | ||
VStack { | ||
// メニューバー | ||
HStack { | ||
// メニューボタン | ||
Image(systemName: "slider.horizontal.3") | ||
.opacity(0.0) | ||
|
||
Spacer() | ||
|
||
// タイマー | ||
|
||
Text("残り\(timerManager.secondsLeft)秒") // TODO: あとで適したものに変更しよう | ||
.font((.custom("Kiwi Maru", size: 60))) | ||
|
||
Spacer() | ||
|
||
// ポーズボタン | ||
Button(action: {}) { | ||
Image(systemName: "pause.fill") | ||
} | ||
} | ||
.padding() | ||
|
||
Spacer() | ||
|
||
// ゲーム画面 | ||
ZStack { | ||
// ゲーム画面(穴の表示) | ||
LazyVGrid(columns: grids) { | ||
ForEach ((0...24), id: \.self) { num in | ||
if showHole.contains(num) { | ||
Hole() | ||
} else { | ||
DummyHole() | ||
} | ||
} | ||
} | ||
// ボタン(ランダム) | ||
LazyVGrid(columns: grids) { | ||
ForEach ((0...24), id: \.self) { num in | ||
if buttonPosition == num { | ||
Button(action: { | ||
playSound() | ||
buttonPosition = showHole.shuffled()[0] | ||
print("Button position is \(buttonPosition)") | ||
}) { | ||
Image("Ookawa_Pumpkin") | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
.offset(y: -15) | ||
} | ||
.opacity(isGameStarted ? 1.0 : 0.0) | ||
} else { | ||
DummyHole() | ||
} | ||
} | ||
} | ||
|
||
// TODO: ボタンデザインを変更しよう | ||
if !isGameStarted { | ||
Button(action: { | ||
isGameStarted = true // ゲームを開始しているか | ||
buttonPosition = showHole.shuffled()[0] // ボタンの場所をシャッフルする | ||
timerManager.start() // タイマーをスタート | ||
}) { | ||
ZStack { | ||
RoundedRectangle(cornerRadius: 25.0) | ||
.frame(maxWidth: .infinity, maxHeight: 90) | ||
Text("Start") | ||
.font(.title) | ||
.fontWeight(.bold) | ||
.foregroundStyle(.white) | ||
} | ||
.padding() | ||
} | ||
} | ||
} | ||
.padding(.bottom, 30) | ||
} | ||
} | ||
.fullScreenCover(isPresented: $timerManager.isTimerStoped, content: { | ||
ScoreResultsView() | ||
}) | ||
} | ||
} | ||
|
||
struct Hole: View { | ||
var body: some View { | ||
ZStack { | ||
Image("Hole") | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
.frame(width: 80, height: 90) | ||
} | ||
} | ||
} | ||
|
||
struct DummyHole: View { | ||
var body: some View { | ||
ZStack { | ||
Image("Hole") | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
.opacity(0.0) | ||
.frame(width: 80, height: 90) | ||
} | ||
} | ||
} | ||
|
||
struct GameGieldView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
GameFieldView() | ||
// Hole() | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// Helper.swift | ||
// PumpkinSmash | ||
// | ||
// Created by cranoo3 on 2023/10/13. | ||
// | ||
|
||
import Foundation | ||
|
||
enum TimerMode { | ||
case running | ||
case paused | ||
case initial | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// TimerManager.swift | ||
// PumpkinSmash | ||
// | ||
// Created by cranoo3 on 2023/10/13. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
class TimerManager: ObservableObject { | ||
@Published var timerMode: TimerMode = .initial | ||
@Published var secondsLeft = 10 | ||
@Published var isTimerStoped = false | ||
var timer = Timer() | ||
|
||
func start() { | ||
timerMode = .running | ||
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in | ||
self.secondsLeft -= 1 | ||
if self.secondsLeft == 0 { | ||
self.timerMode = .initial | ||
self.secondsLeft = 0 | ||
self.isTimerStoped = true | ||
timer.invalidate() | ||
} | ||
}) | ||
} | ||
|
||
func pause() { | ||
timerMode = .paused | ||
timer.invalidate() | ||
} | ||
|
||
func reset() { | ||
timerMode = .initial | ||
self.secondsLeft = 10 | ||
timer.invalidate() | ||
} | ||
} |
Oops, something went wrong.