สไปรท์ไม่เพิ่มในกลุ่มเพื่อแสดงบนหน้าจอ

Traceback (การโทรล่าสุดครั้งล่าสุด): ไฟล์ "***/Pcprojects/pygameKCC/kcc-shmup.py" บรรทัด 49 ใน all_sprites.update()

TypeError: การอัปเดตเมธอดที่ไม่ถูกผูกไว้ () จะต้องถูกเรียกพร้อมกับอินสแตนซ์กลุ่มเป็นอาร์กิวเมนต์แรก (ไม่มีอะไรแทน)

ฉันกำลังติดตามบทช่วยสอนที่มีโค้ดที่ใช้งานได้ (สำหรับเขา) และตลอดชีวิตของฉัน ฉันไม่สามารถเรนเดอร์สไปรท์นี้ได้ ใครสามารถบอกฉันว่าฉันทำอะไรผิด? นี่เป็นครั้งแรกของฉันกับสไปรท์นอกเหนือจากโปรแกรมรักษาหน้าจอลูกบอลเด้งแบบพื้นฐาน ขอบคุณ!

import pygame
import random
import sys
import math

#global constants
WIDTH = 360
HEIGHT = 480
FPS = 30

#define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
#YELLOW = (X, X, X)


class Player(pygame.sprite.Sprite):
    #sprite for the player.
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        #required image and collision rectangle
        self.image = pygame.Surface((50, 50))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2, HEIGHT / 2)

#itialize pygame
#itialize music mixer
pygame.init()
pygame.mixer.init()

#create screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

#create a group for all sprites to belong to
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)


running = True
while running:

    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

#อัปเดต:

    all_sprites.update()
    screen.fill(BLACK)
    all_sprites.draw(screen)
    pygame.display.flip()

pygame.quit()

person DerTier    schedule 10.12.2016    source แหล่งที่มา
comment
หลังจากวาดสไปรท์ หน้าจอของคุณ fill() - วิธีนี้คุณจะลบสไปรท์ทั้งหมดก่อนที่จะส่งออกจากบัฟเฟอร์บนจอภาพ (โดยใช้ flip()) คุณต้องใช้ fill() ก่อน ถัดไป draw() และหลังจากนั้นให้ใช้ flip() เพื่อส่งบัฟเฟอร์บนมอนิเตอร์   -  person furas    schedule 10.12.2016
comment
ฉันรันโค้ดและฉันไม่ได้รับข้อผิดพลาดนี้   -  person furas    schedule 10.12.2016
comment
สำหรับกิจกรรมใน pygame.event.get(): if event.type == pygame.QUIT: running = False #game ออกจาก [x] คลิก #Update all_sprites.update() #Render/Draw screen.fill(BLACK) all_sprites .draw(screen) pygame.display.flip() นี่คือข้อความที่ตัดตอนมาที่ฉันย้ายไปรอบๆ แต่ฉันยังคงได้รับข้อผิดพลาดเดิม อาจเป็นปัญหาของ pycharm ได้หรือไม่ _eta: ฉันไม่รู้ว่าคุณจัดรูปแบบโค้ดในความคิดเห็นอย่างไร ขออภัย!   -  person DerTier    schedule 10.12.2016
comment
เพิ่มสิ่งนี้ในคำถาม - มันจะอ่านง่ายขึ้นและทุกคนจะอ่าน   -  person furas    schedule 10.12.2016
comment
และเพิ่มข้อความแสดงข้อผิดพลาดแบบเต็ม (Traceback) อาจมีข้อมูลที่เป็นประโยชน์มากขึ้น   -  person furas    schedule 10.12.2016
comment
หากคุณคิดว่ามันเป็นปัญหากับ PyCharm ให้เรียกใช้โค้ดภายนอก PyCharm - python script.py - แล้วคุณจะเห็นว่าคุณพูดถูกหรือไม่   -  person furas    schedule 10.12.2016
comment
มันเป็นปัญหาของ pycharm ขอบคุณมาก! ขอโทษสำหรับความสับสน!   -  person DerTier    schedule 10.12.2016


คำตอบ (1)


คุณต้องล้างหน้าจอก่อนโดยใช้ fill() และวาดสไปรท์ในภายหลัง - draw() และหลังจากนั้นคุณสามารถส่งบัฟเฟอร์ไปที่มอนิเตอร์ได้ - flip()

#Render/Draw
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()

BTW: เพื่อจัดองค์ประกอบตรงกลางบน screen คุณสามารถใช้

screen_rect = screen.get_rect()

และหลังจากนั้น

 self.rect.center = screen_rect.center

เช่นเดียวกับที่คุณสามารถจัดวางสิ่งอื่นๆ ไว้ตรงกลางได้ เช่น ข้อความบนปุ่ม

person furas    schedule 10.12.2016