Cara Mencegah Tembakan Berbilang Peluru Secara Serentak
Dalam kod asal, berbilang peluru dilepaskan serentak disebabkan fakta bahawa pygame Fungsi .key.get_pressed() mengembalikan Benar selama kekunci ditekan ke bawah. Tingkah laku ini sesuai untuk mengawal pergerakan pemain tetapi bukan untuk menembak peluru, di mana anda mahu hanya satu peluru dilepaskan setiap kali kekunci ditekan.
Untuk menyelesaikan isu ini, kami boleh memanfaatkan pygame.KEYDOWN peristiwa, yang berlaku sekali sahaja apabila kekunci pada mulanya ditekan. Dengan menggunakan acara ini dan bukannya pygame.key.get_pressed(), kami boleh memastikan bahawa hanya satu peluru dilepaskan apabila bar ruang ditekan.
Selain itu, kami boleh melaksanakan mekanisme penyejukan mudah untuk mengelakkan peluru tembakan pantas. menembak. Ini boleh dilakukan dengan memperkenalkan pembolehubah yang dipanggil next_bullet_threshold, yang mewakili masa terawal peluru seterusnya boleh dilepaskan. Setiap kali peluru dilepaskan, kami mengemas kini next_bullet_threshold kepada masa semasa serta tempoh bertenang yang diingini. Dengan menyemak sama ada masa semasa lebih besar daripada next_bullet_threshold, kami boleh menghalang sebarang peluru daripada dilepaskan di luar tempoh bertenang yang diingini.
Berikut ialah kod yang diubah suai dengan mekanisme bertenang yang dilaksanakan:
<code class="python">import pygame pygame.init() red = 255,0,0 blue = 0,0,255 black = 0,0,0 screenWidth = 800 screenHeight = 600 gameDisplay = pygame.display.set_mode((screenWidth,screenHeight)) ## screen width and height pygame.display.set_caption('JUST SOME BLOCKS') ## set my title of the window clock = pygame.time.Clock() class player(): ## has all of my attributes for player 1 def __init__(self,x,y,width,height): self.x = x self.y = y self.height = height self.width = width self.vel = 5 self.left = False self.right = False self.up = False self.down = False class projectile(): ## projectile attributes def __init__(self,x,y,radius,colour,facing): self.x = x self.y = y self.radius = radius self.facing = facing self.colour = colour self.vel = 8 * facing # speed of bullet * the direction (-1 or 1) def draw(self,gameDisplay): pygame.draw.circle(gameDisplay, self.colour , (self.x,self.y),self.radius) ## put a 1 after that to make it so the circle is just an outline def redrawGameWindow(): for bullet in bullets: ## draw bullets bullet.draw(gameDisplay) pygame.display.update() #mainloop player1 = player(300,410,50,70) # moves the stuff from the class (when variables are user use player1.var) bullets = [] run = True next_bullet_threshold = 0 # Initialize the cooldown threshold bullet_cooldown = 500 # Set the desired cooldown period in milliseconds while run == True: clock.tick(27) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False for bullet in bullets: if bullet.x < screenWidth and bullet.x > 0 and bullet.y < screenHeight and bullet.y > 0: ## makes sure bullet does not go off screen bullet.x += bullet.vel else: bullets.pop(bullets.index(bullet)) keys = pygame.key.get_pressed() ## check if a key has been pressed ## red player movement if keys[pygame.K_w] and player1.y > player1.vel: ## check if that key has been pressed down (this will check for w) and checks for boundry player1.y -= player1.vel ## move the shape in a direction player1.up = True player1.down = False if keys[pygame.K_a] and player1.x > player1.vel: ### this is for a player1.x -= player1.vel player1.left = True player1.right = False if keys[pygame.K_s] and player1.y < screenHeight - player1.height - player1.vel: ## this is for s player1.y += player1.vel player1.down = True player1.up = False if keys[pygame.K_d] and player1.x < screenWidth - player1.width - player1.vel: ## this is for d player1.x += player1.vel player1.right = True player1.left = False if keys[pygame.K_SPACE]: # shooting with the space bar current_time = pygame.time.get_ticks() if current_time >= next_bullet_threshold: # Check the cooldown timer if player1.left == True: ## handles the direction of the bullet facing = -1 else: facing = 1 if len(bullets) < 5: ## max amounts of bullets on screen bullets.append(projectile(player1.x + player1.width //2 ,player1.y + player1.height//2,6,black,facing)) ##just like calling upon a function next_bullet_threshold = current_time + bullet_cooldown # Update cooldown timer ## level gameDisplay.fill((0,255,0)) ### will stop the shape from spreading around and will have a background pygame.draw.rect(gameDisplay,(red),(player1.x,player1.y,player1.width,player1.height)) ## draw player pygame.display.update() redrawGameWindow() pygame.quit()</code>
Atas ialah kandungan terperinci Bagaimana untuk Melaksanakan Mekanisme Cooldown untuk Mencegah Tembakan Peluru Berlebihan?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!