Pygame 个矩形全部聚集在一起
Pygame rectangles all bunching up
我正在制作一个游戏,你每天都在砍柴。我决定把树做成 class,但是当我这样做的时候,我得到了构成树的所有绘制的矩形都聚集在同一个位置。至少我认为它做到了,因为只会出现一棵树。完整代码如下:
import pygame
import time
pygame.init()
root = pygame.display.set_mode((603, 573))
pygame.display.set_caption("Homework")
window_is_open = True
white = (255, 255, 255)
black = (0, 0, 0)
width = 10
leaves_width = 30
height = 20
leaves_height = 10
x = 0
tree_trunk_x = 10
y = 0
tree_trunk_y = 10
vel = 5
brown = (150, 75, 0)
green = (58, 95, 11)
class Tree:
def __init__(self, tree_x, tree_y, tree_property_name):
global pos_x
global pos_y
global tree_property
pos_x = tree_x
pos_y = tree_y
tree_property = tree_property_name
def destroy(self):
count = pygame.font.SysFont('Tahoma', 18, True, False)
root.fill(white)
countdown = count.render('3', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
count = pygame.font.SysFont('Tahoma', 18, True, False)
root.fill(white)
countdown = count.render('2', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
count = pygame.font.SysFont('Tahoma', 18, True, False)
root.fill(white)
countdown = count.render('1', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
tree_property = False
def create_tree(self):
if tree_property:
trunk_x = pos_x + 10
trunk_y = pos_y + 10
pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
pygame.draw.rect(root, green, (pos_x, pos_y, leaves_width, leaves_height))
def redraw(self):
self.create_tree()
tree_one_property = True
tree_two_property = True
tree_three_property = True
tree_four_property = True
tree_five_property = True
tree_six_property = True
tree_seven_property = True
tree_eight_property = True
tree_nine_property = True
tree_ten_property = True
tree_eleven_property = True
tree_twelve_property = True
tree_thirteen_property = True
tree_fourteen_property = True
tree_fifteen_property = True
tree_sixteen_property = True
tree_seventeen_property = True
tree_eighteen_property = True
tree_nineteen_property = True
tree_twenty_property = True
tree_twenty_one_property = True
tree_twenty_two_property = True
tree_twenty_three_property = True
tree_twenty_four_property = True
tree_twenty_five_property = True
tree_one = Tree(0, 0, tree_one_property)
tree_two = Tree(50, 0, tree_two_property)
tree_three = Tree(100, 0, tree_three_property)
tree_four = Tree(150, 0, tree_four_property)
tree_five = Tree(200, 0, tree_five_property)
tree_six = Tree(0, 50, tree_six_property)
tree_eight = Tree(100, 50, tree_eight_property)
tree_seven = Tree(50, 50, tree_seven_property)
tree_nine = Tree(150, 50, tree_nine_property)
tree_ten = Tree(200, 50, tree_ten_property)
tree_eleven = Tree(0, 100, tree_eleven_property)
tree_twelve = Tree(50, 100, tree_twelve_property)
tree_thirteen = Tree(100, 100, tree_thirteen_property)
tree_fourteen = Tree(150, 100, tree_fourteen_property)
tree_fifteen = Tree(200, 100, tree_fifteen_property)
tree_sixteen = Tree(0, 150, tree_sixteen_property)
tree_seventeen = Tree(50, 150, tree_seventeen_property)
tree_eighteen = Tree(100, 150, tree_eighteen_property)
tree_nineteen = Tree(150, 150, tree_nineteen_property)
tree_twenty = Tree(200, 150, tree_twenty_property)
tree_twenty_one = Tree(0, 200, tree_twenty_one_property)
tree_twenty_two = Tree(50, 200, tree_twenty_two_property)
tree_twenty_three = Tree(100, 200, tree_twenty_three_property)
tree_twenty_four = Tree(150, 200, tree_twenty_four_property)
tree_twenty_five = Tree(200, 200, tree_twenty_five_property)
root.fill(white)
while window_is_open:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
if keys[pygame.K_q]:
tree_one.destroy()
root.fill(white)
font = pygame.font.SysFont('Tahoma', 18, True, False)
score = font.render('Score:', True, (0, 0, 0))
root.blit(score, (410, 0))
rectangle = pygame.draw.rect(root, (0, 0, 0), (x, y, width, 10))
tree_one.redraw()
tree_two.redraw()
tree_three.redraw()
tree_four.redraw()
tree_five.redraw()
tree_six.redraw()
tree_seven.redraw()
tree_eight.redraw()
tree_nine.redraw()
tree_ten.redraw()
tree_eleven.redraw()
tree_twelve.redraw()
tree_thirteen.redraw()
tree_fourteen.redraw()
tree_fifteen.redraw()
tree_sixteen.redraw()
tree_seventeen.redraw()
tree_eighteen.redraw()
tree_nineteen.redraw()
tree_twenty.redraw()
tree_twenty_one.redraw()
tree_twenty_two.redraw()
tree_twenty_three.redraw()
tree_twenty_four.redraw()
tree_twenty_five.redraw()
pygame.display.update()
pygame.quit()
如果你创建一个class,那么你必须使用instance attributes(使用self.
),而不是全局变量。例如:
class Tree:
def __init__(self, tree_x, tree_y, tree_property_name):
self.pos_x = tree_x
self.pos_y = tree_y
self.tree_property = tree_property_name
# [...]
def create_tree(self):
if self.tree_property:
trunk_x = self.pos_x + 10
trunk_y = self.pos_y + 10
pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
pygame.draw.rect(root, green, (self.pos_x, self.pos_y, leaves_width, leaves_height))
注意,全局变量只存在一次,但class的每个对象(实例)都存在一个实例属性。因此每个 Tree
对象都有自己的位置。
这不是答案,而是一种查找可能错误的方法。每当您有多个相同类型的对象时,请使用列表或字典。这将避免多个变量,如 tree_1
、tree_2
等
tree_list = []
for _ in range(5):
i = Tree()
tree_list.append(i)
tree_1
然后是 tree_list[0]
等等
我正在制作一个游戏,你每天都在砍柴。我决定把树做成 class,但是当我这样做的时候,我得到了构成树的所有绘制的矩形都聚集在同一个位置。至少我认为它做到了,因为只会出现一棵树。完整代码如下:
import pygame
import time
pygame.init()
root = pygame.display.set_mode((603, 573))
pygame.display.set_caption("Homework")
window_is_open = True
white = (255, 255, 255)
black = (0, 0, 0)
width = 10
leaves_width = 30
height = 20
leaves_height = 10
x = 0
tree_trunk_x = 10
y = 0
tree_trunk_y = 10
vel = 5
brown = (150, 75, 0)
green = (58, 95, 11)
class Tree:
def __init__(self, tree_x, tree_y, tree_property_name):
global pos_x
global pos_y
global tree_property
pos_x = tree_x
pos_y = tree_y
tree_property = tree_property_name
def destroy(self):
count = pygame.font.SysFont('Tahoma', 18, True, False)
root.fill(white)
countdown = count.render('3', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
count = pygame.font.SysFont('Tahoma', 18, True, False)
root.fill(white)
countdown = count.render('2', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
count = pygame.font.SysFont('Tahoma', 18, True, False)
root.fill(white)
countdown = count.render('1', True, (0, 0, 0))
root.blit(countdown, (583, 553))
pygame.display.update()
time.sleep(1)
tree_property = False
def create_tree(self):
if tree_property:
trunk_x = pos_x + 10
trunk_y = pos_y + 10
pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
pygame.draw.rect(root, green, (pos_x, pos_y, leaves_width, leaves_height))
def redraw(self):
self.create_tree()
tree_one_property = True
tree_two_property = True
tree_three_property = True
tree_four_property = True
tree_five_property = True
tree_six_property = True
tree_seven_property = True
tree_eight_property = True
tree_nine_property = True
tree_ten_property = True
tree_eleven_property = True
tree_twelve_property = True
tree_thirteen_property = True
tree_fourteen_property = True
tree_fifteen_property = True
tree_sixteen_property = True
tree_seventeen_property = True
tree_eighteen_property = True
tree_nineteen_property = True
tree_twenty_property = True
tree_twenty_one_property = True
tree_twenty_two_property = True
tree_twenty_three_property = True
tree_twenty_four_property = True
tree_twenty_five_property = True
tree_one = Tree(0, 0, tree_one_property)
tree_two = Tree(50, 0, tree_two_property)
tree_three = Tree(100, 0, tree_three_property)
tree_four = Tree(150, 0, tree_four_property)
tree_five = Tree(200, 0, tree_five_property)
tree_six = Tree(0, 50, tree_six_property)
tree_eight = Tree(100, 50, tree_eight_property)
tree_seven = Tree(50, 50, tree_seven_property)
tree_nine = Tree(150, 50, tree_nine_property)
tree_ten = Tree(200, 50, tree_ten_property)
tree_eleven = Tree(0, 100, tree_eleven_property)
tree_twelve = Tree(50, 100, tree_twelve_property)
tree_thirteen = Tree(100, 100, tree_thirteen_property)
tree_fourteen = Tree(150, 100, tree_fourteen_property)
tree_fifteen = Tree(200, 100, tree_fifteen_property)
tree_sixteen = Tree(0, 150, tree_sixteen_property)
tree_seventeen = Tree(50, 150, tree_seventeen_property)
tree_eighteen = Tree(100, 150, tree_eighteen_property)
tree_nineteen = Tree(150, 150, tree_nineteen_property)
tree_twenty = Tree(200, 150, tree_twenty_property)
tree_twenty_one = Tree(0, 200, tree_twenty_one_property)
tree_twenty_two = Tree(50, 200, tree_twenty_two_property)
tree_twenty_three = Tree(100, 200, tree_twenty_three_property)
tree_twenty_four = Tree(150, 200, tree_twenty_four_property)
tree_twenty_five = Tree(200, 200, tree_twenty_five_property)
root.fill(white)
while window_is_open:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_is_open = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
if keys[pygame.K_q]:
tree_one.destroy()
root.fill(white)
font = pygame.font.SysFont('Tahoma', 18, True, False)
score = font.render('Score:', True, (0, 0, 0))
root.blit(score, (410, 0))
rectangle = pygame.draw.rect(root, (0, 0, 0), (x, y, width, 10))
tree_one.redraw()
tree_two.redraw()
tree_three.redraw()
tree_four.redraw()
tree_five.redraw()
tree_six.redraw()
tree_seven.redraw()
tree_eight.redraw()
tree_nine.redraw()
tree_ten.redraw()
tree_eleven.redraw()
tree_twelve.redraw()
tree_thirteen.redraw()
tree_fourteen.redraw()
tree_fifteen.redraw()
tree_sixteen.redraw()
tree_seventeen.redraw()
tree_eighteen.redraw()
tree_nineteen.redraw()
tree_twenty.redraw()
tree_twenty_one.redraw()
tree_twenty_two.redraw()
tree_twenty_three.redraw()
tree_twenty_four.redraw()
tree_twenty_five.redraw()
pygame.display.update()
pygame.quit()
如果你创建一个class,那么你必须使用instance attributes(使用self.
),而不是全局变量。例如:
class Tree:
def __init__(self, tree_x, tree_y, tree_property_name):
self.pos_x = tree_x
self.pos_y = tree_y
self.tree_property = tree_property_name
# [...]
def create_tree(self):
if self.tree_property:
trunk_x = self.pos_x + 10
trunk_y = self.pos_y + 10
pygame.draw.rect(root, brown, (trunk_x, trunk_y, width, height))
pygame.draw.rect(root, green, (self.pos_x, self.pos_y, leaves_width, leaves_height))
注意,全局变量只存在一次,但class的每个对象(实例)都存在一个实例属性。因此每个 Tree
对象都有自己的位置。
这不是答案,而是一种查找可能错误的方法。每当您有多个相同类型的对象时,请使用列表或字典。这将避免多个变量,如 tree_1
、tree_2
等
tree_list = []
for _ in range(5):
i = Tree()
tree_list.append(i)
tree_1
然后是 tree_list[0]
等等