本文整理匯總了Python中game.models.Game.create_minefield方法的典型用法代碼示例。如果您正苦於以下問題:Python Game.create_minefield方法的具體用法?Python Game.create_minefield怎麽用?Python Game.create_minefield使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類game.models.Game
的用法示例。
在下文中一共展示了Game.create_minefield方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: index
# 需要導入模塊: from game.models import Game [as 別名]
# 或者: from game.models.Game import create_minefield [as 別名]
def index(request):
if request.method == 'POST':
post = (request.POST)
form = GameForm(post)
if form.is_valid():
#create the user if they don't exist already
user = User.objects.filter(name=post['name'])[:1]
if not user:
new_user = User(name=post['name'])
new_user.save()
try:
user = User.objects.get(name=post['name'])
except User.DoesNotExist:
return HttpResponse('database error', status=404)
#create the game including minefield then save it to database
if post['difficulty'] == 'beginner':
game = Game(width=9,height=9,number_of_mines=10,difficulty='beginner',user=user)
elif post['difficulty'] == 'intermediate':
game = Game(width=16,height=16,number_of_mines=40,difficulty='intermediate',user=user)
elif post['difficulty'] == 'expert':
game = Game(width=30,height=16,number_of_mines=99,difficulty='expert',user=user)
game.create_minefield()
game.fields_left = game.width * game.height
game.save()
#redirect to the game page
args = {'name': user.name, 'game_id': str(game.id)}
return HttpResponseRedirect(reverse('game_start', kwargs=args))
else:
form = GameForm()
top_beginner_users = User.objects.filter(game__difficulty='beginner', game__won=True).order_by('game__won')
beginner_dict = create_high_scores(top_beginner_users)
top_inter_users = User.objects.filter(game__difficulty='intermediate', game__won=True)
inter_dict = create_high_scores(top_inter_users)
top_expert_users = User.objects.filter(game__difficulty='expert', game__won=True)
expert_dict = create_high_scores(top_expert_users)
return render(request, 'index.html', {
'form': form,
'beginner_dict': beginner_dict,
'inter_dict': inter_dict,
'expert_dict': expert_dict
})