我做了一个游戏,玩家必须躲避被称为敌人的东西。我试过做碰撞,但没有运气,我不知道如何让敌人走得更慢。以下是我目前掌握的情况:
import pygame,random, sys
from pygame.locals import *
from Player import *
from Person import *
from Badies import *
def terminate():
pygame.quit()
sys.exit()
def writeText(window, written, cenX, cenY):
font = pygame.font.Font(None, 48)
text = font.render(written, 1, TEXTCOLOR)
textpos = text.get_rect()
textpos.centerx = cenX
textpos.centery = cenY
window.blit(text, textpos)
return (textpos)
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE: # pressing escape quits
terminate()
return
def playerHitFoes(playerRect, foes):
for f in foes:
if guy.getRect(f['rect']):
return True
return False
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.key.set_repeat(1, 1)
font = pygame.font.SysFont(None, 48)
WINDOWWIDTH = 800
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
FPS = 40
BADDIEMAXSIZE = 35
BADDIEMINSIZE = 35
BADDIEMAXSPEED = 10
BADDIEMINSPEED = 10
ADDNEWFOESRATE = 6
PLAYERMOVERATE = 5
changedRecs=[]
BACKGROUNDCOLOR = (255, 255, 255)
gameWin = False
win=False
score = 0
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
guy = Player(400, 525)
foes = Badies()
playerRect = getRec(guy)
foess = pygame.image.load('Bad.png')
writeText(screen,"How to Play?" , 400,100)
writeText(screen,"*Help Mario dodge the squares", 400,150)
writeText(screen,"*Controls: Up, Down, Left, Right.", 400,200)
writeText(screen,"(Press Any Key to Start.)", 400,250)
pygame.display.update()
waitForPlayerToPressKey()
gameEnd = foes.drawAndCollision(screen, guy)
pygame.display.update()
gameEnd = foes.drawAndCollision(screen, guy)
WHITE = (255,255,255)
screen.fill(WHITE)
pygame.display.update()
while True:
# set up the start of the game
foes= []
score = 0
moveLeft = moveRight = moveUp = moveDown = False
r = s = False
foesAddCounter = 0
while True: # the game loop runs while the game part is playing
score += 1 # increase score
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_LEFT:
guy.moveLeft()
if event.key == K_RIGHT:
guy.moveRight()
if event.key == K_UP:
guy.moveUp()
if event.key == K_DOWN:
guy.moveDown()
if not r and not s:
foesAddCounter += 1
if foesAddCounter == ADDNEWFOESRATE:
foesAddCounter = 0
baddieSize = BADDIEMAXSIZE
newFoes = {'rect': pygame.Rect(random.randint(0, WINDOWWIDTH-baddieSize), 0 - baddieSize, baddieSize, baddieSize),
'speed': BADDIEMAXSPEED,
'surface':pygame.transform.scale(foess, (baddieSize, baddieSize)),
}
foes.append(newFoes)
for f in foes:
if not r and not s:
f['rect'].move_ip(0, f['speed'])
elif reverseCheat:
f['rect'].move_ip(0, -5)
elif slowCheat:
f['rect'].move_ip(0, 1)
for f in foes[:]:
if f['rect'].top > WINDOWHEIGHT:
foes.remove(f)
screen.fill(BACKGROUNDCOLOR)
writeText(screen,"Score: %s" % (score), 10, 0)
guy.draw(screen)
for f in foes:
windowSurface.blit(f['surface'], f['rect'])
pygame.display.update()
if playerHasHitBaddie(playerRect, baddies):
writeText(screen, "Game Over!", 400,300)
break
pygame.display.update()
This next part is the Player File:
import pygame
from pygame.locals import *
class Player:
# set all class variables in the constructor
def __init__(self, newX, newY):
self.x = newX
self.y = newY
# Add images to the list of images
self.images = [ "Player.gif", "PlayerR.gif","PlayerR.gif", "Player.gif"]
# Use this to keep track of which image to use
self.cos = 0
# draw your person with the correct image
def draw(self, window):
window.blit( pygame.image.load( self.images[ self.cos ] ),
(self.x,self.y))
# move person left and set the costume facing left.
def moveLeft(self):
self.x -= 1
self.cos = 3
# move person right and set the costume facing right
def moveRight(self):
self.x += 1
self.cos = 2
# move person up and set the costume facing up
def moveUp(self):
self.y -= 1
self.cos = 1
# move person down and set the costume facing down
def moveDown(self):
self.y += 1
self.cos = 0
def playerHitFoes(playerRect, foes):
for f in foes:
if playerRect.colliderect(f['rect']):
return True
def playerHasHitHeart(playerRect, foes):
for h in heart:
if playerRect.colliderect(h['rect']):
return True
def collide(self, other):
myRec = self.getRec
otherRec = other.getRec()
oRight = otherRec[0] + otherRec[2]
oBottom = otherRec[1] + otherRec[3]
right = myRec[0] + myRec[2]
bottom = myRec[1] + myRec[3]
if (otherRec[0] <= right) and (oRight >= self.x) and (otherRec[1] <= bottom) and (oBottom >= self.y):
return True
return False
def getRec(self):
myRec = pygame.image.load( self.images[ self.cos ] ).get_rect()
return (self.x, self.y, myRec[2], myRec[3])
如果不能访问gif,也不能运行这段代码,我将假设您的问题在于如何检查冲突。你知道吗
也就是说,
colliderect()
函数接受一个参数,它应该是pygame.Rect()
对象。你知道吗意思是当您执行
playerHitFoes
操作时,您应该传递.rect
对象的f['rect']
对象,如下所示:这可能会也可能不会解决您的问题。
但我将谈到我发现的其他一些东西,它们很可能对这个问题并不重要,但可能会给你带来问题。你知道吗
在上面的代码中,
playerRect
从未被使用过,guy
没有被定义(尽管它很可能会把guy = Player()
拉得很好,但这可能会给您带来问题。你知道吗playerHitFoes()
也定义了两次,一次在野外,一次在player函数中。但它从未在代码示例中使用过。你知道吗playerHitFoes
、playerHasHitHeart
和collide?
在代码的任何地方都不会被调用。你知道吗playerHasHitBaddie
用于检查game is over
,但playerHasHitBaddie
从未定义。你知道吗即使我有GIF,这个代码也会给出很多例外情况。
所以我假设这只是你代码的一小部分,你遗漏了很多东西。。所以,要么以上解决了问题-或者你至少需要给我更多的细节。。例如,您使用什么代码来执行实际的碰撞检测?你知道吗
未定义或未使用的事物列表
Badies()
-我们不知道它是什么样子foes.drawAndCollision
playerHasHitBaddie
playerHitFoes
playerHasHitHeart
collide
gameWin
changedRecs
PLAYERMOVERATE
BADDIEMINSPEED
BADDIEMINSIZE
FPS
win
guy
,TEXTCOLOR
(函数中的局部变量假设全局存在)这只是举几个例子。
一个重要的问题是
Badies()
是什么样子,以及如何检查/调用冲突检测。你知道吗相关问题 更多 >
编程相关推荐