首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在屏幕上绘制对象?

如何在屏幕上绘制对象?
EN

Stack Overflow用户
提问于 2021-03-18 21:04:44
回答 1查看 100关注 0票数 2

我想在屏幕上显示“障碍”类的每个对象。对象存储在列表obstacle_list中。我没有收到错误消息,但窗口只是冻结,没有对象显示在屏幕上。我想问题一定出在def spawn_obstacle(self):的某个地方,但我不知道我在哪里弄错了。

代码语言:javascript
运行
复制
import pygame
import random
import time
import math

# Initialize pygame
pygame.init()

# Create window (width, height)
screen = pygame.display.set_mode(((800, 600)))
ScreenHeight = screen.get_height()
ScreenWidth = screen.get_width()

# Background picture
background = pygame.image.load("background.jpg")

# Title and Icon
pygame.display.set_caption("F22-Raptor Simulator")
icon = pygame.image.load("jet1.png")
pygame.display.set_icon(icon)


# Object List (obstacles)
obstacle_list = []


class Obstacle(object):

    def __init__(self):  # removed unused parameters
        self.obstacleImg = 'rock.png'  # pygame.image.load("rock.png")
        self.obstacleX = random.randint(600, 700)
        self.obstacleY = random.randint(0, ScreenHeight - 64)
        self.obstacleX_change = random.uniform(-0.3, -0.2)

    def __repr__(self):
        return f'Obstacle(image={self.obstacleImg!r}, X={self.obstacleX}, Y={self.obstacleY}, change={self.obstacleX_change})'

    def spawn_obstacle(self):
        image = pygame.image.load(self.obstacleImg)
        screen.blit(image, (self.obstacleX, self.obstacleY))


# Keep window running (Infinite-Loop)
running = True

# Timer
timer1_start = time.time()
timer1_current = 0

# Counter while-loop to display objects
count_object_display = 0

# While-Loop (Everything that takes place during the game is inside here)
while running:

    timer1_current = time.time()

    if timer1_current - timer1_start >= 1:
        timer1_start = time.time()  # Timer of start set to current time
        obstacle = Obstacle()  # Create instance of class obstacle
        obstacle_list.append(obstacle)
        print(obstacle)

    # Insert Background
    screen.blit(background, (0, 0))

    # End game / close window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    while count_object_display <= len(obstacle_list)-1:
        obstacle_list[count_object_display].spawn_obstacle()
        count_object_display += 1

        if count_object_display > len(obstacle_list)-1:
            count_object_display = 0


    # Update after each iteration of the while-loop
    pygame.display.update()
EN

回答 1

Stack Overflow用户

发布于 2021-03-18 21:16:12

你在这里有一个无限循环:

代码语言:javascript
运行
复制
while count_object_display <= len(obstacle_list)-1:
    obstacle_list[count_object_display].spawn_obstacle()
    count_object_display += 1

    if count_object_display > len(obstacle_list)-1:
        count_object_display = 0

进入while循环并将count_object_display设置为1

在下一行中,使用1 > 0count_object_display重新设置为0

由于0 <= 0,while循环永远运行。

我不知道您在尝试做什么,但也许只需要使用一个简单的for循环,比如:

代码语言:javascript
运行
复制
for o in obstacle_list:
    o.spawn_obstacle()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66691700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档