简体   繁体   English

for语句不适用于pygame

[英]The for statement not working with pygame

I've wanted to do some work with evolution simulation. 我想对进化模拟做一些工作。 The first part was to make independently moving squares on pygame. 第一部分是在pygame上制作独立移动的方块。 This seems to work except one slight hitch! 这似乎很有效,只需要轻按一下即可! When using the for statement line 43 it doesn't seem to apply it to all the squares that are generated only the first one. 使用for语句行43时,似乎并没有将其应用于仅第一个生成的所有平方。 So one square moves as intended the rest just sit there doing nothing.. please help :D 因此,一个正方形按预期移动,其余的只是坐在那里什么都不做..请帮助:D

import pygame, sys
from pygame.locals import *
import time
import random

pygame.init()

# set up the window
DISPLAYSURF = pygame.display.set_mode((500, 500), 0, 32)
pygame.display.set_caption('Drawing')

# set up the colors
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
GREEN = (  0, 255,   0)
BLUE  = (  0,   0, 255)

# draw on the surface object
DISPLAYSURF.fill(BLACK)
robot_list=[]
counter =0
while counter < 26:
    x=random.randrange(1,500)
    x2 = x +6
    y= random.randrange(1,500)
    y2 = y +6
    robot_file=pygame.draw.polygon(DISPLAYSURF, WHITE, ((x, y), (x2, y), (x2, y2), (x, y2)))

    robot_list.append(robot_file)
    counter +=1

# run the game loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    for r in robot_list:
        time.sleep(0.1)
        pygame.draw.polygon(DISPLAYSURF, BLACK, ((x, y), (x2, y), (x2, y2), (x, y2)))
        rand2=random.randrange(1,6)
        rand1=random.randrange(-6,6)
        x += rand1
        x2+= rand1
        y+=rand2
        y2+=rand2

        pygame.draw.polygon(DISPLAYSURF, WHITE, ((x, y), (x2, y), (x2, y2), (x, y2)))
        pygame.display.update()

    pygame.display.update()

Simple: You only have one polygon in your code. 简单:代码中只有一个多边形。 I'm not sure what pygame.draw.polygon() returns; 我不确定pygame.draw.polygon()返回什么; according to the documentation , it doesn't return anything useful. 根据文档 ,它不会返回任何有用的信息。

What you need to do instead is put the coordinates of the polygon into the list robot_file (which is a pretty bad name for a list, BTW) and then update those coordinates. 相反,您需要做的是将多边形的坐标放入列表robot_file (对于列表robot_file ,这是一个很糟糕的名称,BTW),然后更新这些坐标。

it doesn't seem to apply it to all the squares that are generated only the first one. 它似乎没有将其应用于仅第一个生成的所有正方形。

The problem here is that you don't have any kind of square . 这里的问题是您没有任何正方形 You just draw something using the drawing function. 您只需要使用绘图功能来绘图即可。 To understand this better, maybe just create a class abstracting your game objects from the main loop. 为了更好地理解这一点,也许只需创建一个类即可从主循环中抽象出游戏对象

Example: 例:

class Polygon(object):
    def __init__(self):
       self.x = random.randrange(1,500)
       self.x2 = self.x + 6
       self.y = random.randrange(1,500)
       self.y2 = self.y + 6

    def move(self):
        rand2 = random.randrange(1,6)
        rand1 = random.randrange(-6,6)
        self.x += rand1
        self.x2 += rand1
        self.y += rand2
        self.y2 += rand2

    def draw(self, surface):
        pygame.draw.polygon(surface, WHITE, ((self.x, self.y), (self.x2, self.y), (self.x2, self.y2), (self.x, self.y2)))

for _ in xrange(26):
    robot_list.append(Polygon())

# run the game loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    for r in robot_list:
        r.move()
        r.draw(DISPLAYSURF)

    time.sleep(0.1)
    pygame.display.update()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM