我在导入pygame模块时遇到问题

-2 投票
1 回答
42 浏览
提问于 2025-04-14 17:57
import math
import random
import pygame
import tkinter as tk
from tkinter import messagebox

class cube(object):
    rows = 20

    def __init__(self, start, dirnix=1, dirniy=0, color=(0, 0, 0)):
        self.pos = start
        self.dirnx = dirnix
        self.dirny = dirniy
        self.color = color

    def move(self, dirnx, dirny):
        self.dirnx = dirnx
        self.dirny = dirny
        self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)

    def draw(self, surface, eyes=False):
        dis = size // rows
        rw = self.pos[0]
        cm = self.pos[1]
        pygame.draw.rect(surface, self.color, (rw * dis + 1, cm * dis + 1, dis - 2, dis - 2))

        if eyes:
            center = dis // 2
            radius = 3
            circle_middle = (rw * dis + center - radius - 2, cm * dis + 8)
            circle_middle2 = (rw * dis + dis - radius * 2, cm * dis + 8)
            pygame.draw.circle(surface, (255, 255, 255), circle_middle, radius)
            pygame.draw.circle(surface, (255, 255, 255), circle_middle2, radius)


class snake(object):
    body = []
    turns = {}

    def __init__(self, color, pos):
        self.color = color
        self.head = cube(pos)
        self.body.append(self.head)
        self.dirnx = 0
        self.dirny = 1

    def move(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

        keys = pygame.key.get_pressed()

        for i, c in enumerate(self.body):
            p = c.pos[:]
            if p in self.turns:
                turn = self.turns[p]
                c.move(turn[0], turn[1])
                if i == len(self.body) - 1:
                    self.turns.pop(p)
            else:
                if c.dirnx == -1 and c.pos[0] <= 0:
                    c.pos = (c.rows - 1, c.pos[1])
                elif c.dirnx == 1 and c.pos[0] >= c.rows - 1:
                    c.pos = (0, c.pos[1])
                elif c.dirny == 1 and c.pos[1] >= c.rows - 1:
                    c.pos = (c.pos[0], 0)
                elif c.dirny == -1 and c.pos[1] <= 0:
                    c.pos = (c.pos[0], c.rows - 1)
                else:
                    c.move(c.dirnx, c.dirny)

    def reset(self):
        pass

    def add_cube(self):
        pass

    def draw(self, surface):
        for i, c in enumerate(self.body):
            if i == 0:
                c.draw(surface, True)
            else:
                c.draw(surface)

def draw_grid(w, rows, surface):
    size_between = w // rows

    x = 0
    y = 0
    for l in range(rows):
        x = x + size_between
        y = y + size_between
        pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, w))
        pygame.draw.line(surface, (255, 255, 255), (0, y), (w, y))

def draw_window(surface):
    surface.fill((0, 255, 0))  # RGB
    s.draw(surface)
    draw_grid(size, rows, surface)
    pygame.display.update()

def main():
    global size, rows, s
    size = 500  # obszar gry
    rows = 20

    window = pygame.display.set_mode((size, size))

    s = snake((0, 0, 0), (10, 10))

    frag = True
    clock = pygame.time.Clock()

    while frag:
        pygame.time.delay(50)
        clock.tick(10)
        s.move()
        draw_window(window)

main()

我其实不知道该怎么做,我觉得我试过所有的方法。

1 个回答

-1

我猜你的意思是,导入失败了,因为你还没有安装pygame这个模块。你需要在命令窗口里运行pip命令:

pip install pygame

具体的安装步骤可以参考PyGame的文档。

撰写回答