单击以执行分配给它的代码时,CTRL 不起作用
CTRL not working when clicked to perform the code allocated to it
所以代码可以正常工作,但我不知道发生了什么,所以如果您单击左 CTRL 或右 CTRL,red/yellow 应该会发射子弹,但没有发生。所以我用 print 来检查代码是否会在 red/yellow_bullet 列表中附加一些东西,但它没有,所以是我这边的问题还是代码中的问题???
请帮忙
代码:
import pygame
import os
from pygame import event #opereting system
WIDTH, HEIGHT = 900, 500 #this is to make the WINDOW
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Maisa Ahmed")
DARK_GREEN = (00, 64, 00)
BLACK = (0, 0, 0)
BLOOD_RED = (136, 8, 8)
YELLOW = (255, 255, 0)
boarder = pygame.Rect(WIDTH//2-5, 0, 10, HEIGHT)
FPS = 60
SPACESHIP_WIDTH , SPACESHIP_HEIGHT = 55,40
velocity = 5
Bullet_velocity = 7
MAX_BULLETS = 5
YELLOW_HITS = pygame.USEREVENT + 1
RED_HITS = pygame.USEREVENT + 2
#importing images
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)
RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', "spaceship_red.png"))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)),270)
def draw_window(red ,yellow, red_bullets, yellow_bullets):
WIN.fill(DARK_GREEN)
pygame.draw.rect(WIN, BLACK, boarder)
WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))
for bullet in red_bullets:
pygame.draw.rect(WIN, BLOOD_RED, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
pygame.display.update()
def yellow_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - velocity > 0:
yellow.x -= velocity
if keys_pressed[pygame.K_d] and yellow.x + velocity + yellow.width < boarder.x:
yellow.x += velocity
if keys_pressed[pygame.K_w] and yellow.y - velocity > 0:
yellow.y -= velocity
if keys_pressed[pygame.K_s] and yellow.y + velocity + yellow.height < HEIGHT - 10:
yellow.y += velocity
def red_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - velocity > boarder.x + boarder.width: # left
red.x -= velocity
if keys_pressed[pygame.K_RIGHT] and red.x + velocity + red.width < WIDTH: # right
red.x += velocity
if keys_pressed[pygame.K_UP] and red.y - velocity > 0:# up
red.y -= velocity
if keys_pressed[pygame.K_DOWN] and red.y + velocity + red.height < HEIGHT - 10: # down
red.y += velocity
def handle_bullets(yellow_bullets, red_bullets, yellow, red):
for bullet in yellow_bullets:
bullet.x += Bullet_velocity
if red.colliderect(bullet):
pygame.event.post(event.Event(RED_HITS))
yellow_bullets.remove
for bullet in red_bullets:
bullet.x -= Bullet_velocity
if yellow.colliderect(bullet):
pygame.event.post(event.Event(YELLOW_HITS))
red_bullets.remove
def main():
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) # x,y , width , height
red_bullets = []
yellow_bullets = []
clock = pygame.time.Clock() # create a clock object which can be used to keep track of time
run = True
while run: #event loop
clock.tick(FPS) #ensures that you will never go above the FPS you set the game on
for event in pygame.event.get():
if event.type == pygame.QUIT: #.QUIT to end pygame in the loop
run = False
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y + yellow.height//2 - 2,10,5) # so it goes from the middle
yellow_bullets.append(bullet)
if event.type == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2,10,5) # so it goes from the middle
red_bullets.append(bullet)
print(red_bullets, yellow_bullets)
keys_pressed = pygame.key.get_pressed() # this tells us what keys are being clicked every 1
yellow_movement(keys_pressed, yellow)
red_movement(keys_pressed, red)
handle_bullets(yellow_bullets, red_bullets, yellow, red)
draw_window(red, yellow, red_bullets, yellow_bullets) #you took the yellow and red to the Drawing functions
pygame.quit #.quit to end pygame in general
if __name__ == "__main__": #doesnt matter
main()
将 event.type == pygame.K_LCTRL
更改为 event.key == pygame.K_LCTRL
,将 event.type == pygame.K_RCTRL
更改为 event.key == pygame.K_RCTRL
所以代码可以正常工作,但我不知道发生了什么,所以如果您单击左 CTRL 或右 CTRL,red/yellow 应该会发射子弹,但没有发生。所以我用 print 来检查代码是否会在 red/yellow_bullet 列表中附加一些东西,但它没有,所以是我这边的问题还是代码中的问题??? 请帮忙
代码:
import pygame
import os
from pygame import event #opereting system
WIDTH, HEIGHT = 900, 500 #this is to make the WINDOW
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Maisa Ahmed")
DARK_GREEN = (00, 64, 00)
BLACK = (0, 0, 0)
BLOOD_RED = (136, 8, 8)
YELLOW = (255, 255, 0)
boarder = pygame.Rect(WIDTH//2-5, 0, 10, HEIGHT)
FPS = 60
SPACESHIP_WIDTH , SPACESHIP_HEIGHT = 55,40
velocity = 5
Bullet_velocity = 7
MAX_BULLETS = 5
YELLOW_HITS = pygame.USEREVENT + 1
RED_HITS = pygame.USEREVENT + 2
#importing images
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)
RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', "spaceship_red.png"))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)),270)
def draw_window(red ,yellow, red_bullets, yellow_bullets):
WIN.fill(DARK_GREEN)
pygame.draw.rect(WIN, BLACK, boarder)
WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))
for bullet in red_bullets:
pygame.draw.rect(WIN, BLOOD_RED, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
pygame.display.update()
def yellow_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - velocity > 0:
yellow.x -= velocity
if keys_pressed[pygame.K_d] and yellow.x + velocity + yellow.width < boarder.x:
yellow.x += velocity
if keys_pressed[pygame.K_w] and yellow.y - velocity > 0:
yellow.y -= velocity
if keys_pressed[pygame.K_s] and yellow.y + velocity + yellow.height < HEIGHT - 10:
yellow.y += velocity
def red_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - velocity > boarder.x + boarder.width: # left
red.x -= velocity
if keys_pressed[pygame.K_RIGHT] and red.x + velocity + red.width < WIDTH: # right
red.x += velocity
if keys_pressed[pygame.K_UP] and red.y - velocity > 0:# up
red.y -= velocity
if keys_pressed[pygame.K_DOWN] and red.y + velocity + red.height < HEIGHT - 10: # down
red.y += velocity
def handle_bullets(yellow_bullets, red_bullets, yellow, red):
for bullet in yellow_bullets:
bullet.x += Bullet_velocity
if red.colliderect(bullet):
pygame.event.post(event.Event(RED_HITS))
yellow_bullets.remove
for bullet in red_bullets:
bullet.x -= Bullet_velocity
if yellow.colliderect(bullet):
pygame.event.post(event.Event(YELLOW_HITS))
red_bullets.remove
def main():
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) # x,y , width , height
red_bullets = []
yellow_bullets = []
clock = pygame.time.Clock() # create a clock object which can be used to keep track of time
run = True
while run: #event loop
clock.tick(FPS) #ensures that you will never go above the FPS you set the game on
for event in pygame.event.get():
if event.type == pygame.QUIT: #.QUIT to end pygame in the loop
run = False
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y + yellow.height//2 - 2,10,5) # so it goes from the middle
yellow_bullets.append(bullet)
if event.type == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2,10,5) # so it goes from the middle
red_bullets.append(bullet)
print(red_bullets, yellow_bullets)
keys_pressed = pygame.key.get_pressed() # this tells us what keys are being clicked every 1
yellow_movement(keys_pressed, yellow)
red_movement(keys_pressed, red)
handle_bullets(yellow_bullets, red_bullets, yellow, red)
draw_window(red, yellow, red_bullets, yellow_bullets) #you took the yellow and red to the Drawing functions
pygame.quit #.quit to end pygame in general
if __name__ == "__main__": #doesnt matter
main()
将 event.type == pygame.K_LCTRL
更改为 event.key == pygame.K_LCTRL
,将 event.type == pygame.K_RCTRL
更改为 event.key == pygame.K_RCTRL