我是python和pygame的新手,并尝试在稍微调整它的同时复制程序。但是,我在运行时只会出现黑屏。
我尝试使用PyCharm解释器将其作为python 2.7运行。由于Movaja OSX och python 3似乎存在问题。
import pygame
import random
import time
import numpy as np
def display_snake(snake_pos, display):
for position in snake_pos:
pygame.draw.rect(display, (255,255,0), pygame.Rect(position[0], position[1], 10, 10))
def display_apple(apple_pos, display):
pygame.draw.rect(display, (0,255,0), pygame.Rect(apple_pos[0],apple_pos[1],10,10))
def starting_pos():
snake_head = [100,100]
snake_pos = [[100,100], [90,100], [80,100]]
apple_pos = [random.randrange(1, 50) * 10, random.randrange(1, 50) * 10]
score = 3
return snake_head, snake_pos, apple_pos, score
def collision_apple(apple_pos, score):
apple_pos = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return apple_pos, score
def collision_boundaries(snake_head):
if snake_head[0]>=500 or snake_head[0]<0 or snake_head[1]>=500 or snake_head[1]<0:
return 1
else:
return 0
def collision_self(snake_pos):
snake_head = snake_pos[0]
if snake_head in snake_pos[1:]:
return 1
else:
return 0
def is_dir_blocked(snake_pos, current_dir_vector):
next_step = snake_pos[0] + current_dir_vector
snake_head = snake_pos[0]
if collision_boundaries(snake_head) == 1 or collision_self(snake_pos) == 1:
return 1
else:
return 0
def gen_snake(snake_head, snake_pos, apple_pos, button_dir, score):
if button_dir == 1:
snake_head[0] += 10
elif button_dir == 0:
snake_head[0] -= 10
elif button_dir == 2:
snake_head[1] += 10
elif button_dir == 3:
snake_head[1] -= 10
else:
pass
if snake_head == apple_pos:
apple_pos, score = collision_apple(apple_pos, score)
snake_pos.insert(0,list(snake_head))
else:
snake_pos.insert(0,list(snake_head))
snake_pos.pop()
return snake_pos, apple_pos, score
def play_game(snake_head, snake_pos, apple_pos, button_dir, score):
crashed = False
prev_button_dir = button_dir
button_dir = button_dir
current_dir_vector = np.array(snake_pos[0])-np.array(snake_pos[1])
while crashed is not True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and prev_button_dir != 1:
button_dir = 0
elif event.key == pygame.K_RIGHT and prev_button_dir != 0:
button_dir = 1
elif event.key == pygame.K_UP and prev_button_dir != 2:
button_dir = 3
elif event.key == pygame.K_DOWN and prev_button_dir != 3:
button_dir = 2
else:
button_dir = button_dir
display.fill(window_color)
display_snake(snake_pos,display)
display_apple(apple_pos,display)
snake_pos, apple_pos, score = gen_snake(snake_head, snake_pos, apple_pos, button_dir, score)
pygame.display.set_caption("Snake game"+" "+"SCORE:"+str(score))
pygame.display.update()
prev_button_dir = button_dir
if is_dir_blocked(snake_pos, current_dir_vector) == 1:
crashed = True
clock.tick(15)
return score
def display_final_score(display_text):
largeText = pygame.font.Font('freesansbold.ttf',35)
TextSurf = largeText.render(display_text, True, (0,0,0))
TextRect = TextSurf.get_rect()
TextRect.center = ((display_width/2), (display_height/2))
display.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
if __name__ == "__main__":
display_width = 500
window_color = (200,0,0)
display_height = 500
clock = pygame.time.Clock()
snake_head = [250, 250]
snake_pos = [[250, 250], [240, 250], [230, 250]]
apple_pos = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score = 0
pygame.init() # initialize pygame modules
#### display game window #####
display = pygame.display.set_mode((display_width, display_height))
display.fill(window_color)
pygame.display.update()
final_score = play_game(snake_head, snake_pos, apple_pos, 1, score)
display = pygame.display.set_mode((display_width, display_height))
display.fill(window_color)
pygame.display.update()
display_text = 'Your Score is: ' + str(final_score)
display_final_score(display_text)
pygame.quit()
这应该是一个简单的蛇游戏,但是我只看到一个空白屏幕。几秒钟后消失。我尝试在蛇周围“移动”,这增加了程序的寿命。因此,我猜想该程序可以运行,但不会显示在屏幕上。