我试图创建一个游戏(pygame),其中有一个精灵,当玩家碰撞时,随机移动到表面上的另一个地方,我不知道我会怎么做,就像我有非常基本的知识,一个猜测我会将random.randint分配给精灵,但我会从那里去哪里?非常感谢,我很感激帮助。代码如下:)
import pygame, sys
from pygame.locals import *
import random
pygame.init()
game=0
width=450
height=613
blue=(100,149,237)
white=(255,255,255)
purple =(128,0,128)
Level=pygame.image.load("level.jpg")
background=pygame.image.load("background.jpg")
startscreen=pygame.image.load("ocean.jpg")
gamescreen=pygame.image.load("screengame.jpg")
Display=pygame.display.set_mode((width,height))
pygame.display.set_caption("Ocean")
movex = 100
movey = 100
DirectX = 0
DirectY = 0
game = 0
MoveEnemy1X = 150
MoveEnemy1Y = 50
rectDirectX = 10
rectDirectY = 9
MoveEnemy2X = 300
MoveEnemy2Y = 100
rectDirect2X = 30
rectDirect2Y = 10
MoveEnemy3X = 400
MoveEnemy3Y = 200
rectDirect3X = 12
rectDirect3Y = 10
clock = pygame.time.Clock()
gameScore = 0
scorefont = pygame.font.SysFont("Arial",30)
score = scorefont.render("Score: " +str(gameScore),True,white)
##pygame.mixer.music.load('ocean.mp3')
##pygame.mixer.music.play(-1,0.0)
direc1 = 5
direc2 = 5
class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.Rect(x,y,width,height)
def RenderPlayer(self):
pygame.draw.rect(Display,blue,(self.x,self.y,self.width,self.height))
self.rect = pygame.Rect(self.x,self.y,self.width,self.height)
class EnemySprite(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.Rect(x,y,width,height)
def RenderEnemy(self):
pygame.draw.rect(Display,white,(MoveEnemy1X,MoveEnemy1Y,30,30))
self.rect = pygame.Rect(MoveEnemy1X,MoveEnemy1Y,self.width,self.height)
class EnemySprite2(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.Rect(x,y,width,height)
def RenderEnemy2(self):
pygame.draw.rect(Display,white,(MoveEnemy2X,MoveEnemy2Y,40,10))
self.rect = pygame.Rect(MoveEnemy2X,MoveEnemy2Y, self.width,self.height)
class EnemySprite3(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.Rect(x,y,width,height)
def RenderEnemy3(self):
pygame.draw.rect(Display, white,(MoveEnemy3X,MoveEnemy3Y,10,10))
self.rect = pygame.Rect(MoveEnemy3X,MoveEnemy3Y, self.width,self.height)
class GoodSprite(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.Rect(x,y,width,height)
def RenderGoodSprite(self):
pygame.draw.rect(Display,purple,(100,100,10,10))
self.rect = pygame.Rect(100,100,self.width,self.height)
Player1=Player(100,100,20,20)
Enemy1=EnemySprite(200,200,30,30)
Enemy2=EnemySprite2(300,200,40,10)
Enemy3=EnemySprite3(240,190,30,20)
Good=GoodSprite(150,400,10,10)
TheGameLoop = True
while TheGameLoop:
for event in pygame.event.get():
if (event.type==pygame.QUIT):
TheGameLoop=False
if (event.type==pygame.KEYDOWN):
if (event.key==pygame.K_LEFT):
direc1 = -4
gameScore+=1
score = scorefont.render("Score: " +str(gameScore),True,white)
if (event.key==pygame.K_RIGHT):
direc1 = 4
gameScore+=1
score = scorefont.render("Score: " +str(gameScore),True,white)
if (event.key==pygame.K_DOWN):
direc2 = 4
gameScore+=1
score = scorefont.render("Score: " +str(gameScore),True,white)
if (event.key==pygame.K_UP):
direc2 = -4
gameScore+=1
score = scorefont.render("Score: " +str(gameScore),True,white)
if (event.type==pygame.KEYUP):
if (event.key==pygame.K_LEFT):
direc1 = 0
if (event.key==pygame.K_RIGHT):
direc1 = 0
if (event.key==pygame.K_UP):
direc2 = 0
if (event.key==pygame.K_DOWN):
direc2 = 0
if MoveEnemy1X > 430:
MoveEnemy1X = 425
rectDirectX = -5
elif MoveEnemy1X < 0:
MoveEnemy1X = 10
rectDirectX = 5
elif MoveEnemy1Y > 613:
MoveEnemy1Y = 600
rectDirectY = -9
elif MoveEnemy1Y < 0:
MoveEnemy1Y = 0
rectDirectY = 11
if MoveEnemy2X > 430:
MoveEnemy2X = 425
rectDirect2X = -5
elif MoveEnemy2X < 0:
MoveEnemy2X = 11
rectDirect2X = 6
elif MoveEnemy2Y > 613:
MoveEnemy2Y = 600
rectDirect2Y = -12
elif MoveEnemy2Y < 0:
MoveEnemy2Y = 0
rectDirect2Y = 8
if MoveEnemy3X > 430:
MoveEnemy3X = 422
rectDirect3X = -7
elif MoveEnemy3X < 0:
MoveEnemy3X = 11
rectDirect3X = 8
elif MoveEnemy3Y > 613:
MoveEnemy3Y = 600
rectDirect3X = -12
elif MoveEnemy3Y < 0:
MoveEnemy3Y = 0
rectDirect3Y = 5
if direc1 > 430:
Display.blit(Level,(0,0))
pygame.display.update()
if game==0:
Display.blit(startscreen,(0,0))
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_SPACE:
game=1
Display.blit(gamescreen,(0,0))
elif event.key==pygame.K_ESCAPE:
pygame.quit()
pygame.display.update()
if game==1:
Display.blit(gamescreen,(0,0))
Display.blit(score,(220,10))
Player1.RenderPlayer()
Enemy1.RenderEnemy()
Enemy2.RenderEnemy2()
Enemy3.RenderEnemy3()
Good.RenderGoodSprite()
Player1.x +=direc1
Player1.y +=direc2
MoveEnemy1X +=rectDirectX
MoveEnemy1Y +=rectDirectY
MoveEnemy2X +=rectDirect2X
MoveEnemy2Y +=rectDirect2Y
MoveEnemy3X + rectDirect3X
MoveEnemy3Y += rectDirectY
## pygame.mixer.music.load("ocean.mp3")
## pygame.mixer.music.play(-1,0.0)
pygame.display.update()
if pygame.sprite.collide_rect(Player1,Enemy1):
game=2
Display.blit(background,(0,0))
pygame.mixer.music.load("untitled.mp3")
pygame.mixer.music.play(1,0.0)
if pygame.sprite.collide_rect(Player1,Enemy2):
game=2
Display.blit(background,(0,0))
pygame.mixer.music.load("untitled.mp3")
pygame.mixer.music.play(1,0.0)
if pygame.sprite.collide_rect(Player1,Good):
gameScore+=1
Good.x=random.randint(0,500)
Good.y=random.randint(0,600)
pygame.display.update()
if game==2:
Display.blit(background,(0,0))
Display.blit(score,(200,20))
gameScore=0
pygame.display.update()
pygame.mixer.music.stop()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_SPACE:
game=1
pygame.display.update()
clock.tick(50)
答案 0 :(得分:1)
关注代码的这一部分:
if pygame.sprite.collide_rect(Player1,Enemy1):
game=2
Display.blit(background,(0,0))
pygame.mixer.music.load("untitled.mp3")
pygame.mixer.music.play(1,0.0)
我通常使用pygame.sprite.spritecollide()。
我不确定“collide_rect”是否也能做到这一点,但这就是诀窍:
pygame.sprite.spritecollide()返回一个碰撞列表。这非常有用。
(另外,我会把你想要的所有精灵都放在列表中。)
- 所以如果我在你的情况下使用所有这些:
for each_enemy in enemy_list:
# see if player hit an enemy;
# the 'False' flag means DON'T delete anything in the event of a collision
enemy_touched_list = pygame.sprite.spritecollide(each_enemy, Player1, False)
# good, now each enemy that was touching a player is in this list
# now we will iterate through this list and find a random place for the enemy.
for enemy_touched in enemy_touched_list:
enemy_touched.render(random.randint(0,width), random.randint(0,height))
好的,所以代码将通过你的敌人列表中的每个敌人,检查它是否正在触及一个玩家,如果它是敌人的位置将被你的敌人类.render()方法重置。 / p>
为了使最后一行有效,我们需要稍微调整一下你的课程。\
类:
看起来您理解设置课程的语法,但是您错过了他们的力量。
没有理由你应该有4个敌人的课程!它们几乎完全相同。唯一的区别是几个值!这听起来像是我的对象!
您应该有一个enemysprite类,然后根据您的心愿从该类中创建尽可能多的对象。这就是为什么课程是最好的。精灵之间你想要的任何轻微的自定义,设置你的类,这样你就可以在创建一个对象时传递它们。
- 如果您需要更多帮助来改变课堂内容,请告诉我.--
- 也许你是OOP的新手,这些课程来自教程,如果是这样,我可以提供更多信息 -
和我之前说的一样,为了使我的代码版能够工作,我们需要改变你的(很快就会是单一的)敌人类的.render()方法:原文:
def RenderEnemy3(self):
pygame.draw.rect(Display, white,(MoveEnemy3X,MoveEnemy3Y,10,10))
self.rect = pygame.Rect(MoveEnemy3X,MoveEnemy3Y, self.width,self.height)
修改:
def RenderEnemy3(self,xpos,ypos):
pygame.draw.rect(Display, white,(MoveEnemy3X,MoveEnemy3Y,xpos,ypos))
self.rect = pygame.Rect(MoveEnemy3X,MoveEnemy3Y, self.width,self.height)
如果你愿意,我可以编辑这个答案,把整个“EnemySprite”类修改为与自己沟通,并且只需要一个类。
希望这会有所帮助。如果您需要澄清任何事情,请发表评论。 Pygame很有趣。