2016-06-26 2 views
3

私は食べるべきヘビのための食糧を作り出す方法を計算することができません。私はライン97と98でヘビの位置を知っています。私は22行目で平和を描きたいピクセルを生成するクラスを作成しました。(EDIT:#def(?)コード)。私がしなければならないことは、ランダムに割り当てられた位置からx座標とy座標に15ピクセルを加えてそれを印刷してブロックを得ることだけです。ヘビのゲームのための食糧を作り出す方法

問題は食べるかどうかを確認することです。それはのようなものでなければなりません:

if x >= x_food && x <= x_food + 15 || y >= y_food && y <= y_food + 15: 
    ...add a point and make snake longer... 

問題は、何らかの理由でそれをすべて一緒に入れます。..いくつかのいずれかが私にヒントを与えるか、私は他の問題を継続することができますので、私はこのクラスを記述する必要がありますどのように解決することはできますか?ありがとうございました!

import pygame 
import random 

#Global variables 
#Color 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 

#Start length of snake 
snake_length = 3 

#Set the width of the segments of the snake 
segment_width = 15 
segment_height = 15 
# Margin within each segment 
segment_margin = 3 

#Set initial speed 
x_change = segment_width + segment_margin 
y_change = 0 

#def (?) 
class Food(): 
    #Class to print food 
    x_food = random.randint(0, 785) 
    y_food = random.randint(0, 585) 


class Segment(pygame.sprite.Sprite): 
    """ Class to represent the segment of the snake. """ 
    # Methods 
    # Constructer function 
    def __init__(self, x, y): 
     #Call the parents constructor 
     super().__init__() 

     #Set height, width 
     self.image = pygame.Surface([segment_width, segment_height]) 
     self.image.fill(WHITE) 

     #Make our top-left corner the passed-in location. 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

#Call this function so the Pygame library can initialize itself 
pygame.init() 

#Create an 800x600 size screen 
screen = pygame.display.set_mode([800, 600]) 

#Set the title of the window 
pygame.display.set_caption("Snake") 

allspriteslist = pygame.sprite.Group() 

#Create an initial snake 
snake_segments = [] 
for i in range(snake_length): 
    x = 250 - (segment_width + segment_margin) * i 
    y = 30 
    segment = Segment(x, y) 
    snake_segments.append(segment) 
    allspriteslist.add(segment) 

clock = pygame.time.Clock() 
done = False 

while not done: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

     #Set the speed based on the key pressed 
     #We want the speed to be enough that we move a full 
     #Segment, plus the margin 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_LEFT: 
       x_change = (segment_width + segment_margin) * -1 
       y_change = 0 
      if event.key == pygame.K_RIGHT: 
       x_change = (segment_width + segment_margin) 
       y_change = 0 
      if event.key == pygame.K_UP: 
       x_change = 0 
       y_change = (segment_height + segment_margin) * -1 
      if event.key == pygame.K_DOWN: 
       x_change = 0 
       y_change = (segment_width + segment_margin) 

    #Get rid of last segment of the snake 
    #.pop() command removes last item in list 
    old_segment = snake_segments.pop() 
    allspriteslist.remove(old_segment) 

    #Figure out where new segment will be 
    x = snake_segments[0].rect.x + x_change 
    y = snake_segments[0].rect.y + y_change 
    segment = Segment(x, y) 

    #Insert new segment to the list 
    snake_segments.insert(0, segment) 
    allspriteslist.add(segment) 

    #Draw 
    #Clear screen 
    screen.fill(BLACK) 

    allspriteslist.draw(screen) 

    #Flip screen 
    pygame.display.flip() 

    #Pause 
    clock.tick(5) 

pygame.quit() 
+0

もしあなたが興味があれば、Salviati、iveはパイゲのヘビを手に入れました。もしあなたがそれを望むなら、私はあなたにコードを送ることができ、それからいくつかのヒントを得ることができます。食品と1つのすべてをまとめるためには、 –

答えて

1

私はあなたのコードを取り、私は何かをうまく考えて、plsは蛇がブロックの上に行くこれだけモニターするとき、それが印刷さことに注意してください:おいしいので、あなたは、詳細を追加する必要があります、また、注意してくださいあなたのクラスを使って食べ物を作りません:

import pygame 
import random 

#Global variables 
#Color 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 

#Start length of snake 
snake_length = 3 

#Set the width of the segments of the snake 
segment_width = 15 
segment_height = 15 
# Margin within each segment 
segment_margin = 3 

#Set initial speed 
x_change = segment_width + segment_margin 
y_change = 0 

#def (?) 
class Food(): 
    #Class to print food 
    x_food = random.randint(0, 785) 
    y_food = random.randint(0, 585) 


class Segment(pygame.sprite.Sprite): 
    """ Class to represent the segment of the snake. """ 
    # Methods 
    # Constructer function 
    def __init__(self, x, y): 
     #Call the parents constructor 
     super().__init__() 

     #Set height, width 
     self.image = pygame.Surface([segment_width, segment_height]) 
     self.image.fill(WHITE) 

     #Make our top-left corner the passed-in location. 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

#Call this function so the Pygame library can initialize itself 
pygame.init() 

#Create an 800x600 size screen 
screen = pygame.display.set_mode([800, 600]) 

#Set the title of the window 
pygame.display.set_caption("Snake") 

allspriteslist = pygame.sprite.Group() 

#Create an initial snake 
snake_segments = [] 
for i in range(snake_length): 
    x = 250 - (segment_width + segment_margin) * i 
    y = 30 
    segment = Segment(x, y) 
    snake_segments.append(segment) 
    allspriteslist.add(segment) 

clock = pygame.time.Clock() 
done = False 
x_food = random.randint(0, 785) 
y_food = random.randint(0, 585) 

while not done: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

     #Set the speed based on the key pressed 
     #We want the speed to be enough that we move a full 
     #Segment, plus the margin 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_LEFT: 
       x_change = (segment_width + segment_margin) * -1 
       y_change = 0 
      if event.key == pygame.K_RIGHT: 
       x_change = (segment_width + segment_margin) 
       y_change = 0 
      if event.key == pygame.K_UP: 
       x_change = 0 
       y_change = (segment_height + segment_margin) * -1 
      if event.key == pygame.K_DOWN: 
       x_change = 0 
       y_change = (segment_width + segment_margin) 

      if y < y_food+30: 
       if x > x_food and x < x_food+30 or x+20 > x_food and x+20<x_food+30: 
        print('yummy') 



    #Get rid of last segment of the snake 
    #.pop() command removes last item in list 
    old_segment = snake_segments.pop() 
    allspriteslist.remove(old_segment) 

    #Figure out where new segment will be 
    x = snake_segments[0].rect.x + x_change 
    y = snake_segments[0].rect.y + y_change 
    segment = Segment(x, y) 

    #Insert new segment to the list 
    snake_segments.insert(0, segment) 
    allspriteslist.add(segment) 

    #Draw 
    #Clear screen 
    screen.fill(BLACK) 
    pygame.draw.rect(screen, WHITE, [x_food, y_food, 30, 30]) 

    allspriteslist.draw(screen) 

    #Flip screen 
    pygame.display.flip() 

    #Pause 
    clock.tick(5) 

pygame.quit() 

希望、これは助けてくれてありがとう!

+1

完璧!今それは、ちょうどいくつかの小さな調整が残って動作します:) Ty! – Salviati

+0

問題ありません、それはあなたのために働いてうれしいSalviati –

1

はどのように使用することについて:

if snake_headx == foodx and snake_heady == foody: 

    food_eated() 
    snake_grow() 

をすべてのウルのコードを読んでいませんでし

しかしちょうど提案、ちょうどuはそれが便利見つけるかもしれないと思いました。

アクチュアリー・アイブが解決策になったので、あなたが望むのは正方形です。その蛇がその正方形に近づくと何か起こるでしょうか?正方形の車を打ったときにあなたに衝突するレースゲームがありますILはちょうどここにコードをコピー:これはあなたの車(または蛇)のxとyを監視

if y < thing_starty+thing_height: 
     if x > thing_startx and x < thing_startx+thing_width or  x+car_width > thing_startx and x+car_width<thing_startx+thing_width: 
      snake_eated() 
      snake_grow() 

とチェックの事(または食品)「SYがあなたの車よりも小さい場合、それは、xのを確認し、他のものがたくさんありますが、基本的には四角形の周りに大きな線ができて、あなたの場合は交差することができません。残りの部分を追加するだけです。

+0

私は今それを試してみましたが、悲しいことに成功しませんでした。正方形は左上隅に印刷されます(座標が得られず、代わりに(0,0)を使用します)、スローになります。私はそれをどうすればいいのか分かりません。 – Salviati

+0

'Food'クラスを使用しようとしているコードのバージョンを表示できますか?質問のコードは、それをまったく使用しようとしない。 – Blckknght

+0

はい家に帰るとクラスを更新します。私は別のテキストファイルにコードをコピーしていましたので、何かを混乱させず、間違ったコードを投稿しました。 – Salviati

1
if x >= x_food && x <= x_food + 15 || y >= y_food && y <= y_food + 15: 

はなぜあなたOR条件のこれらのペアを行いますか?同時に4つのテストが真実である必要はありませんか?

if x >= x_food && x <= x_food + 15 && y >= y_food && y <= y_food + 15: 
+0

彼らはそうである必要はありません、スネークは方向xまたはyからeather来るでしょう。そのうちの1つだけが真実でなければなりません。 – Salviati

+2

'&&'と '||'はPythonでは有効な演算子ではありません。 'と'と 'または'を使用してください。私はここですべてのテストで 'と'をしたいと思う。たとえあなたが右または左に遠く離れていても、あなたはそれと同じ列にあるからといって、食べ物を食べるのには意味がありません。あなたは同じ列と同じ列の両方にいる必要があります。 – Blckknght

+0

はい、私は今朝、それを試してみました。私の心が崩れているはずです。 ||についてそれは間違っていました。解明のためにTy! – Salviati

1

あなたの食べ物に画像として単純な塗りつぶし矩形のスプライトを作成し、スプライトの衝突pygame.sprite.spritecollide()を使用して、あなたのヘビがあなたの食物と衝突するかどうかを確認してください。 Pygameは、あなたのために2つの長方形が重なっているかどうか、実際のロジックを処理します。

また、既にスプライトグループを使用しているので、毎回新しいセグメントを作成する代わりに、スネークセグメントを移動するヘビセグメントの更新機能を作成することをお勧めします。その後、あなたのメインゲームループでallspriteslist.update()と呼ぶだけで、各ヘビセグメントの更新機能を呼び出すことができます。

最後に、pygameウェブサイトの多数のsnake examplesを見てみるとよいでしょう。

関連する問題