python - 如何在屏幕上绘制对象?

标签 python class object pygame blit

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

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()

最佳答案

这里有一个无限循环:

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_display1 .

在下一行中,1 > 0所以你设置count_object_display返回0 .

0 <= 0开始while 循环永远运行。


我不知道你想在那里做什么,但也许只是使用一个简单的 for循环,如:

for o in obstacle_list:
    o.spawn_obstacle()

关于python - 如何在屏幕上绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66691700/

相关文章:

c# - 将自定义对象属性映射回原始对象

python - 单元格编辑期间 QTableWidgetItem 中的居中文本

python - 如何从具有匹配 block 值的 block 中复制 pandas 中的第一行和最后一行?

c++ - C2039 : 'Method' +W is not a member of Class

javascript - 为什么这个选择器不起作用? JS

javascript - Uncaught ReferenceError : calcAge is not defined

Python 诅咒 : module function vs instance function

python - matplotlib 在 X 轴上绘制整数

matlab - 返回特定属性的默认 get 方法 - MATLAB

javascript - 如果你给一个对象赋一个属性,你能让它随着对象的变化而变化吗?