我写了简单的蛇游戏。代码在这里:
import pygame
import random
from collections import deque
import time
DISPLAY_WIDTH = 100
DISPLAY_HEIGHT = 100
HEAD_SIZE = 10
FOOD_SIZE = HEAD_SIZE
TAIL_SIZE = 5
BLACK = (0,0,0)
WHITE = (255,255,255)
ONE_MOVE = 5
pygame.init()
gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption("Snake")
clock = pygame.time.Clock()
class Snake:
def __init__(self):
self.x = 0
self.y = 0
self.snake_lenght = 0
self.tail = []
def draw_head(self):
pygame.draw.rect(gameDisplay, BLACK, [self.x, self.y, HEAD_SIZE, HEAD_SIZE])
def draw_tail(self):
self.tail.append([self.x, self.y])
for i in self.tail:
pygame.draw.rect(gameDisplay, BLACK, [int(i[0]), int(i[1]), TAIL_SIZE, TAIL_SIZE])
def rise(self):
foodx = random.randrange(ONE_MOVE, DISPLAY_WIDTH - ONE_MOVE, 5) # random food in a distance from the wall
foody = random.randrange(ONE_MOVE, DISPLAY_HEIGHT - ONE_MOVE, 5)
for i in self.tail:
if foodx == i[0] and foody == i[1]: # avoid food in tail
self.rise()
break
self.snake_lenght += 20
return foodx, foody, self.snake_lenght
def direction_of_movement(self, direction):
if direction == "turn_right":
return [ONE_MOVE, 0, 0, 0]
elif direction == "turn_left":
return [0, ONE_MOVE, 0, 0]
elif direction == "turn_down":
return [0, 0, ONE_MOVE, 0]
elif direction == "turn_up":
return [0, 0, 0, ONE_MOVE]
def crash(self): # restart game after failing
time.sleep(2)
self.game_loop()
def game_loop(self):
self.x = (DISPLAY_WIDTH * 0.5)
self.y = (DISPLAY_HEIGHT * 0.5)
start = 0
self.snake_lenght = 10 # reset snake's lenght after calling game_loop again
self.tail = deque([], maxlen=self.snake_lenght)
while True:
if start == 0: # start to move
speed = self.direction_of_movement("turn_right")
foodx, foody, self.snake_lenght = self.rise()
start += 1
for event in pygame.event.get(): # quit if exit pressed
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN: # arrow navigation
if event.key == pygame.K_RIGHT:
speed = self.direction_of_movement("turn_right")
elif event.key == pygame.K_LEFT:
speed = self.direction_of_movement("turn_left")
elif event.key == pygame.K_DOWN:
speed = self.direction_of_movement("turn_down")
elif event.key == pygame.K_UP:
speed = self.direction_of_movement("turn_up")
# game logic
self.x += speed[0] #moving directions
self.x -= speed[1]
self.y += speed[2]
self.y -= speed[3]
if self.x < 0 or self.x + ONE_MOVE > DISPLAY_WIDTH: # hit into wall
self.crash()
elif self.y < 0 or self.y + ONE_MOVE > DISPLAY_HEIGHT:
self.crash()
for i in self.tail: # bite itself
if self.x == i[0]:
if self.y == i[1]:
self.crash()
if self.x == foodx and self.y == foody:
foodx, foody, self.snake_lenght = self.rise()
self.tail = deque(self.tail, maxlen=self.snake_lenght)
# draw
gameDisplay.fill(WHITE)
self.draw_head()
self.draw_tail()
pygame.draw.rect(gameDisplay, BLACK, [foodx, foody, FOOD_SIZE, FOOD_SIZE])
pygame.display.update()
clock.tick(10) # game speed
snake = Snake()
snake.game_loop()
在另一个文件中,我要导入整个类,添加常量并运行它。在快捷方式中:
from snake_file import Snake
class Snake_for_AI(Snake):
def __init__(self):
pass
def trying(self):
print("hey")
thing = Snake_for_AI()
thing.trying()
这是我的问题。它从不打印“嘿”。此外,当我创建类对象Thing = game_loop
时,它将运行Snake_for_AI
并开始游戏,而无需调用thing.game_loop()
。为什么?
任何代码审查也非常感谢。
答案 0 :(得分:2)
导入时,将在文件底部执行两行。如果那不是您想要的行为,则将它们包围起来:
if __name__ == "__main__":
snake = Snake()
snake.game_loop()
只有在您实际运行该文件的情况下,它们才会被解释。