亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Python 的 Pygame 庫更改類中的參數值

如何使用 Python 的 Pygame 庫更改類中的參數值

慕仙森 2022-09-27 09:35:44
我試圖寫一個像游戲一樣的太空侵略者。我最初編寫代碼時只提供1個入侵者來給我一些東西來構建。當我開始添加更多入侵者時,我決定創建一個類,在這個類中,我可以讓入侵者列表循環。我試圖在我的列表中的方法中更改入侵者的x坐標,但發現它不會改變值,因此入侵者被留在原地。任何幫助將不勝感激!這是我正在研究的課程:import sys, pygame, randomfrom pygame.locals import *import itertools#from bullet import Invaderpygame.init()screen_height = 600screen_width = 1200DISPLAYSURF = pygame.display.set_mode((screen_width, screen_height))FPS = 200score = 0pygame.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.centerxtext_rect.centery = DISPLAYSURF_rect.centeryinvader_right_movement = Falseinvader_left_movement = Trueclass 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 = 40invader_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_yspaceship_rect.centerx = DISPLAYSURF_rect.centerxspaceship_rect.centery = DISPLAYSURF_rect.bottom - 40move_right = Falsemove_left = Falsemove_rate = 5bullet_firing = False
查看完整描述

1 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

問題是,您每幀都調用該類,這會將其值放回 中分配的值。您應該在開始時調用該類一次,然后每幀調用一次方法。這是一個鏈接,用于了解有關類的更多信息。__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_rect


bullet = 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


查看完整回答
反對 回復 2022-09-27
  • 1 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號