Pygame window 未打开,但 pygame 图标显示在文档中
Pygame window not opening, but pygame icon showing up in doc
我想弄清楚为什么我的显示器没有出现。我在 mac 上,我可以在我的停靠栏中看到 pygame 图标,但没有制作 windows。我是 运行 pygame 2.0.2 python 3.9.6 pycharm。我试过打开其他 pygame 个脚本,它们打开得很好,但我看不出这两个脚本打开它的方式有什么区别。
import pygame
import random
from enum import Enum
from collections import namedtuple
pygame.init()
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN1 = (0, 255, 0)
GREEN2 = (0, 200, 100)
BLACK = (0, 0, 0)
SPEED = 20
BLOCK_SIZE = 20
Point = namedtuple('Point', 'x, y')
font = pygame.font.SysFont('arial', 25)
class Direction(Enum):
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
class SnakeGame:
def __init__(self, w=640, h=480):
self.w = w
self.h = h
# init display
self.display = pygame.display.set_mode((self.w, self.h))
pygame.display.set_caption('Snake')
self.clock = pygame.time.Clock()
# init game state
self.direction = Direction.RIGHT
self.head = Point(self.w / 2, self.h / 2)
self.snake = [self.head,
Point(self.head.x - BLOCK_SIZE, self.head.y),
Point(self.head.x - BLOCK_SIZE * 2, self.head.y)]
self.score = 0
self.food = None
self._place_food()
def _place_food(self):
x = random.randint(0, (self.w - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
y = random.randint(0, (self.w - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
self.food = Point(x, y)
if self.food in self.snake:
self._place_food()
def play_step(self):
# Collect user input
# Move
# Check if game over
# Place new food or just move
# Update ui and clock
# self._update_ui()
# self.clock.tick(SPEED)
# Return game over and score
game_over = False
return game_over, self.score
def _update_ui(self):
self.display.fill(BLACK)
for pt in self.snake:
pygame.draw.rect(self.display, GREEN1, pygame.Rect(pt.x, pt.y, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(self.display, GREEN2, pygame.Rect(pt.x + 4, pt.y + 4, 12, 12))
pygame.draw.rect(self.display, RED, pygame.Rect(self.food.x, self.food.y, BLOCK_SIZE, BLOCK_SIZE))
text = font.render('Score: ' + str(self.score), True, WHITE)
self.display.blit(text, [0, 0])
pygame.display.flip()
if __name__ == '__main__':
game = SnakeGame()
while True:
game_over, score = game.play_step()
if game_over:
break
print('Final Score', score)
pygame.quit()
您必须处理应用程序循环中的事件。见 pygame.event.get()
respectively pygame.event.pump()
:
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
if __name__ == '__main__':
game = SnakeGame()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
game_over, score = game.play_step()
if game_over:
break
print('Final Score', score)
pygame.quit()
我想弄清楚为什么我的显示器没有出现。我在 mac 上,我可以在我的停靠栏中看到 pygame 图标,但没有制作 windows。我是 运行 pygame 2.0.2 python 3.9.6 pycharm。我试过打开其他 pygame 个脚本,它们打开得很好,但我看不出这两个脚本打开它的方式有什么区别。
import pygame
import random
from enum import Enum
from collections import namedtuple
pygame.init()
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN1 = (0, 255, 0)
GREEN2 = (0, 200, 100)
BLACK = (0, 0, 0)
SPEED = 20
BLOCK_SIZE = 20
Point = namedtuple('Point', 'x, y')
font = pygame.font.SysFont('arial', 25)
class Direction(Enum):
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
class SnakeGame:
def __init__(self, w=640, h=480):
self.w = w
self.h = h
# init display
self.display = pygame.display.set_mode((self.w, self.h))
pygame.display.set_caption('Snake')
self.clock = pygame.time.Clock()
# init game state
self.direction = Direction.RIGHT
self.head = Point(self.w / 2, self.h / 2)
self.snake = [self.head,
Point(self.head.x - BLOCK_SIZE, self.head.y),
Point(self.head.x - BLOCK_SIZE * 2, self.head.y)]
self.score = 0
self.food = None
self._place_food()
def _place_food(self):
x = random.randint(0, (self.w - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
y = random.randint(0, (self.w - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
self.food = Point(x, y)
if self.food in self.snake:
self._place_food()
def play_step(self):
# Collect user input
# Move
# Check if game over
# Place new food or just move
# Update ui and clock
# self._update_ui()
# self.clock.tick(SPEED)
# Return game over and score
game_over = False
return game_over, self.score
def _update_ui(self):
self.display.fill(BLACK)
for pt in self.snake:
pygame.draw.rect(self.display, GREEN1, pygame.Rect(pt.x, pt.y, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(self.display, GREEN2, pygame.Rect(pt.x + 4, pt.y + 4, 12, 12))
pygame.draw.rect(self.display, RED, pygame.Rect(self.food.x, self.food.y, BLOCK_SIZE, BLOCK_SIZE))
text = font.render('Score: ' + str(self.score), True, WHITE)
self.display.blit(text, [0, 0])
pygame.display.flip()
if __name__ == '__main__':
game = SnakeGame()
while True:
game_over, score = game.play_step()
if game_over:
break
print('Final Score', score)
pygame.quit()
您必须处理应用程序循环中的事件。见 pygame.event.get()
respectively pygame.event.pump()
:
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
if __name__ == '__main__':
game = SnakeGame()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
game_over, score = game.play_step()
if game_over:
break
print('Final Score', score)
pygame.quit()