gpt4 book ai didi

python - 移动背景pygame

转载 作者:行者123 更新时间:2023-11-28 17:41:30 25 4
gpt4 key购买 nike

我一直在研究 python 游戏开发,我对它很陌生,我试图复制一个太空入侵者,这就是我想出的,它的灵感来 self 在网上找到的教程和代码。

我想知道的是让背景移动,所以我认为创建 2 个 Sprite 和 2 个背景图像并让它们一个接一个地移动是个好主意。一旦有人离开屏幕,它就会重新出现在屏幕底部,依此类推。但它不起作用我不确定出了什么问题。这是我必须做的:

#!/usr/bin/env python

from helpers import *

class Space1(pygame.sprite.Sprite):
def __init__(self, i):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image("space.png", 10)
self.dx = -5
self.reset(i)

def update(self, i):
self.rect.top += self.dx
if i == 1:
if self.rect.top <= -600:
self.__init__(i)
else:
if self.rect.top <= -1200:
self.__init__(i)

def reset(self, i):
if i == 1:
self.rect.top = 1
else:
self.rect.top = 300

class Space2(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image("space.png", 10)
self.dx = -5
self.reset()

def update(self):
self.rect.top += self.dx
if self.rect.top <= -1200:
self.__init__()

def reset(self):
self.rect.top = 600

class Player(pygame.sprite.Sprite):

def __init__(self):

pygame.sprite.Sprite.__init__(self)

self.image, self.rect = load_image('ship.gif', -1)
self.x_dist = 5
self.y_dist = 5
self.lasertimer = 0
self.lasermax = 5
self.rect.centery = 400
self.rect.centerx = 400

def update(self):
key = pygame.key.get_pressed()

# Movement
if key[K_UP]:
self.rect.centery += -3
if key[K_DOWN]:
self.rect.centery += 3
if key[K_RIGHT]:
self.rect.centerx += 3
if key[K_LEFT]:
self.rect.centerx += -3

# Lasers
if key[K_SPACE]:
self.lasertimer = self.lasertimer + 1
if self.lasertimer == self.lasermax:
laserSprites.add(Laser(self.rect.midtop))
self.lasertimer = 0

# Restrictions
self.rect.bottom = min(self.rect.bottom, 600)
self.rect.top = max(self.rect.top, 0)
self.rect.right = min(self.rect.right, 800)
self.rect.left = max(self.rect.left, 0)

class Laser(pygame.sprite.Sprite):
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image("laser.png", -1)
self.rect.center = pos

def update(self):
if self.rect.right > 800:
self.kill()
else:
self.rect.move_ip(0, -15)

class Enemy(pygame.sprite.Sprite):
def __init__(self, centerx):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image("alien.png", -1)
self.rect = self.image.get_rect()
self.dy = 8
self.reset()

def update(self):
self.rect.centerx += self.dx
self.rect.centery += self.dy
if self.rect.top > 600:
self.reset()

# Laser Collisions
if pygame.sprite.groupcollide(enemySprites, laserSprites, 1, 1):
explosionSprites.add(EnemyExplosion(self.rect.center))

# Ship Collisions
if pygame.sprite.groupcollide(enemySprites, playerSprite, 1, 1):
explosionSprites.add(EnemyExplosion(self.rect.center))
explosionSprites.add(PlayerExplosion(self.rect.center))

def reset(self):
self.rect.bottom = 0
self.rect.centerx = random.randrange(0, 600)
self.dy = random.randrange(5, 10)
self.dx = random.randrange(-2, 2)

class EnemyExplosion(pygame.sprite.Sprite):
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image("enemyexplosion.png", -1)
self.rect.center = pos
self.counter = 0
self.maxcount = 10

def update(self):
self.counter = self.counter + 1
if self.counter == self.maxcount:
self.kill()

class PlayerExplosion(pygame.sprite.Sprite):
def __init__(self, pos):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image("enemyexplosion.png", -1)
self.rect.center = pos
self.counter = 0
self.maxcount = 10

def update(self):
self.counter = self.counter + 1
if self.counter == self.maxcount:
self.kill()
exit()

def main():
# Initialize Everything

pygame.init()

screen = pygame.display.set_mode((800, 600))

pygame.display.set_caption('UoN Invaders')

# Create The Backgound

background = pygame.Surface(screen.get_size())

background = background.convert()

background.fill((000, 000, 000))

# Display The Background

screen.blit(background, (0, 0))

pygame.display.flip()

# Start Music

music = pygame.mixer.music.load ("data/spacequest.mp3")
pygame.mixer.music.play(-1)



# Initialize Game Objects
global clock

clock = pygame.time.Clock()

i = 0
i += 1

space1 = Space1(i)
space2 = Space2()
global player

player = Player()


# Render Objects
# Space
space1 = pygame.sprite.RenderPlain((space1))
space2 = pygame.sprite.RenderPlain((space2))

# Player
global playerSprite
playerSprite = pygame.sprite.RenderPlain((player))

# Enemy
global enemySprites
enemySprites = pygame.sprite.RenderPlain(())
enemySprites.add(Enemy(200))
enemySprites.add(Enemy(300))
enemySprites.add(Enemy(400))

# Projectiles
global laserSprites
laserSprites = pygame.sprite.RenderPlain(())

# Collisions
global enemyExplosion
enemyExplosion = pygame.sprite.RenderPlain(())
global playerExplosion
playerExplosion = pygame.sprite.RenderPlain(())
global explosionSprites
explosionSprites = pygame.sprite.RenderPlain(())


# Main Loop

going = True

while going:

clock.tick(60)



# Input Events

for event in pygame.event.get():

if event.type == QUIT:

going = False

elif event.type == KEYDOWN and event.key == K_ESCAPE:

going = False

# Update
space1.update(i)
space2.update()
player.update()
enemySprites.update()
laserSprites.update()
enemyExplosion.update()
playerExplosion.update()
explosionSprites.update()



screen.blit(background, (0, 0))


# Draw
space1.draw(screen)
space2.draw(screen)
playerSprite.draw(screen)
enemySprites.draw(screen)
laserSprites.draw(screen)
enemyExplosion.draw(screen)
playerExplosion.draw(screen)
explosionSprites.draw(screen)


pygame.display.flip()



pygame.quit()

if __name__ == '__main__':

main()

最佳答案

class Space(pygame.sprite.Sprite):
def __init__(self, num):
pygame.sprite.Sprite.__init__(self)
self.rect.top = num * 600
self.image, self.rect = load_image("space.png", 10)
self.dx = -5
self.reset()

def update(self):
self.rect.top += self.dx
if self.rect.top <= -1200:
self.reset()

def reset(self):
self.rect.top = 600

所以这是一个用于存储您的 Sprite 的类。我认为您不需要 2,因为您可以拥有所有空间实例的列表,例如在 main 中,而不是......

space1 = Space1(i)
space2 = Space2()

我建议做类似的事情。

spaces = []
for x in range(2):
spaces.append(Space(x))

然后改变

space1 = pygame.sprite.RenderPlain((space1))
space2 = pygame.sprite.RenderPlain((space2))

for space in spaces:
space = pygame.sprite.RenderPlain((space))

然后代替

space1.update(i)
space2.update()

for space in spaces:
space.update()

终于代替了

space1.draw(screen) 
space2.draw(screen)

for space in spaces:
space.draw(screen)

我希望这对您有用,因为我认为这是一种合理的存储空间实例的方式,并允许您使用更少的代码更轻松地获得代码。如果您在执行此操作时遇到任何问题,请发表评论,我会帮助您解决问题。

干杯!

关于python - 移动背景pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23560235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com