【发布时间】:2021-09-29 13:52:28
【问题描述】:
所以我正在观看 Teach with Tim pygame 90 分钟,我在 1:24:39。当我运行代码时,红色船的健康在我射击它时会持续下降(通过黄色船并且当红色船撞到黄色船时),直到我移动其中一艘船或在我只射击另一艘船时按下射击。出了什么问题有人可以帮忙吗? 我认为问题出在这部分,但我不确定:
if event.type == red_hit:
red_health -= 1
if event.type == yellow_hit:
yellow_health -= 1
import pygame
import os
import time
pygame.font.init()
width , height = 900 , 500
dispay_surface = pygame.display.set_mode((width, height))
pygame.display.set_caption("first game")
teal = (153,255,255)
light_green = (102,255,102)
BLACK = (0,0,0)
RED = (255,0,0)
YELLOW = (255,255,0)
BORDER = pygame.Rect(width//2,0,10,height)
health_font = pygame.font.SysFont('comicsans', 40)
fps = 60
VEL = 5
BULLET_VEL = 7
max_bullet = 10
spaceship_width, spaceship_height = 55, 40
yellow_hit = pygame.USEREVENT +1
red_hit = pygame.USEREVENT +2
#making the space ship and rotating it correctly
yellow_spaceship_image = pygame.image.load(
os.path.join('Assets','spaceship_yellow.png'))
yellow_spaceship_nr = pygame.transform.scale(
yellow_spaceship_image, (spaceship_width, spaceship_height))
yellow_spaceship = pygame.transform.rotate(yellow_spaceship_nr, 90)
red_spaceship_image = pygame.image.load(
os.path.join('Assets','spaceship_red.png'))
red_spaceship_nr= pygame.transform.scale(
red_spaceship_image, (spaceship_width, spaceship_height))
red_spaceship = pygame.transform.rotate(red_spaceship_nr, -90)
space_background = pygame.transform.scale(pygame.image.load(os.path.join('Assets','space.png')),(width,height))
def draw_window(red,yellow,red_bullet,yellow_bullet,red_health,yellow_health):
dispay_surface.blit(space_background,(0,0))
#showing the health
red_health_text = health_font.render("health: " + str(red_health), 1 , teal)
yellow_health_text = health_font.render("health: " + str(yellow_health), 1, teal)
dispay_surface.blit(red_health_text,(width - red_health_text.get_width()- 10 , 10))
dispay_surface.blit(yellow_health_text, (10 , 10))
#use to draw the ship
dispay_surface.blit(yellow_spaceship,(yellow.x, yellow.y))
dispay_surface.blit(red_spaceship,(red.x, red.y))
#drawing a border so the 2 spaceship can't go on top of each other
pygame.draw.rect(dispay_surface,BLACK,BORDER)
for bullet in red_bullet:
pygame.draw.rect(dispay_surface,RED,bullet)
for bullet in yellow_bullet:
pygame.draw.rect(dispay_surface,YELLOW,bullet)
pygame.display.update()
#getting yellow ship to move when player input
def yellow_mov(keys_pressed,yellow):
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: #right
yellow.x += VEL
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: #left
yellow.x -= VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: #up
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y - VEL + yellow.height < height - 15 : #down
yellow.y += VEL
#getting red to move
def red_mov(keys_pressed,red):
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < width: #right
red.x += VEL
if keys_pressed[pygame.K_LEFT] and red.x - VEL >BORDER.x + BORDER.width: #left
red.x -= VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0: #up
red.y -= VEL
if keys_pressed[pygame.K_DOWN] and red.y - VEL + red.height < height - 15: #down
red.y += VEL
def handle_bullet(yellow_bullet,red_bullet,yellow,red):
for bullet in yellow_bullet:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(red_hit))
yellow_bullet.remove(bullet)
elif bullet.x > width:
yellow_bullet.remove(bullet)
for bullet in red_bullet:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(red_hit))
red_bullet.remove(bullet)
elif bullet.x < 0 :
red_bullet.remove(bullet)
#keep window running
def main():
#determin the player position
red = pygame.Rect(700,300,spaceship_width,spaceship_height)
yellow = pygame.Rect(100,300,spaceship_width,spaceship_height)
red_bullet = []
yellow_bullet = []
red_health = 10
yellow_health = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#Shoot the bullet
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL and len(yellow_bullet) < max_bullet:
bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 , 10 ,5)
yellow_bullet.append(bullet)
if event.key == pygame.K_RCTRL and len(red_bullet) < max_bullet:
bullet = pygame.Rect(red.x , red.y + red.height//2 , 10 ,5)
red_bullet.append(bullet)
if event.type == red_hit:
red_health -= 1
if event.type == yellow_hit:
yellow_health -= 1
if red_health <=0:
winner_text = "yellow wins"
if yellow_health <=0:
winner_text = "red wins"
keys_pressed = pygame.key.get_pressed()
yellow_mov(keys_pressed,yellow)
red_mov(keys_pressed,red)
handle_bullet(yellow_bullet,red_bullet,yellow,red)
draw_window(red,yellow,red_bullet,yellow_bullet,red_health,yellow_health)
pygame.quit()
if __name__ == '__main__':
main()
【问题讨论】:
-
您的 if 语句在 for 循环之外。缩进和一致性很重要。
标签: python python-3.x pygame