PyCharm编写小游戏当运行代码时,它会出现很短的时间并立即关闭。求大神指导!谢谢!


import pygame
import sys
import time
import random
from pygame.locals import *
#
pygame.init()
fpsClock = pygame.time.Clock()

# 创建pygame显示框层
playsurface = pygame.display.set_mode((640, 480))
# 定义标题
pygame.display.set_caption('Sanke go!')
# 加载资源图片,game.ico包含在最后的文件中
image = pygame.image.load('cc.jpg')
# 设置图标
pygame.display.set_icon(image)

# 定义颜色变量
redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
lightGrey = pygame.Color(220, 220, 220)

# 定义gameover函数
def gameover(playsurface, score):
    gameoverfont = pygame.font.Font('arial.ttf', 72)
    gameoversurf = gameoverfont.render('Game Over', True, greyColour)
    gameoverrect = gameoversurf.get_rect()
    gameoverrect.midtop = (320, 125)
    playsurface.blit(gameoversurf, gameoverrect)
    # 显示分数并定义字体和大小
    scorefont = pygame.font.Font('arial.ttf', 72)
    scoresurf = scorefont.render('Game Over', True, greyColour)
    scorerect = scoresurf.get_rect()
    scorerect.midtop = (320, 125)
    playsurface.blit(scoresurf, scorerect)
    pygame.display.flip()
    time.sleep(1000)
    pygame.quit()
    sys.exit()


# 初始化变量
sankePosition = [100, 100]
snakeSegments = [[100, 100], [80, 100], [60, 100]]
foodPosition = [300, 300]
foodSpawned = 1
direction = 'right'
changeDirection = direction
score = 0
# 按键pygame事件
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.eixt()
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.RIGHT or event.key == ord('d'):
            changeDirection = 'right'
        if event.key == pygame.LEFT or event.key == ord('a'):
            changeDirection = 'left'
        if event.key == pygame.UP or event.key == ord('w'):
            changeDirection = 'up'
        if event.key == pygame.DOWN or event.key == ord('s'):
            changeDirection = 'down'
        if event.key == pygame.ESCAPE:
            pygame.event.post(pygame.event.Event(pygame.QUIT))
# 方向判断
if changeDirection == 'right' and not direction == 'left':
    direction = changeDirection
if changeDirection == 'left' and not direction == 'right':
    direction = changeDirection
if changeDirection == 'up' and not direction == 'down':
    direction = changeDirection
if changeDirection == 'down' and not direction == 'up':
    direction = changeDirection
# 蛇头转弯动作
if direction == 'right':
    sankePosition[0] += 20
if direction == 'left':
    sankePosition[0] += 20
if direction == 'up':
    sankePosition[1] += 20
if direction == 'down':
    sankePosition[1] += 20

# 将蛇头位置加入到列表中
snakeSegments.insert(0, list(sankePosition))

# 判断是否吃的食物
if sankePosition[0] == foodPosition[0] and sankePosition[1] == foodPosition[1]:
    foodSpawned = 0
else:
    snakeSegments.pop()
# 重新生成食物
if foodSpawned == 0:
    x = random.randrange(1, 32)
    y = random.randrange(1, 24)
    foodPosition = [int(x*20), int(y*20)]
    foodSpawned = 1
    score += 1

# 编写pygame显示层
playsurface.fill(blackColour)
for position in snakeSegments[1:]:
    pygame.draw.rect(playsurface, whiteColour, Rect(position[0], position[1], 20, 20))  # 白色为蛇身
pygame.draw.rect(playsurface, lightGrey, Rect(sankePosition[0], sankePosition[1], 20, 20))  # 灰色为蛇头
pygame.draw.rect(playsurface, redColour, Rect(foodPosition[0], foodPosition[1], 20, 20))  # 红色为食物

#  刷新显示层
pygame.display.flip()

#  死亡判断
if sankePosition[0] > 620 or sankePosition[0] < 0:
    gameover(playsurface, score)
if sankePosition[1] > 460 or sankePosition[1] < 0:
    gameover(playsurface, score)
for sankeBody in snakeSegments[1:]:
    if sankePosition[0] == sankeBody[0] and sankePosition[1] == sankeBody[1]:
        gameover(playsurface, score)

# 增加游戏难度
if len(snakeSegments) < 40:
    speed = 6 + len(snakeSegments)//4
else:
    speed = 16
fpsClock.tick(speed)

 

查看全部
weixin_50741739
weixin_50741739
2020/11/25 23:34
  • python
  • 点赞
  • 收藏
  • 回答
    私信

1个回复