我试图每秒将Double变量减少0.05以加速函数spawnEnemy(),但我找不到一种效果很好的方法。所以,我试图在更短的时间内“生成”更多的敌人(UIButtons)。任何帮助都会很棒,非常感谢。谢谢!我下面的代码编译得很好,但没有加速控制函数spawnEnemy()
的NSTimerimport UIKit
class ViewController: UIViewController {
@IBOutlet weak var scoreLabel: UILabel!
var points: Int = 0
func randomPoint() -> CGPoint {
let randomPoint: CGPoint = CGPoint(x:CGFloat(arc4random()%320),y:CGFloat(568-arc4random()%390))
return randomPoint
}
func randomColor() -> UIColor {
let red = CGFloat(drand48())
let green = CGFloat(drand48())
let blue = CGFloat(drand48())
return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}
func spawnEnemy() {
let enemy: UIButton = UIButton(frame: CGRect(x: 160, y: 160, width: 100, height: 100))
enemy.backgroundColor = randomColor()
enemy.center = randomPoint()
enemy.addTarget(self, action: Selector("buttonPushed:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(enemy)
}
func buttonPushed(sender : UIButton) {
if sender.frame.height < 50 || sender.frame.width < 50 {
sender.frame = CGRectMake(sender.frame.origin.x, sender.frame.origin.y, 50, 50)
sender.backgroundColor = randomColor()
sender.center = randomPoint()
return
}
sender.backgroundColor = UIColor.blackColor()
points = points + 1
scoreLabel.textAlignment = .Center
scoreLabel.text = "\(points)"
scoreLabel.backgroundColor = UIColor.blackColor()
}
func decreaseDouble() -> Double {
var num: Double = 2.0
num = num - 0.05
return num
}
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(decreaseDouble(), target: self, selector: Selector("spawnEnemy"), userInfo: nil, repeats: true)
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("decreaseDouble"), userInfo: nil, repeats: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
以下是我的新代码。任何决赛的事情还是应该有效?我还添加了一个声明,所以你只能在敌人不是黑色时获得积分,否则你可以一直点击生成敌人的地方(但被点击)并继续获得积分。但是,现在当我运行应用程序时,已经产生了两个敌人,但我仍然可以点击它们(只允许获得两个点)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scoreLabel: UILabel!
var points: Int = 0
func randomPoint() -> CGPoint {
let randomPoint: CGPoint = CGPoint(x:CGFloat(arc4random()%320),y:CGFloat(568-arc4random()%390))
return randomPoint
}
func randomColor() -> UIColor {
let red = CGFloat(drand48())
let green = CGFloat(drand48())
let blue = CGFloat(drand48())
return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}
func spawnEnemy() {
let enemy: UIButton = UIButton(frame: CGRect(x: 160, y: 160, width: 100, height: 100))
enemy.backgroundColor = randomColor()
enemy.center = randomPoint()
enemy.addTarget(self, action: Selector("buttonPushed:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(enemy)
}
func buttonPushed(sender : UIButton) {
if sender.frame.height < 50 || sender.frame.width < 50 {
sender.frame = CGRectMake(sender.frame.origin.x, sender.frame.origin.y, 50, 50)
sender.backgroundColor = randomColor()
sender.center = randomPoint()
return
}
while sender.backgroundColor != UIColor.blackColor() {
points = points + 1
scoreLabel.textAlignment = .Center
scoreLabel.text = "\(points)"
scoreLabel.backgroundColor = UIColor.blackColor()
sender.backgroundColor = UIColor.blackColor()
}
delayTime -= 0.05
}
var delayTime: Double = 2.0
override func viewDidLoad() {
super.viewDidLoad()
delayTime = 2.0
NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("spawnEnemy"), userInfo: nil, repeats: false)
NSTimer.scheduledTimerWithTimeInterval(delayTime, target: self, selector: Selector("spawnEnemy"), userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
答案 0 :(得分:2)
一旦启动,重复NSTimers
始终会在同一时间间隔内触发。
如果你想使用递减间隔而不是重复计时器,我建议使用单“一次性”计时器(不是2个计时器)。使用repeats:false
创建计时器。然后在您的计时器方法(spawnEnemy
,在您的情况下)减少您的时间间隔并使用它创建一个新的计时器(也使用repeats:false
)使用新的,更短的间隔并调用相同的方法。 / p>
您的decreaseDouble函数无法按写入方式工作。它将始终返回相同的值2.0 - 0.05,因为每次调用该函数时都将num设置为2.0。而是摆脱了reduceDouble函数。创建一个实例变量delayValue
并在viewDidLoad中为其赋值(例如2.0)。然后每次调用spawnEnemy时从它中减去0.05,产生一个新的敌人,并在delayValue
没有变得太小的情况下创建一个新的计时器。 (当delayValue
变得非常小时,你将每秒产生10个敌人。最终delayValue
将变为零,并且计时器间隔不应该<= 0.0。
答案 1 :(得分:0)
你有正确的想法。我会使用重复计时器作为&#39; tick&#39;然后保留两个属性 - 一个是滴答计数,另一个是生成新敌人之前的滴答计数。通过减少后者,你可以加快产卵速度。
像这样的东西
##21 Card Game
##Wattel
##2016 Spring
from random import *
import random
from random import shuffle, randint
CARDPOINTS = {'AD': 11,'AH': 11,'AC': 11,'AS': 11,
'KD': 10,'KH': 10,'KC': 10,'KS':10,
'QD': 10,'QH': 10,'QC': 10,'QS': 10,
'JD': 10,'JH': 10,'JC': 10,'JS': 10,
'10D': 10,'10H': 10,'10C': 10,'10S': 10,
'9D': 9,'9H': 9,'9C': 9,'9S': 9,
'8D': 8,'8H': 8,'8C': 8,'8S': 8,
'7D': 7,'7H': 7,'7C': 7,'7S': 7,
'6D': 6,'6H': 6,'6C': 6,'6S': 6,
'5D': 5,'5H': 5,'5C': 5,'5S': 5,
'4D': 4,'4H': 4,'4C': 4,'4S': 4,
'3D': 3,'3H': 3,'3C': 3,'3S': 3,
'2D': 2,'2H': 2,'2C': 2,'2S': 2,}
List = ['AD','AH','AC','AS',
'KD','KH','KC','KS',
'QD','QH','QC','QS',
'JD','JH','JC','JS',
'10D','10H','10C','10S',
'9D','9H','9C','9S',
'8D','8H','8C','8S',
'7D','7H','7C','7S',
'6D','6H','6C','6S',
'5D','5H','5C','5S',
'4D','4H','4C','4S',
'3D','3H','3C','3S',
'2D','2H','2C','2S',]
def want2play():
Hit2Play = input(" Hit 'y' to begin your game of 21): ")
print ("")
return Hit2Play
def deckCheck(DECK):
print (DECK)
print("There are:",len(DECK),"cards in the deck.")
if len(DECK) < 20:
DECK = shuffleDeck()
return DECK
else:
return DECK
def openingDeal(DECK):
PHAND = []
DHAND = []
DHANDT = []
PHAND, DECK = dealOneCard(PHAND, DECK)
DHAND, DECK = dealOneCard(DHAND, DECK)
DHANDT.append(DHAND[0])
PHAND, DECK = dealOneCard(PHAND, DECK)
DHAND, DECK = dealOneCard(DHAND, DECK)
DHANDT.append("back of card")
PPOINTS = scoreHAND(PHAND)
printScores_0(PPOINTS, PHAND, DHANDT)
return PHAND, DHAND, DHANDT, DECK
def pLoop(PHAND, DHAND, DHANDT, DECK):
while scoreHAND(PHAND) < 21:
hitorhold = input('Do you want to hit or hold?: ')
if hitorhold == 'hit':
dealOneCard(PHAND, DECK)
printScores_0(scoreHAND(PHAND), PHAND, DHANDT)
elif hitorhold == 'hold':
print('Player holds')
printScores_0(scoreHAND(PHAND), PHAND, DHANDT)
break
return PHAND, DHAND, DECK
def dLoop(PHAND, DHAND, DECK):
printScores_1(scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND)
if scoreHAND(DHAND) < 18 or scoreHAND(DHAND) < 21 or scoreHAND(DHAND) < scoreHAND(PHAND) or scoreHAND(DHAND) < 21 or scoreHAND(PHAND) < 22:
dealOneCard(DHAND, DECK)
printScores_1(scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND)
return scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND
else:
return scoreHAND(PHAND), scoreHAND(DHAND), PHAND, DHAND
def checkScore(pWin, dWin, PPOINTS, DPOINTS):
if DPOINTS > PPOINTS and DPOINTS < 22:
dWin += 1
print (' ')
print ("Dealer Win")
return pWin, dWin
elif PPOINTS == DPOINTS and PPOINTS < 21:
dWin += 1
print (' ')
print ("Dealer Win")
return pWin, dWin
elif PPOINTS > 21 and DPOINTS < 21:
dWin += 1
print(' ')
print("Dealer Win")
return pWin, dWin
elif PPOINTS < 22 and DPOINTS > 21:
pWin += 1
print (' ')
print ("Player Win")
return pWin, dWin
elif PPOINTS > 21 and DPOINTS > 21:
print (' ')
print ("Tie.")
return pWin, dWin
elif PPOINTS == 21 and DPOINTS == 21:
print(' ')
print("Tie.")
return pWin, dWin
def dealOneCard(HAND,DECK):
theCard = DECK.pop(0)
HAND.append(theCard)
return HAND, DECK
def shuffleDeck():
CardPile = len(List)
random.shuffle(List)
return List
def scoreHAND(HAND):
points = addScore(HAND)
aceCount=0
if points > 21:
aceCount += HAND.count('AS')
aceCount += HAND.count('AH')
aceCount += HAND.count('AC')
aceCount += HAND.count('AD')
while points > 21 and aceCount > 0:
points -= 10
aceCount -= 1
return points
else:
return points
def addScore(HAND):
tempScore = 0
for i in HAND:
tempScore += CARDPOINTS[i]
return tempScore
def printScores_0(POINTS, HAND, HAND1):
print("Player's cards: ", HAND, "Player's hand score: ", POINTS)
print("Dealer's cards: ", HAND1)
def printScores_1(PPOINTS, DPOINTS, PHAND, DHAND):
print("Player's cards: ", PHAND, "Player's hand score: ", PPOINTS)
print("Dealer's cards: ", DHAND, "Dealer's hand score: ", DPOINTS)
def main():
DECK = shuffleDeck()
pWin = 0
dWin = 0
while True:
Hit2Play = want2play()
if Hit2Play == 'y':
DECK = deckCheck(DECK)
PHAND, DHAND, DHANDT, DECK = openingDeal(DECK)
PHAND, DHAND, DECK = pLoop(PHAND, DHAND, DHANDT, DECK)
PPOINTS, DPOINTS, PHAND, DHAND = dLoop(PHAND, DHAND, DECK)
pWin, dWin = checkScore(pWin, dWin, PPOINTS, DPOINTS)
print("")
print("Player's Wins:", pWin, "Dealer's Wins:",dWin)
print("")
else:
print("Player's Wins:", pWin, "Dealer's Wins:",dWin)
break
main()
您可以根据需要修改滴答率和滴答减量,以便为您提供
后的范围