python - Pygame 碰撞和子弹系统
问题描述
大家好,我在使用 python 中的 pygame 制作的测试游戏中找不到使用子弹的方法。 在此处输入图像描述
子弹被冻结在一个地方(忽略艺术)并且不动
import pygame
pygame.init()
WIDTH = 800 #-------------------Width of the screen-----------------------#
HEIGHT = 600 #------------------Height of the screen------------------------#
WIDTH_player , Height_player = (40,40) #-----------------------------Player Width and height--------------------#
player1_hit = pygame.USEREVENT +1
player2_hit = pygame.USEREVENT +2
#------------------------Main screen-----------------------------------#
Screen = pygame.display.set_mode((WIDTH,HEIGHT))
#--------------------------Locked FPS---------------------------------------#
FPS = 60
vel=5
Vel = 8
Max_Bullet = 10
#--------------------------------(Img location) And scale of the player------------------------------------------------#
player1_img = pygame.image.load(r'P:/python codes/pygame/All contain/Player01.png')
player1 = pygame.transform.scale(player1_img,(WIDTH_player,Height_player))
player2_Img = pygame.image.load(r'P:/python codes/pygame/All contain/player02.png')
player2 = pygame.transform.scale(player2_Img,(WIDTH_player,Height_player))
#----------------------------------IMP ITEMS --------------------------------#
player001 = pygame.Rect(100,300,WIDTH_player,Height_player)
player002 = pygame.Rect(600,300,WIDTH_player,Height_player)
center_Border = pygame.Rect(WIDTH//2-5,0,5,HEIGHT)
#---------------------Making a while loop true and false
run = True
#-------------To make the FPS limit-------------#
clock = pygame.time.Clock()
#----------------------Bullets-------------------------#
Bullet_player1 = []
Bullet_player2 = []
def Handle_bullets(Bullet_player1 , Bullet_player2 , Player001 , player002):
for bullet in Bullet_player1:
bullet.x =+ vel
if player002.collidedict(bullet):
pygame.event.post(pygame.event.Event(player1_hit))
Bullet_player1.remove(bullet)
for bullet in Bullet_player2:
bullet.x += 10
if player001.collidedict(bullet):
pygame.event.post(pygame.event.Event(player2_hit))
Bullet_player2.remove(bullet)
#------------Main while loop--------------#
while run:
#------------------Limit the Fps -------------#
clock.tick(FPS)
#------------------Main event---------------------#
for event in pygame.event.get():
if event.type ==pygame.QUIT:
run = False
#------------------Bullets-------------------------#
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL and len(player001)<Max_Bullet:
bullet = pygame.Rect(player001.x + player001.width , player001.y + player001.height//2 , 10,5)
Bullet_player1 .append(bullet)
if event.key == pygame.K_RCTRL and len(player002)<Max_Bullet :
bullet = pygame.Rect(player002.x , player002.y + player002.height//2 , 10,5)
Bullet_player2 .append(bullet)
Screen.fill((255,255,255))
Screen.blit(player1,(player001.x,player001.y))
Screen.blit(player2,(player002.x,player002.y))
pygame.draw.rect(Screen,(0,0,0), center_Border)
for bullet in Bullet_player1:
pygame.draw.rect(Screen,(255,0,0), bullet)
for bullet in Bullet_player2:
pygame.draw.rect(Screen,(255,0,0), bullet)
#--------------------Key pressed------------------------#
#-------------------For player 01------------------------#
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_w] and player001.y - vel > 0:#Up
player001.y -= vel
if keys_pressed[pygame.K_s] and player001.y + vel + player001.height < HEIGHT:#Down
player001.y += vel
if keys_pressed[pygame.K_a] and player001.x - vel > 0:#Right
player001.x -= vel
if keys_pressed[pygame.K_d] and player001.x + vel + player001.width < center_Border.x :#left
player001.x += vel
#-----------------------For Player 02-------------------------#
if keys_pressed[pygame.K_UP] and player002.y - vel > 0: #UP
player002.y -=vel
if keys_pressed[pygame.K_DOWN] and player002.y + vel + player002.height < HEIGHT:#down
player002.y +=vel
if keys_pressed[pygame.K_RIGHT] and player002.x - vel +player002.width < WIDTH-5:#Right
player002.x += vel
if keys_pressed[pygame.K_LEFT] and player002.x -vel>center_Border.x+center_Border.width:#Left
player002.x -= vel
#-------To update --------#
pygame.display.update()
#退出pygame pygame.quit()
听到是代码,所以请查看代码并帮助我
如果有人可以帮助我,那对我来说意义重大
解决方案
推荐阅读
- jquery - $.validator.unobtrusive.parse($('form')) 上的“Uncaught TypeError: Cannot read property 'parse' of undefined”
- java - 如何在springboot应用程序中注销用户
- swift - UITableView 具有多个具有多行的部分。我需要部分独立运行
- ruby-on-rails - 在rails中将字符串转换为类名
- ios - UICollectionView 快速拖放
- c++ - 从数组中搜索元素的索引
- angular - 从订阅中检索文件路径引用并将其存储到 firebase
- javascript - Node.js '[对象文件]' BLOB
- scala - 我在 rdd 中有一组要按顺序执行的查询
- javascript - React包json加载环境变量