如何解决每次矩形与其他矩形碰撞时,我的分数都会增加很多
所以我试图做到这一点,如果我的矩形与另一个矩形/点碰撞,它会将点 y 位置从矩形移开,并将一个添加到我的分数中。我遇到的问题是,当我的矩形与我的点 https://gyazo.com/d9e4167f749f2a672fe7f713ce5bab2e 碰撞时,我的分数将增加两个而不是一个。我曾尝试使用真假陈述使其仅在我的分数中添加一个而不是两个,但这也不起作用。我也尝试过使用计时器来告诉它何时添加,但这对我不起作用。
这是我试过的
if point:
score += 1
text = font.render(""+str(score),True,(0,0))
point = False
# Making the rectangles get off the screen for a short amount of time
if Ptimer > 0:
Ptimer += 1
point1.y = -200
if Ptimer >= 15:
Ptimer = 0
if Ptimer2 > 0:
Ptimer2 += 1
point2.y = -200
if Ptimer2 >= 20:
Ptimer2 = 0
# The player collideing with the point's
if playerman.rect.colliderect(point1.rect):
Ptimer = 1
point = True
if playerman.rect.colliderect(point2.rect):
Ptimer2 = 1
point = True
我的完整代码
import pygame,random
pygame.init()
width = 500
height = 600
# Screen width and height
window = pygame.display.set_mode((width,height))
# Name of the Screen
pygame.display.set_caption("Game")
# The background
background = pygame.image.load("img/BG_2.png").convert_alpha()
# Making both peipes separate
Pipe_distance = 830
# part of making background shift
bg_shift = 0
# player class
class Player:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.normal = [pygame.image.load("img/normal_fish" + str(i) + ".png") for i in range(1,7)]
self.up = [pygame.image.load("img/Up_fish" + str(i) + ".png") for i in range(1,7)]
self.down = [pygame.image.load("img/Down_fish" + str(i) + ".png") for i in range(1,7)]
self.speed = 5
self.JumpCount = False
self.isJump = 10
self.fall = 0
self.direction = "up"
self.direction = "down"
self.direction = "normal"
self.next_frame_time = 0
self.fps = 10
self.normal = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.normal]
self.up = [pygame.transform.scale(image,image.get_height()//5)) for image in self.up]
self.down = [pygame.transform.scale(image,image.get_height()//5)) for image in self.down]
self.clock = pygame.time.Clock()
self.anim_index = 0
self.rect = pygame.Rect(x,height)
def get_rect(self):
self.rect.topleft = (self.x,self.y)
return self.rect
def draw(self):
self.rect.topleft = (self.x,self.y)
if self.direction == "normal":
image_list = self.normal
elif self.direction == "up":
image_list = self.up
elif self.direction == "down":
image_list = self.down
# Telling when to change frames
time_Now = pygame.time.get_ticks()
if (time_Now > self.next_frame_time):
# The time until next frame
inter_time_delay = 1000 // self.fps
self.next_frame_time = time_Now + inter_time_delay
# Changing it to the next frame
self.anim_index += 1
if self.anim_index >= len(image_list):
self.anim_index = 0
if self.anim_index >= len(image_list):
self.anim_index = 0
player_image = image_list[self.anim_index]
player_rect = player_image.get_rect(center = self.get_rect().center)
player_rect.centerx
player_rect.centery
window.blit(player_image,player_rect)
# Pipe class
class Pipe:
def __init__(self,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 3
self.pipe = pygame.image.load("img/chain.png").convert_alpha()
self.pipe = pygame.transform.scale(self.pipe,(self.pipe.get_width()//2,self.pipe.get_height()//2))
self.rect = pygame.Rect(x,height)
self.anim_index = 0
def get_rect(self):
self.rect.topleft = (self.x,self.y)
pipe_rect = self.pipe.get_rect(center = self.get_rect().center)
pipe_rect.centerx += 10
pipe_rect.centery += 595
window.blit(self.pipe,pipe_rect)
# Pipe2 class
class Pipe2:
def __init__(self,self.y)
pipe_rect = self.pipe.get_rect(center = self.get_rect().center)
pipe_rect.centerx += 26
pipe_rect.centery -= 635
window.blit(self.pipe,pipe_rect)
# Partical class
class Partical:
def __init__(self,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.par = [pygame.image.load("img/Partical" + str(i) + ".png") for i in range(1,8)]
self.rect = pygame.Rect(x,height)
self.anim_index = 0
self.next_frame_time = 0
self.fps = 10
self.play = False
self.clock = pygame.time.Clock()
self.direction = "par"
def draw(self):
self.rect.topleft = (self.x,self.y)
if self.direction == "par":
image_list = self.par
# Is it time to show next frame?
time_Now = pygame.time.get_ticks()
if (time_Now > self.next_frame_time):
# Time until the next frame
inter_time_delay = 750 // self.fps
self.next_frame_time = time_Now + inter_time_delay
# Show the next frame
if self.play:
self.anim_index += 1
if self.anim_index >= len(image_list):
self.anim_index = 0
if self.play:
if self.anim_index >= len(image_list):
self.anim_index = 0
par_image = image_list[self.anim_index]
window.blit(par_image,self.rect)
# Point class
class Point:
def __init__(self,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 3
self.rect = pygame.Rect(x,height)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# Color
white = (255,255,255)
green = (0,0)
blue = (0,255)
# Calss's cords,size,and color
playerman = Player(200,250,40,white)
pipe1 = Pipe(350,900,60,700,green)
pipe2 = Pipe2(350,-900,green)
pipe3 = Pipe(720,9900,white)
pipe4 = Pipe2(720,-9900,blue)
par = Partical(-300,350,50,blue)
point1 = Point(400,-300,70,white)
point2 = Point(770,white)
# All my list
pipes = [pipe1,pipe3]
pipes2 = [pipe2,pipe4]
particals = [par]
points = [point1,point2]
# Point system
font = pygame.font.Font("img/CAT.ttf",60)
score = 0
text = font.render(""+str(score),0))
textRect = text.get_rect()
textRect.center = ((250,60))
# displaying class's in main loop
def redrawwindow():
window.fill((0,0))
window.blit(background,0))
# Part of making background move
bg_width = background.get_width()
bg_offset = bg_shift % bg_width
window.blit(background,(-bg_offset,0))
window.blit(background,(bg_width - bg_offset,0))
# Drawing my classes and other things
playerman.draw()
for Pipe in pipes:
Pipe.draw()
for Pipe2 in pipes2:
Pipe2.draw()
for Partical in particals:
Partical.draw()
for Point in points:
Point.draw()
window.blit(text,textRect)
# To make the game more difuclt
ptimer = 0
# for playing sprite when player jumps
Jumping = 0
# Making it so the rectangle dose not come for a whilw
Ptimer = 0
Ptimer2 = 0
point = False
fps = 35
clock = pygame.time.Clock()
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# Making most of my classes move
for Pipe in pipes:
Pipe.x -= Pipe.speed
for Pipe2 in pipes2:
Pipe2.x -= Pipe2.speed
for Pipe2 in pipes2:
if Pipe2.x <= -200:
Pipe2.x = 550
for Point in points:
Point.x -= Point.speed
for Point in points:
if Point.x <= -200:
Point.x = 550
# Moving backgrond
bg_shift += 2
# Randomizing pipe
for Pipe in pipes:
if Pipe.x <= -200:
Pipe.x = 550
Pipe.y = random.randint(150,500)
pipe2.y = pipe1.y - Pipe_distance
pipe4.y = pipe3.y - Pipe_distance
point1.y = pipe1.y - 90
point2.y = pipe3.y - 90
if point:
score += 1
text = font.render(""+str(score),0))
point = False
# Making the rectangles get off the screen for a short amount of time
if Ptimer > 0:
Ptimer += 1
point1.y = -200
if Ptimer >= 15:
Ptimer = 0
if Ptimer2 > 0:
Ptimer2 += 1
point2.y = -200
if Ptimer2 >= 20:
Ptimer2 = 0
# The player collideing with the point's
if playerman.rect.colliderect(point1.rect):
Ptimer = 1
point = True
if playerman.rect.colliderect(point2.rect):
Ptimer2 = 1
point = True
# Making the game faster
ptimer += 1
if ptimer == 3000:
for Pipe in pipes:
for Pipe2 in pipes2:
for Point in points:
Pipe.speed += 1
Pipe2.speed += 1
Point.speed += 1
bg_shift += 1
if ptimer == 6000:
for Pipe in pipes:
for Pipe2 in pipes2:
for Point in points:
Pipe.speed += 1
Pipe2.speed += 1
Point.speed += 1
bg_shift += 3
# If the player collides with the chain
for Pipe in pipes:
if playerman.rect.colliderect(Pipe.rect):
print("collide")
for Pipe2 in pipes2:
if playerman.rect.colliderect(Pipe2.rect):
print("collide2")
# Mostly about player direction
if Jumping > 0:
Jumping += 1
playerman.direction = "up"
for Partical in particals:
Partical.x = playerman.x + 10
Partical.y = playerman.y + 25
Partical.play = True
if Jumping >= 15:
playerman.direction = "normal"
Partical.y = -200
Partical.anim_index = 0
if Jumping >= 20:
Jumping = 0
playerman.direction = "down"
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
Jumping = 1
collide = False
playerman.y += playerman.speed
# bird moving
if not playerman.isJump:
# [...]
# the bird is allowed to jump even if it is not colliding:
if keys[pygame.K_SPACE]:
playerman.isJump = True
if collide:
playerman.fall = 0
else:
if playerman.JumpCount > 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
playerman.JumpCount -= 1
else:
playerman.JumpCount = 10
# if K_SPACE is pressed,then the bird keeps jumping
if not keys[pygame.K_SPACE]:
playerman.isJump = False
redrawwindow()
pygame.display.update()
pygame.quit()
解决方法
当您将对象移出可见区域时,您更改了该对象的 y
属性:
point1.y = -200
但是您使用 rect
属性进行碰撞测试:
if playerman.rect.colliderect(point1.rect):
因此,在执行下一次碰撞测试之前,您需要在更改 rect
属性后更新 y
属性:
if Ptimer > 0:
Ptimer += 1
point1.y = -200
point1.rect.topleft = (point1.x,point1.y)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。