Python Pygame-让精灵在类中的图像中循环,并分配一个命中框
TLDR;代码如下 我受够了pygame我只是太困惑了, 就像我想我正在学习更多关于课程、循环和一切的知识,但是,我认为我们在这所学校的项目中花了几天的时间(面对面)就学到了很多东西。这是他们让我们做的第一个项目,我在stackoverflow上从你那里学到了很多,但是你给出了复杂的答案,我们让它们工作,但是我想我在调试方面学到了很多 不管怎么说,这么多与pygame相关的问题的原因肯定是我不想成为pygame专家 僵尸代码:Python Pygame-让精灵在类中的图像中循环,并分配一个命中框,python,python-3.x,image,pygame,Python,Python 3.x,Image,Pygame,TLDR;代码如下 我受够了pygame我只是太困惑了, 就像我想我正在学习更多关于课程、循环和一切的知识,但是,我认为我们在这所学校的项目中花了几天的时间(面对面)就学到了很多东西。这是他们让我们做的第一个项目,我在stackoverflow上从你那里学到了很多,但是你给出了复杂的答案,我们让它们工作,但是我想我在调试方面学到了很多 不管怎么说,这么多与pygame相关的问题的原因肯定是我不想成为pygame专家 僵尸代码: import pygame import random import
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()
导入pygame
输入数学
随机输入
从僵尸导入*
#从pygame.locals导入*
pygame.init()
win=pygame.display.set_模式((900567))
pygame.display.set_标题(“强力游骑兵僵尸”)
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()
职业玩家(pygame.sprite.sprite):
定义初始值(自、x、y、宽度、高度):
self.image=pygame.Surface((144200))
self.rect=self.image.get_rect()
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.vel=0
self.isJump=False
self.left=False
self.right=False
self.walkCount=0
self.jumpCount=10
self.rect.x=x
自校正y=y
def抽签(自我,赢):
如果self.walkCount+1>=18:
self.walkCount=0
如果self.left:
blit(walkLeft[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
elif self.right:
blit(walkRight[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
其他:
win.blit(char,(self.x,self.y))
类背景(pygame.sprite.sprite):
def_uuuinit_uuu(自我、图像文件、位置):
pygame.sprite.sprite.\uuuu初始化(self)\uuuu调用sprite初始值设定项
self.image=pygame.image.load('images/background.png')
self.rect=self.image.get_rect()
self.rect.left,self.rect.top=位置
背景=背景('images/BackGround.png',[0,0])
#def重画游戏窗口():
#温·布利特(背景,(0,0))
#男子平局(胜利)
#pygame.display.update()
all_zombies=pygame.sprite.Group()
#主回路
男子=运动员(1003404060)
运行=真
对于范围(50)内的i:
新的x=random.randrange(0,10000)#随机x位置
#新y=random.randrange(0,)#随机y位置
z=僵尸敌人(新的x)
所有僵尸。添加(z)#创建并添加到组中
z、 向球员(男子)移动
#####
运行时:
时钟滴答作响(27)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
keys=pygame.key.get_pressed()
如果键[pygame.K_左]和man.x>man.vel:
BackGround.rect.left=BackGround.rect.left+int(10)
man.x-=man.vel
左=真
对=错
elif keys[pygame.K_RIGHT]:#和man.x<500-man.width-man.vel:
BackGround.rect.left=BackGround.rect.left-int(10)
man.x+=man.vel
正确的
左=假
其他:
对=错
左=假
man.walkCount=0
如果没有(man.isJump):
如果键[pygame.K_SPACE]:
man.isJump=True
对=错
左=假
man.walkCount=0
其他:
如果man.jumpCount>=-10:
负=1
如果man.jumpCount<0:
负=-1
man.y-=(man.jumpCount**2)*0.5*neg
man.jumpCount-=1
其他:
man.isJump=False
man.jumpCount=10
#重画游戏窗口()
对于所有_僵尸中的僵尸:
僵尸。向玩家(人)移动
win.blit(BackGround.image,BackGround.rect)
所有僵尸。更新()
男子平局(胜利)
所有僵尸。抽签(赢)
pygame.display.flip()
pygame.quit()
玩家(人
)是一个僵尸,所有僵尸都在所有僵尸
中用于查找僵尸和玩家之间的冲突。e、 g:
如果pygame.sprite.spritecollide(人,所有僵尸,错误):
打印(“点击”)
要做到这一点,您必须在.rect
属性中跟踪玩家的位置,因为pygame.sprite.spritecollide
使用该属性查找碰撞。您根本不需要属性
.x
和.y
。e、 g:
职业玩家(pygame.sprite.sprite):
定义初始值(自、x、y、宽度、高度):
# [...]
self.rect.x=x
自校正y=y
def抽签(自我,赢):
如果self.walkCount+1>=18:
self.walkCount=0
如果self.left:
blit(walkLeft[self.walkCount//3],(self.rect.x,self.rect.y))
self.walkCount+=1
elif self.right:
blit(walkRight[self.walkCount//3],(self.rect.x,self.rect.y))
self.walkCount+=1
其他:
blit(char,(self.rect.x,self.rect.y))
运行时:
# [...]
keys=pygame.key.get_pressed()
如果键[pygame.K_LEFT]和man.rect.x>man.vel:
BackGround.rect.left=BackGround.rect.left+int(10)
man.rect.x-=man.vel
左=右