【问题标题】:Do you know why my grid isn't working as I am expecting?你知道为什么我的网格没有像我预期的那样工作吗?
【发布时间】:2021-08-04 06:19:22
【问题描述】:

背景信息: 我正在尝试用 python 制作一个塔防游戏,以便更好地理解 Turtle 和 PyGame 等可视化 python 程序。我目前正在使用 turtle 包,我正在尝试制作一个 7 x 7 的测试网格。我正在试验我的代码,所以它可能有点乱。

问题: 我无法让网格正常工作。你知道我该如何解决这个问题吗?

要记住的其他事项: 此刻,龟屏上只画了一个正方形。我相信 for 循环应该沿着 y 坐标或行绘制一个正方形,然后沿着列向右移动。我需要一个网格而不是一个正方形。请记住,海龟形状周围没有边框。

过去的问题: 过去,我收到过一个问题,即网格沿 y 轴在不一致的位置绘制块。当我更改代码时,这个问题就消失了。

WindowClass1

import turtle
class Window1 :
    def __init__(self, width, height, startx, starty, turtle):
        self.width   = width
        self.height  = height
        self.startx   = startx
        self.starty = starty
        self.turtle = turtle
        
        #Sets the title of the screen
        turtle.title("Bloons Tower Defense Game")
        #Sets background color of the screen
        turtle.bgcolor("white")
        #Sets the size and position of the screen
        turtle.setup(width, height, startx, starty)
    def GetTurtle(self):
        return self.turtle*

块类1

import turtle
#Should I make the block class extend the turtle class?
#This way the Block class will have the same properties as turtle with some additional properties like x, y, width, height, IDGround, IDAir
#I think I need to have the block class inherit the turtle class 100%
class Block():
    blockturtle = turtle.Turtle()
    def __init__(self, x, y, size, Id):
        self.x = x
        self.y = y
        self.size = size
        self.Id = Id

    def GetXCoor(self):
        return self.x
    
    def SetXCoor(self, x):
        self.x = x
    
    def GetYCoor(self,y):
        return self.y
    
    def SetYCoor(self, y):
        self.y = y

    def GetSize(self):
        return self.size

    def SetSize(self, size):
        self.size = size

    def DrawBlock(self):
        self.blockturtle.penup()
        self.blockturtle.speed(0)
        self.blockturtle.pendown()
        if(self.Id == 0):
            self.blockturtle.color("red")
        elif(self.Id == 1):
            self.blockturtle.color("green")
        turtle.register_shape("test_square", ((0,0),(0,self.size),(self.size,self.size),(self.size,0)))
        self.blockturtle.shape("test_square")
        #Creates Duplicates of the blockturtle object.  THis way we aren't changing data for the same turtle each time.
        self.blockturtle.stamp()

    def DrawGrid(self, row_count, column_count):
        self.blockturtle.setpos(self.x,self.y)
        for c in range(column_count):
            for r in range(row_count):
                self.SetXCoor(-300 + (c*self.size))
                self.SetYCoor(-100 + (r*self.size))
                self.DrawBlock()

主文件

import sys
import turtle
import numpy as np
from WindowClass1 import Window1
from BlockClass1 import Block

#I only need a turtle object when I am wanting to draw a turtle
#Bg stands for background image

#Used to speed up the speed of complex graphics.  It will basically draw on the screen n number of times.
screen = turtle.Screen()

GameWindow = Window1(800, 600, 20, 7, screen)

row_count = 7
column_count = 7
count = 0

block = Block(-300,-100,20,0)
block.DrawGrid(row_count, column_count)

#This keeps the turtle objects drawing on the screen
turtle.done()

【问题讨论】:

    标签: python-3.x turtle-graphics python-turtle


    【解决方案1】:

    您将 功能 乌龟与 面向对象 乌龟混合在一起,这总是会导致混乱。这是您的代码的简化的一个文件的实现,我相信它可以满足您的需求:

    from turtle import Screen, Turtle
    
    class Window:
        def __init__(self, width, height, startx, starty, screen):
            self.screen = screen
    
            # Sets the title of the screen
            self.screen.title("Bloons Tower Defense Game")
            # Sets background color of the screen
            self.screen.bgcolor('white')
            # Sets the size and position of the screen
            self.screen.setup(width, height, startx, starty)
    
        def registerSquare(self, name, square):
            self.screen.register_shape(name, square)
    
    class Block(Turtle):
        def __init__(self, x, y, size, color, window):
            super().__init__()
    
            self.size = size
    
            window.registerSquare("test_square", ((0, 0), (0, self.size), (self.size, self.size), (self.size, 0)))
    
            self.shape("test_square")
            self.speed('fastest')
            self.penup()
            self.setposition(x, y)
            self.color(color)
    
        def getSize(self):
            return self.size
    
        def setSize(self, size):
            self.size = size
    
        def drawBlock(self):
            ''' Creates images of the block object.  This way we aren't changing data for the same turtle each time. '''
    
            self.stamp()
    
        def drawGrid(self, row_count, column_count):
            for c in range(column_count):
                for r in range(row_count):
                    self.setx(c * self.size - 300)
                    self.sety(r * self.size - 100)
                    self.drawBlock()
    
    row_count = 7
    column_count = 7
    
    screen = Screen()
    
    window = Window(800, 600, 20, 7, screen)
    
    block = Block(-300, -100, 20, 'red', window)
    
    block.drawGrid(row_count, column_count)
    
    screen.mainloop()
    

    这里Block 使用您的Window 实例,而不是依赖Screen 实例或海龟功能API。 Window 有一个屏幕,而Block 是一个海龟。两种有效的方法。

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多