[英]How to change the value of parameters in a class using Python's Pygame library
我正在嘗試寫一個像游戲一樣的太空入侵者。 我最初編寫我的代碼只提供 1 個入侵者來給我一些可以構建的東西。 當我開始添加更多入侵者時,我決定創建一個 class,在其中我可以讓入侵者列表循環。
我試圖在我的列表中的一個方法中更改入侵者的 x 坐標,但我發現它並沒有改變值,因此入侵者留在原地。 任何幫助將不勝感激!
這是我正在研究的 class:
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
#Testing bullet firing
RED = (240, 0, 0)
x = spaceship_rect.centerx - 3
y = spaceship_rect.top
width = 6
height = 24
#bullet = pygame.draw.rect(DISPLAYSURF, RED, (x, y, width, height))
#bullet.centerx = spaceship_rect.centerx - 3
#bullet.centery = spaceship_rect.top + height
#bullet_rect = pygame.draw.rect(DISPLAYSURF, RED, (x, y, width, height))
bullets_fired = []
bullet_fired = False
while True:
DISPLAYSURF.fill((0, 0, 0))
DISPLAYSURF.blit(spaceship, spaceship_rect)
DISPLAYSURF.blit(text, text_rect)
#get invader to move 1-8 pixels randomly to one side before turning back
num_movements = random.randint(1, 8)
for invader in invaders:
invader.move_invader(num_movements)
for invader in invaders:
if invader.rect.centerx >= screen_width - 30:
invader_left_movement = True
invader_right_movement = False
if invader.rect.centerx <= 0 + 30:
invader_left_movement = False
invader_right_movement = True
FPSCLOCK = pygame.time.Clock()
#movement of bullet
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]
bullet_fired = False
elif bullets_fired[bullet].y > 0:
bullet_fired = True
bullets_fired[bullet].y -= 14
pygame.draw.rect(DISPLAYSURF,RED,bullets_fired[bullet])
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
#Movement of player
if event.key == K_RIGHT:
move_right = True
elif event.key == K_LEFT:
move_left = True
#Bullet firing
elif event.key == K_SPACE:
if bullet_fired == False:
rect = spaceship_rect
rect.x = spaceship_rect.centerx - 3
rect.y = spaceship_rect.top
bullets_fired.append(rect)
elif event.type == KEYUP:
#Movement of player
if event.key == K_RIGHT:
move_right = False
elif event.key == K_LEFT:
move_left = False
text = FONT.render('Score ' + str(score), True, (180, 180, 180))
if move_right == True and spaceship_rect.right <= screen_width:
spaceship_rect.centerx += move_rate
if move_left == True and spaceship_rect.left >= 0:
spaceship_rect.centerx -= move_rate
pygame.display.update()
FPSCLOCK.tick(FPS)
問題是您在每一幀都調用 class ,這會將其值放回__init__
中分配的值。 您應該在開始時調用 class 一次,然后每幀調用這些方法。 這是一個鏈接以了解有關課程的更多信息。
要修復您的代碼,請執行以下操作:
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
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 個)跳過一個項目符號。 但是如果你向后 go,如果你刪除一個子彈,你已經檢查過的子彈會被移動,你可以繼續檢查下一個。
對於子彈,您有 bullet 和 bullet_rect 是相同的東西,並且您有一個子彈列表,就個人而言,我會擺脫 bullet_rect
bullet = pygame.draw.rect(screen,RED,(x,y,w,y))
為了
pygame.draw.rect(screen,RED,bullets_fired[bullet]
現在在你添加項目符號的地方,當你按空格鍵時,而不是得到 y,只得到 rect
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[bullet].y
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.