我正在學習使用Python 3.6的PyGame模塊,並且在獲取知識的旅程中,我決定創建一個Snake遊戲。我的遊戲代碼在下面,除了蛇和蘋果不對齊,並且如果它們不對齊,吃蘋果是非常困難的,一切似乎都很好。 (他們關閉的只是幾個像素是否有幫助)蛇和蘋果不對齊
# Imports
import pygame
import random
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
# Setup
pygame_init = pygame.init()
dis_width = 1000
dis_height = 600
window = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption("Slither")
window.fill(white)
fps = 10
clock = pygame.time.Clock()
size = 25
apple = [[0, 0]]
snake = [[0, 0]]
x_step, y_step = 0, 0
apples_ate = 0
# Game Loop
main_menu = True
play_game = False
death_screen = False
while True:
# Main Menu
while main_menu:
# Setup
snake = [[(dis_width - 200)/2, dis_height/2, size]]
apple = [[random.randrange(0, dis_width - 200 - size), random.randrange(0, dis_height - size), size]]
# Message
x_step, y_step = 0, 0
window.fill(black, rect=[0, 0, dis_width, dis_height])
font = pygame.font.SysFont(None, 50)
window.blit(font.render("Press 'Space' To Continue", True, green), [100, 200])
pygame.display.update()
# Event Handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
main_menu = False
play_game = True
# Play Menu
while play_game:
# Event Handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_step = -size
x_step = 0
elif event.key == pygame.K_DOWN:
y_step = size
x_step = 0
elif event.key == pygame.K_LEFT:
x_step = -size
y_step = 0
elif event.key == pygame.K_RIGHT:
x_step = size
y_step = 0
# Boundries
if snake[0][0] + x_step < 0 or snake[0][0] + x_step > dis_width - size - 200:
play_game = False
death_screen = True
elif snake[0][1] + y_step < 0 or snake[0][1] + y_step > dis_height - size:
play_game = False
death_screen = True
else:
snake[0][0] += x_step
snake[0][1] += y_step
# Apple Test
if apple[0][0] == snake[0][0]:
apple = [[random.randrange(0, dis_width - 200 - size),
random.randrange(0, dis_height - size), size]]
apples_ate += 1
# Draws
window.fill(white, rect=[dis_width - 200, 0, 200, dis_height])
window.fill(black, rect=[0, 0, dis_width - 200, dis_height])
font = pygame.font.SysFont(None, 50)
window.blit(font.render("SCORE:", True, black), [dis_width - 200, 0])
window.blit(font.render(str(apples_ate), True, black), [dis_width - 200, 50])
pygame.draw.rect(window, red, [apple[0][0], apple[0][1], size, size])
pygame.draw.rect(window, green, [snake[0][0], snake[0][1], size, size])
pygame.display.update()
clock.tick(fps)
# Death Screen
while death_screen:
# Message
window.fill(black, rect=[0, 0, dis_width - 200, dis_height])
font = pygame.font.SysFont(None, 50)
window.blit(font.render("You died.", True, red), [100, 100])
window.blit(font.render("Press 'A' To Play Again.", True, red), [100, 200])
window.blit(font.render("Press 'Q' To Quit", True, red), [100, 300])
pygame.display.update()
# Event Handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
main_menu = True
death_screen = False
elif event.key == pygame.K_q:
pygame.quit()
quit()