【发布时间】:2017-10-02 00:06:11
【问题描述】:
我决定自己尝试制作吃蛇圆点游戏。当我尝试运行我拥有的东西时,我无法让我的 pygame 窗口保持打开状态。我怎样才能让它保持打开状态?
import pygame
import time
import random
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
black = (0,0,0)
white = (255,255,255)
snake_width = 20
snake_height = 20
pygame.init()
game_width = 800
game_height = 600
gameDisplay = pygame.display.set_mode((game_width,game_height))
pygame.display.set_caption('Snake Game!')
clock = pygame.time.Clock()
class game:
def _init_(self):
snake_startx = (game_width/2)
snake_starty = (game_heiht/2)
x = thingx
y = thingy
x_change = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
x_change = -5
elif event.key == pygame.K_UP:
x_change = 5
snake(white, thing_startx, thing_starty, snake_width,
snake_height, x_change)
pygame.display.update()
clock.tick(60)
class snake:
def _init_(self, color, thingx, thingy, thingw, thingh, things):
color = white
thingx = snake_startx
thingy = snake_starty
thingw = snake_width
thingh = snake_height
things = x_change
pygame.draw.rect(gameDisplay, white, [snake_startx,
snake_starty,
snake_width, snake_height,])
game()
pygame.quit()
quit()
【问题讨论】:
-
你在学习什么教程?
-
你的代码有很多问题,但第一个问题是你的游戏中的
_init_函数实际上从未被调用,所以你永远不会进入你的 while 循环(尝试放置一些print语句在那里进行测试。将其重命名为__init__(双下划线,在python中专门定义),它至少应该调用所有这些东西(即使它现在已经坏了)。 -
我建议你使用 Sentdex 的 Pygame 初学者指南。可以在 youtube 上找到。