我的代码
import pygame
import time
pygame.init()
display_width=640
display_height=480
gameDisplay=pygame.display.set_mode((display_width,display_height))
clock=pygame.time.Clock()
pygame.display.set_caption("Slide!")
pygame.display.update()
background=pygame.image.load("background.png")
walkup=pygame.image.load("up.png")
walkleft=pygame.image.load("left.png")
walkright=pygame.image.load("right.png")
walkdown=pygame.image.load("down.png")
snowball=pygame.image.load("snowball.png")
direction=walkdown
black=(0,0,0)
gameExit=False
fps=60
clock.tick(fps)
font=pygame.font.SysFont(None, 25)
ball_x=display_width/3
ball_y=display_height/3
location_x=display_width/2
location_y=display_height/2
moveY=0
moveX=0
step_size=5
class maincharacter (pygame.sprite.Sprite):
def __init__(self,pos,*groups):
pygame.sprite.Sprite.__init__(self)
self.image=direction
self.rect=self.image.get_rect(topleft=pos)
class thesnowball (pygame.sprite.Sprite):
def __init__(self,pos,*groups):
pygame.sprite.Sprite.__init__(self)
self.image=snowball
self.rect=self.image.get_rect(topleft=pos)
def scoreCount(msg,color):
printscore=font.render(msg,True,color)
gameDisplay.blit(printscore,[20,20])
def messagetoscreen(msg,color):
screen_text=font.render(msg,True,color)
gameDisplay.blit(screen_text,[display_width/5,display_height/2])
def gameloop():
direction=walkdown
timer=1
startTime=time.time()
gameExit=False
gameOver=False
location_x=display_width/2
location_y=display_height/2
moveY=0
moveX=0
while not gameExit:
while gameOver==True:
messagetoscreen("Press C to continue or Q to quit !",black)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_q:
gameExit=True
gameOver=False
if event.key == pygame.K_c:
gameloop()
for event in pygame.event.get():
if event.type==pygame.QUIT:
gameExit=True
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_a:
direction=walkleft
moveX= -step_size
moveY=0
if event.key==pygame.K_d:
direction=walkright
moveX=step_size
moveY=0
if event.key == pygame.K_s:
direction=walkdown
moveY=step_size
moveX=0
if event.key==pygame.K_w:
direction=walkup
moveY=-step_size
moveX=0
if location_x>=(display_width-40) or location_x<0 or location_y>=(display_height-30) or location_y<-15:
gameOver=True
secondsPassed=time.time() - startTime
if timer==(int(secondsPassed)):
print(timer)
timer+=1
gameDisplay.blit(snowball,(ball_x,ball_y))
gameDisplay.blit(direction,(location_x,location_y))
gameDisplay.blit(background,(0,0))
scoreCount("Score: "+str(int(secondsPassed)),black)
location_y+=moveY
location_x+=moveX
clock.tick(fps)
pygame.display.update()
pygame.quit()
quit()
gameloop()
正如你所看到的,我有游戏的基础。目前我所拥有的只是一个雪球,但最终的目标是让它在屏幕上移动并让玩家避开它。
我想使用pygame.sprite.spritecollide或pygame.sprite.collide_rect,但我不确定如何做到这一点,因为出现了很多错误,并且确实没有任何用处关于如何做的教程。
为了清楚起见,我已经制作了类,我无法将其实现为精灵组,或者进入collide_rect方法
感谢