Ich kann nicht herausfinden, wie man das Essen für die Schlange zu essen erzeugt. Ich kenne die Position der Schlange in Zeile 97 und 98, ich habe eine Klasse erstellt, um ein Pixel zu erzeugen, wo ich in der Zeile 22 ein Stück Essen zeichnen möchte (EDIT: sollte wohl eine Funktion sein, kommentierte #def (?) In) der Code). Ich muss lediglich 15 Pixel an den X- und Y-Koordinaten von der zufällig zugewiesenen Position hinzufügen und sie ausdrucken, um einen Block zu erhalten.Wie erzeugt man das Essen für ein Schlangenspiel
Das Problem ist zu überprüfen, ob ich es esse oder nicht. Es sollte so etwas sein:
if x >= x_food && x <= x_food + 15 || y >= y_food && y <= y_food + 15:
...add a point and make snake longer...
Das Problem, sie alle zusammen aus irgendeinem Grunde setzt .. Kann jemand mir einen Tip geben oder lösen, wie ich diese Klasse schreiben sollte, so kann ich mit anderen Problemen weitergehen? Vielen Dank!
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()
Wenn Sie interessiert sind Salviati, ich habe eine Pygame Schlange, kann ich Ihnen den Code senden, wenn Sie es möchten, und Sie können vielleicht ein paar Tipps davon erhalten, der Code ist actuely 3 Programme, 1 für die Schlange, 1 für das Essen und 1 für alles zusammen, –