Python Pygame运行极慢
我正在尝试用Pygame编写一个简单的蛇游戏。由于某些原因,游戏运行速度非常慢,大约为每秒1.3帧。我在我的MacBookPro和Windows笔记本电脑上测试了它,它在两台电脑上的运行速度都是一样的。该项目分布在多个文件中,因此我将链接github:。相关部分是Python Pygame运行极慢,python,pygame,Python,Pygame,我正在尝试用Pygame编写一个简单的蛇游戏。由于某些原因,游戏运行速度非常慢,大约为每秒1.3帧。我在我的MacBookPro和Windows笔记本电脑上测试了它,它在两台电脑上的运行速度都是一样的。该项目分布在多个文件中,因此我将链接github:。相关部分是run.py文件和Game文件夹 以下是我认为最相关的几个文件: run.py(主驱动程序类): snake_env.py(环境管理器): Snake.py(控制蛇): 不知道,但尝试升级到PyGame2,你可以下载它 pyth
run.py
文件和Game
文件夹
以下是我认为最相关的几个文件:
run.py
(主驱动程序类):
snake_env.py
(环境管理器):
Snake.py
(控制蛇):
不知道,但尝试升级到PyGame2,你可以下载它
python3 -m pip install -U pygame==2.0.0.dev6 --user
它至少应该起作用
pygame 2运行速度比普通pygame快在此处发布相关代码,而不是相关代码的链接。另外,创建一个最小的可复制示例:会很有帮助here@NotZack你可能在这方面运气更好。也就是说,我甚至猜不出你为什么要为一个简单的蛇游戏进口一堆Keras的东西。你需要人工智能技术做什么?@KarlKnechtel这是一个更大项目的一部分,我正在做强化学习的工作,也尝试升级到最新版本的python。
from Game.Snake import Snake
from Game.Food import Food
import pygame
import random
import numpy as np
import sys
from PIL import Image
class SnakeEnv():
def create_food(self):
x = random.randint(0, 19)
y = random.randint(0, 19)
while(self.snake.board[x, y] == 1 or self.snake.board[x, y] == 2):
x = random.randint(0, 19)
y = random.randint(0, 19)
self.food = Food(self.screen, x * 30, y * 30)
self.snake.update_food(x, y)
self.snake.board[x, y] = 5
def __init__(self, screen):
self.action_space = np.array([0, 1, 2, 3])
self.state = None
pygame.init()
self.screen = screen
self.snake = Snake(self.screen)
self.create_food()
self.state = self.snake.board
self.total_reward = 0
def reset(self):
self.__init__()
def screenshot(self):
data = pygame.image.tostring(self.screen, 'RGB')
image = Image.frombytes('RGB', (600, 600), data)
matrix = np.asarray(image.getdata(), dtype=np.uint8)
matrix = (matrix - 128)/(128 - 1)
matrix = np.reshape(matrix, (1, 600, 600, 3))
return matrix
def step(self, action):
d = dict()
d['state'] = self.screenshot()
self.snake.move(action)
reward = 0
done = False
if(self.snake.head.x == self.food.x and self.snake.head.y == self.food.y):
self.create_food()
self.snake.add_body()
reward += 1
else:
lost = self.snake.check_loss()
if lost == 1:
reward = -1
done = True
self.total_reward += reward
d['action'] = action
d['reward'] = reward
self.state = self.snake.board
d['next_state'] = self.screenshot()
d['done'] = done
return d
def get_state(self):
return np.reshape(self.snake.board, (400, 1)).T / 5
def render(self):
self.screen.fill((0, 0, 0))
self.food.render()
self.snake.render()
for r in self.snake.List:
pygame.display.update(r.rect)
def close(self):
pygame.quit()
def move(self, key):
self.snake.move(key)
import pygame
from Game.Body import Body
import numpy as np
class Snake:
def __init__(self, window):
self.head = Body(window, 0, 0, None)
self.List = [self.head]
self.window = window
self.board = np.zeros((20, 20))
self.board.fill(0.1)
self.board[0, 0]= 2
self.food_x = 0
self.food_y = 0
def render(self):
for body in self.List:
body.render()
def update_board(self):
self.board = np.zeros((20, 20))
self.board.fill(0.1)
self.board[self.food_x, self.food_y] = 5
for body in self.List:
body.move()
x = body.y // 30
y = body.x // 30
if(body.ahead is None):
try:
self.board[x, y] = 2
except:
pass
else:
self.board[x, y] = 1
def move(self, key):
if key == 3 and self.List[0].direction != 1:
self.List[0].direction = 3
elif key == 0 and self.List[0].direction != 2:
self.List[0].direction = 0
elif key == 1 and self.List[0].direction != 3:
self.List[0].direction = 1
elif key == 2 and self.List[0].direction != 0:
self.List[0].direction = 2
elif key == pygame.K_a:
print(self.head.old_x)
print(self.head.old_y)
return
self.update_board()
def add_body(self):
x = self.List[len(self.List) - 1].old_y
y = self.List[len(self.List) - 1].old_x
b = Body(self.window, x, y, self.List[len(self.List) - 1])
self.List.append(b)
direction = self.List[len(self.List) - 2].direction
b.set_direction(direction)
def check_loss(self):
if(self.head.x >= 600 or self.head.x < 0 or self.head.y >= 600 or self.head.y < 0):
#pygame.quit()
return 1
else:
for body in self.List:
if body is not self.head and body.x == self.head.x and body.y == self.head.y:
#pygame.quit()
return 1
return 0
def update_food(self, x, y):
self.food_x = y
self.food_y = x
def take_action(self, action):
if action == 0:
self.move(pygame.K_UP)
elif action == 1:
self.move(pygame.K_RIGHT)
elif action == 2:
self.move(pygame.K_DOWN)
elif action == 3:
self.move(pygame.K_LEFT)
import pygame
from pygame import Rect
class Body:
"""
0 - up
1 - right
2 - down
3 - left
"""
def __init__(self, window, x, y, ahead):
self.direction = 2
self.window = window
self.x = x
self.y = y
self.old_x = x
self.old_y = y
self.rect = Rect(x, y, 30, 30)
self.ahead = ahead
def render(self):
pygame.draw.rect(self.window, (255, 255, 255), (self.x, self.y, 30, 30))
def set_direction(self, direction):
self.direction = direction
def move(self):
if(self.ahead is None):
self.old_x = self.y
self.old_y = self.x
if self.direction == 0:
self.y -= 30
elif self.direction == 1:
self.x += 30
elif self.direction == 2:
self.y += 30
elif self.direction == 3:
self.x -= 30
else:
self.old_x = self.y
self.old_y = self.x
self.direction = self.ahead.direction
self.x = self.ahead.old_y
self.y = self.ahead.old_x
python3 -m pip install -U pygame==2.0.0.dev6 --user