【发布时间】: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