猿问

如何使用 Python 的 Pygame 库更改类中的参数值

我试图写一个像游戏一样的太空侵略者。我最初编写代码时只提供1个入侵者来给我一些东西来构建。当我开始添加更多入侵者时,我决定创建一个类,在这个类中,我可以让入侵者列表循环。


我试图在我的列表中的方法中更改入侵者的x坐标,但发现它不会改变值,因此入侵者被留在原地。任何帮助将不胜感激!


这是我正在研究的课程:


import sys, pygame, random

from pygame.locals import *

import itertools

#from bullet import Invader


pygame.init()

screen_height = 600

screen_width = 1200

DISPLAYSURF = pygame.display.set_mode((screen_width, screen_height))

FPS = 200


score = 0


pygame.display.set_caption('Testing Pygame')

spaceship = pygame.image.load('spaceship copy.bmp')


spaceship_rect = spaceship.get_rect()

DISPLAYSURF_rect = DISPLAYSURF.get_rect()


FONT = pygame.font.Font('freesansbold.ttf', 32)

text = FONT.render('Score ' + str(score), True, (180, 180, 180))

text_rect = text.get_rect()

text_rect.centerx = DISPLAYSURF_rect.centerx

text_rect.centery = DISPLAYSURF_rect.centery


invader_right_movement = False

invader_left_movement = True


class Invader():


    def __init__(self, invader, invader_x, invader_y):

        self.invader = invader

        self.rect = invader.get_rect()

        self.rect.x = invader_x

        self.rect.y = invader_y


    def move_invader(self, movement):

        #self.x = self.rect.x

        #self.y = self.rect.y

        #Move invader

        if invader_right_movement == True:

            self.rect.x += movement

        if invader_left_movement == True:

            self.rect.x -= movement

        DISPLAYSURF.blit(self.invader, (self.rect.x, self.rect.y))


invaders_x = [10, 90, 170, 250, 330, 410, 490, 570, 650, 730]

invaders_y = 40


invader_image = pygame.image.load('invader.bmp')

invaders = []

for x in invaders_x:

    invaders.append(Invader(invader_image,x,invaders_y)) 


invaders_rect = []

for invader, x in zip(invaders, invaders_x):

    invader.centerx = x

    invader.centery = invaders_y



spaceship_rect.centerx = DISPLAYSURF_rect.centerx

spaceship_rect.centery = DISPLAYSURF_rect.bottom - 40


move_right = False

move_left = False

move_rate = 5


bullet_firing = False


慕仙森
浏览 96回答 1
1回答

月关宝盒

问题是,您每帧都调用该类,这会将其值放回 中分配的值。您应该在开始时调用该类一次,然后每帧调用一次方法。这是一个链接,用于了解有关类的更多信息。__init__要修复代码,请执行以下操作:while True:    #do other stuff    for invader in invaders:        invader.move_invader()    #do other stuff由于所有入侵者都具有相同的图像,因此您可以invader_image = pygame.image.load('invader.bmp')invaders = []for x in invader_x:    invaders.append(Invader(invader_image,x,40,1))这应该使每个入侵者每帧移动1个像素要获取冲突的矩形,您可以将矩形添加到类中class Invader    def __init__(self, invader, invader_x, invader_y, movement):        self.rect = invader.get_rect()        self.rect.x = invader_x        self.rect.y = invader_y        #rest of code    def move_invader(self):        #if moveing move x        self.rect.x = x        #rest of code和碰撞for bullet in range(len(bullets_fired)-1,-1,-1):     for invader in invaders:        if invader.rect.colliderect(bullets_fired[bullet]):            score += 1            invader_x = DISPLAYSURF_rect.centerx            invader_x = DISPLAYSURF_rect.top + 40            del bullets_fired[bullet]            invaders.remove(invader) #to get rid of the invader too            bullet_fired = False            break       elif bullets_fired[bullet].y > 0:            bullet_fired = True            bullets_fired[bullet].y -=  14            bullet_rect = pygame.draw.rect(DISPLAYSURF, RED, (x, y, width, height))对于碰撞,我向后循环,因为如果您删除列表中的第2个项目符号,则第3个项目符号将成为第2个项目符号,循环转到跳过项目符号的第3个项目符号(即第4个项目符号)。但是,如果你向后走,如果你删除了一个项目符号,你已经检查过的项目符号就会被移动,你可以继续检查下一个项目符号。对于子弹,你有子弹和bullet_rect它们是一回事,你有一个项目符号列表,就个人而言,我会摆脱bullet_rectbullet = pygame.draw.rect(screen,RED,(x,y,w,y))为pygame.draw.rect(screen,RED,bullets_fired[bullet]现在对于您添加项目符号的位置,当您按空格键时,而不是获得y,只需获取矩形elif event.key == K_SPACE:    if bullet_fired == False:        rect = spaceship_rect.copy() # this makes the bullet the same size as the ship        #you could also do rect = pygame.rect(0,0,0,0)        rect.x = spaceship_rect.centerx - 3        rect.y = spaceship_rect.top        #rect.w = width        #rect.h = height        bullets_fired.append(rect)并返回到碰撞循环中,将 y 更改为bullets_fired[项目符号]y
随时随地看视频慕课网APP

相关分类

Python
我要回答