python-3.x - 你知道为什么我的网格没有像我预期的那样工作吗?
问题描述
背景信息:我正在尝试用 Python 制作塔防游戏,以便更好地理解 Turtle 和 PyGame 等可视化 Python 程序。我目前正在使用 turtle 包,我正在尝试制作一个 7 x 7 的测试网格。我正在试验我的代码,所以它可能有点混乱。
问题:我无法让网格正常工作。你知道我该如何解决这个问题吗?
其他要记住的事情:目前,龟屏上只绘制了一个正方形。我相信 for 循环应该沿着 y 坐标或行绘制一个正方形,然后沿着列向右移动。我需要一个网格而不是一个正方形。请记住,海龟形状周围没有边框。
过去的问题:过去,我收到过一个问题,即网格沿 y 轴在不一致的位置绘制块。当我更改代码时,这个问题就消失了。
窗口类 1
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()
解决方案
您将功能乌龟与面向对象的乌龟混合在一起,这总是会导致混乱。这是您的代码的简化的一个文件的实现,我相信它可以满足您的要求:
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
是一只乌龟。两种有效的方法。
推荐阅读
- metrics - 量规初始化
- javascript - 当值为 0 时,更改复选框 - 输入只接受数字
- sql - SQL UPDATE CASE 返回(0 行受影响),它不应该
- java - 硒的 ZAP 设置
- spring-boot - 模拟收件人列表并从目录中读取
- elasticsearch - 多个 Elasticsearch Inner_hits 查询未按预期提供结果
- python - Swagger 与 Flask-Restplus、API 和多个蓝图
- javascript - 如何由用户自定义 FullCalenderBundle [Symfony4]
- c++ - 无法配置 websocketpp 示例 CMakeLists.txt 文件
- javascript - 在 CMD 中运行测试时如何更改赛普拉斯输出