为什么新的选择表格会增加?

时间:2017-03-26 16:13:22

标签: ruby-on-rails

import pygame

from pygame.locals import*

#initialise pygame
pygame.init()

WHITE = (255,255,255)

#counts which sprite you should be on when running

#create screen
screen_width = 160
screen_height = 144
screen_multiplier = 4
screen = pygame.display.set_mode(((screen_width*screen_multiplier),(screen_height*screen_multiplier)))
pygame.display.set_caption('Pokemon Blood Red')

#Sprite stuff
sprite = pygame.image.load('player_east_still.png')
#Reform the sprite
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
sprite.set_colorkey(WHITE)

class Player(pygame.sprite.Sprite):

    def __init__(self):
        super(Player, self).__init__()
        self.player_sprite = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
        self.player_sprite.fill(WHITE)
        self.rect = self.player_sprite.get_rect()
        self.rect.x = 160/2
        self.rect.y = 144/2

class Blocks(pygame.sprite.Sprite):

    def __init__(self, width, height):
        super(Blocks, self).__init__()
        block_width = width
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.block_width = width
        self.block_height = height
#######################################################################        
player = Player()
nt_trees_left = Blocks(15*screen_multiplier, 144*screen_multiplier)
nt_trees_left.rect.x = 0*screen_multiplier
nt_trees_left.rect.y = 0*screen_multiplier
screen.blit(nt_trees_left.image, (nt_trees_left.rect.x, nt_trees_left.rect.y))

nt_trees_top = Blocks(160*screen_multiplier, 14*screen_multiplier)
nt_trees_top.rect.x = 33*screen_multiplier
nt_trees_top.rect.y = 0*screen_multiplier
screen.blit(nt_trees_top.image, (nt_trees_top.rect.x, nt_trees_top.rect.y))

laboratory = Blocks(50*screen_multiplier, 46*screen_multiplier)
laboratory.rect.x = 98*screen_multiplier
laboratory.rect.y = 0*screen_multiplier
screen.blit(laboratory.image, (laboratory.rect.x, laboratory.rect.y))

nt_trees_right = Blocks(15*screen_multiplier, 72*screen_multiplier)
nt_trees_right.rect.x = 148*screen_multiplier
nt_trees_right.rect.y = 0*screen_multiplier
screen.blit(nt_trees_right.image, (nt_trees_right.rect.x, nt_trees_right.rect.y))

nt_trees_right_bottom = Blocks(15*screen_multiplier, 65*screen_multiplier)
nt_trees_right_bottom.rect.x = 148*screen_multiplier
nt_trees_right_bottom.rect.y = 91*screen_multiplier
screen.blit(nt_trees_right_bottom.image, (nt_trees_right_bottom.rect.x, nt_trees_right_bottom.rect.y))

nt_trees_bottom = Blocks(160*screen_multiplier, 14*screen_multiplier)
nt_trees_bottom.rect.x = 0*screen_multiplier
nt_trees_bottom.rect.y = 128*screen_multiplier
screen.blit(nt_trees_bottom.image, (nt_trees_bottom.rect.x, nt_trees_bottom.rect.y))

nt_player_house = Blocks(20*screen_multiplier, 22*screen_multiplier)
nt_player_house.rect.x = 38*screen_multiplier
nt_player_house.rect.y = 53*screen_multiplier
screen.blit(nt_player_house.image, (nt_player_house.rect.x, nt_player_house.rect.y))

nt_friend_house = Blocks(20*screen_multiplier, 22*screen_multiplier)
nt_friend_house.rect.x = 70*screen_multiplier
nt_friend_house.rect.y = 53*screen_multiplier
screen.blit(nt_friend_house.image, (nt_friend_house.rect.x, nt_friend_house.rect.y))

################################################################################
class Doors(pygame.sprite.Sprite):

    def __init__(self):
        super(Doors, self).__init__()
        self.player_house_door = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
        self.player_house_door.fill(WHITE)
        self.rect = self.player_house_door.get_rect()
        self.rect.x = 160/2
        self.rect.y = 144/2

player_house_door = Doors()
door_group = pygame.sprite.Group()
door_group.add(player_house_door)
################################################################################
blockades = pygame.sprite.Group()
blockades.add(nt_trees_left)
blockades.add(nt_trees_top)
blockades.add(nt_trees_right)
blockades.add(nt_trees_right_bottom)
blockades.add(laboratory)
blockades.add(nt_trees_bottom)
blockades.add(nt_player_house)
blockades.add(nt_friend_house)

place = 1
def area_load():
    global background
    if place == 1:
        background = pygame.image.load('neuory_town.png').convert()
        background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
        screen.blit(background, (0, 0))
        blockades.add(nt_trees_left)
area_load()





#######################################################################


#Random variables for later use

pygame.init()

amount_caught = 0
place = 1
catch1 = {'pokemon':'none',
          'hp':0,
          'attack':0,
          'defence':0,
          'sp_attack':0,
          'sp_defence':0,}

(x) = 160*0.45
(y) = 144*0.45
def caught():
    if amount_caught == 0:
        pass
        #catch1 values are equal to wild one's

#Mainloop
crashed = False
while not crashed:

    x_change = 0
    y_change = 0

    #Different buttons
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -3*screen_multiplier
                player.rect.x += x_change
                screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                hits = pygame.sprite.spritecollide(player, blockades, False)              
                if hits:
                    while hits:
                        print ('Collision!')
                        x_change = 1*screen_multiplier
                        player.rect.x += x_change
                        hits = pygame.sprite.spritecollide(player, blockades, False)
                sprite = pygame.image.load('player_west_still.png')
            elif event.key == pygame.K_RIGHT:
                x_change = 3*screen_multiplier
                player.rect.x += x_change
                screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                hits = pygame.sprite.spritecollide(player, blockades, False)              
                if hits:
                    while hits:
                        print ('Collision!')
                        x_change = -1*screen_multiplier
                        player.rect.x += x_change
                        hits = pygame.sprite.spritecollide(player, blockades, False)
                sprite = pygame.image.load('player_east_still.png')
            elif event.key == pygame.K_UP:      
                y_change = -3*screen_multiplier
                player.rect.y += y_change
                screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                hits = pygame.sprite.spritecollide(player, blockades, False)              
                if hits:
                    while hits:
                        print ('Collision!')
                        y_change = 1*screen_multiplier
                        player.rect.y += y_change
                        hits = pygame.sprite.spritecollide(player, blockades, False)
                sprite = pygame.image.load('player_north_still.png')
            elif event.key == pygame.K_DOWN:
                y_change = 3*screen_multiplier
                player.rect.y += y_change
                screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
                hits = pygame.sprite.spritecollide(player, blockades, False)              
                if hits:
                    while hits:
                        print ('Collision!')
                        y_change = -1*screen_multiplier
                        player.rect.y += y_change
                        hits = pygame.sprite.spritecollide(player, blockades, False)
                sprite = pygame.image.load('player_south_still.png')  
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x_change = 0
            elif event.key == pygame.K_RIGHT:
                x_change = 0
            elif event.key == pygame.K_UP:
                y_change = 0
            elif event.key == pygame.K_DOWN:
                y_change = 0    
    x += x_change
    y += y_change
    player.rect.x += x_change
    player.rect.y += y_change

    #Check for collisions
    hits = pygame.sprite.spritecollide(player, blockades, False)
    door_enter = pygame.sprite.spritecollide(door_group, player, False)
    if door_enter:
        print ('yaaas')
    #Draw everything
    screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
    screen.blit(background, (0, 0))
    sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
    sprite.set_colorkey(WHITE)
    screen.blit(sprite,(player.rect.x,player.rect.y))
    screen.blit(player_house_door.player_house_door, (player_house_door.rect.x, player_house_door.rect.y))

    pygame.display.flip()
pygame.quit()    

以上是帖子contorller(post_tags是帖子和标签之间的中间表),视图是这样的。

 @post.post_tags.build

当我编辑帖子时,与帖子相关的标签会正确显示,但新的选择框会带有"选择名称"也创造了。我不想要新的选择框,我也不知道为什么选择框被创建。

我该如何解决?

谢谢, 肯

1 个答案:

答案 0 :(得分:0)

使用间接关系时,请不要关注连接模型。 ActiveRecord将为您创建它们。

让我们说:

class Post < ApplicationRecord
  has_many :tags, through: :taggings
end

class Tag < ApplicationRecord
  has_many :posts, through: :taggings
end

class Tagging < ApplicationRecord
  belongs_to :tag
  belongs_to :post
end

这将让我们通过以下方式标记帖子:

@post.tags < @tag

或者通过以下方式创建标签:

@post.tags.create(name: '#yolo')

如果您想创建一个用户可以选择标签的选择标签或复选框,请使用collection helpers

<%= form_for(:post) do |f| %>
  <%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %>
<% end %>

特殊的relation_name_ids setter可以从一组id中动态创建/销毁关联。最好的事情是,当你使用HABTM或has_many through:时,Rails会处理你的加入。