gpt4 book ai didi

python - Pygame - 让 Sprite 循环浏览类中的图像,并分配一个碰撞盒

转载 作者:行者123 更新时间:2023-12-01 00:07:06 25 4
gpt4 key购买 nike

TLDR;代码如下

我已经厌倦了 pygame 我只是很困惑,就像我想我正在学习更多关于类(class)、循环和一切的知识,但是,我认为在这个学校项目中,我们在短短几天的共同努力(面对面)中付出了太多的努力。去学校学习 Full Stacks Dev,这是他们让我们做的第一个项目,我在 stackoverflow 上从你那里学到了很多东西,但是你给出了复杂的答案,我们让它们工作,但我想我学到了很多关于调试的知识。

无论如何,这么多与 pygame 相关的问题的原因肯定是不想成为 pygame 专家......

僵尸代码:

import pygame
import random
import math
# import fighting_game

# Player = player()




class ZombieEnemy(pygame.sprite.Sprite):
# zwalkRight = [pygame.image.load('images/zombiestandright1.png'), pygame.image.load('images/ztep.png'), pygame.image.load('imageszombiestandright1.png'), pygame.image.load('images/zombiestandright1.png')]

# zwalkLeft = [(pygame.image.load('images/zombiestandleft.png'), pygame.image.load('images/ztepleft.png'), pygame.image.load('images/zombiestandleft.png'), pygame.image.load('images/ztep2.png')]
def __init__(self, x=300, y=360):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/zombie.png')
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.speedy = random.randrange(1, 8)
def move_towards_player(self, player):
# Find direction vector (dx, dy) between enemy and player.
dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
dist = math.hypot (dx, dy)
dx, dy = dx / dist, dy / dist # Normalize
# Move along this normalized vector towards the player
self.rect.x += dx * 10
self.rect.y += dy * 0

主要代码:

import pygame
import math
import random
from Zombie import *
# from pygame.locals import *
pygame.init()

win = pygame.display.set_mode((900,567))

pygame.display.set_caption("Power Rangers ZOMBIES")

walkRight = [pygame.image.load('images/walk1.png'), pygame.image.load('images/walk2.png'), pygame.image.load('images/walk3.png'), pygame.image.load('images/walk4.png'), pygame.image.load('images/walk5.png'), pygame.image.load('images/walk6.png')]
walkLeft = [pygame.image.load('images/leftwalk2.png'), pygame.image.load('images/leftwalk3.png'), pygame.image.load('images/leftwalk4.png'), pygame.image.load('images/leftwalk5.png'), pygame.image.load('images/leftwalk6.png'), pygame.image.load('images/leftwalk7.png')]
bg = pygame.image.load('images/background.png')
char = pygame.image.load('images/standingstill.png')
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
self.image = pygame.Surface((144,200))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 0
self.isJump = False
self.left = False
self.right = False
self.walkCount = 0
self.jumpCount = 10
self.rect.x = x
self.rect.y = y

def draw(self, win):
if self.walkCount + 1 >= 18:
self.walkCount = 0

if self.left:
win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount +=1
else:
win.blit(char, (self.x,self.y))

class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load('images/background.png')
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location

BackGround = Background('images/background.png', [0,0])

# def redrawGameWindow():
# win.blit(bg, (0,0))
# man.draw(win)

# pygame.display.update()
all_zombies = pygame.sprite.Group()





#mainloop
man = Player(100, 340, 40, 60)
run = True
for i in range( 50 ):
new_x = random.randrange( 0, 10000) # random x-position
# new_y = random.randrange( 0, ) # random y-position
z = ZombieEnemy(new_x)
all_zombies.add(z) # create, and add to group
z.move_towards_player(man)

#####
while run:
clock.tick(27)

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

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and man.x > man.vel:
BackGround.rect.left = BackGround.rect.left + int(10)
man.x -= man.vel
man.left = True
man.right = False
elif keys[pygame.K_RIGHT]: #and man.x < 500 - man.width - man.vel:
BackGround.rect.left = BackGround.rect.left - int(10)
man.x += man.vel
man.right = True
man.left = False
else:
man.right = False
man.left = False
man.walkCount = 0

if not(man.isJump):
if keys[pygame.K_SPACE]:
man.isJump = True
man.right = False
man.left = False
man.walkCount = 0
else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10

# redrawGameWindow()
for zombie in all_zombies:
zombie.move_towards_player(man)

win.blit(BackGround.image, BackGround.rect)
all_zombies.update()
man.draw(win)
all_zombies.draw(win)
pygame.display.flip()


pygame.quit()

最佳答案

玩家(man)是 Sprite所有的僵尸都在 Groupall_zombies
使用pygame.sprite.spritecollide()寻找僵尸和玩家之间的碰撞。例如:

if pygame.sprite.spritecollide(man, all_zombies, False):
print("hit")

要实现这一点,您必须在 .rect 属性中跟踪玩家的位置,因为 pygame.sprite.spritecollide 使用它来查找碰撞。
您根本不需要属性 .x.y。例如:

class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
# [...]

self.rect.x = x
self.rect.y = y

def draw(self, win):
if self.walkCount + 1 >= 18:
self.walkCount = 0

if self.left:
win.blit(walkLeft[self.walkCount//3], (self.rect.x,self.rect.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.rect.x,self.rect.y))
self.walkCount +=1
else:
win.blit(char, (self.rect.x,self.rect.y))
while run:
# [...]

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and man.rect.x > man.vel:
BackGround.rect.left = BackGround.rect.left + int(10)
man.rect.x -= man.vel
man.left = True
man.right = False
elif keys[pygame.K_RIGHT]: #and man.x < 500 - man.width - man.vel:
BackGround.rect.left = BackGround.rect.left - int(10)
man.rect.x += man.vel
man.right = True
man.left = False
else:
man.right = False
man.left = False
man.walkCount = 0

关于python - Pygame - 让 Sprite 循环浏览类中的图像,并分配一个碰撞盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59904989/

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