本文整理汇总了Python中game.models.Game.fields_left方法的典型用法代码示例。如果您正苦于以下问题:Python Game.fields_left方法的具体用法?Python Game.fields_left怎么用?Python Game.fields_left使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类game.models.Game
的用法示例。
在下文中一共展示了Game.fields_left方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index
# 需要导入模块: from game.models import Game [as 别名]
# 或者: from game.models.Game import fields_left [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
})