所以我从一段时间后看到一篇关于子弹的帖子,并决定实施它,因为它最简单所以如果它是你的。谢谢!我收到一条错误,指出“pygame.Surface”对象没有属性'update'这是Galaga和Space Invaders之间的交叉。子弹应该从鼠标位置出来。
import math
import random
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
yellow =( 255, 255, 0)
blue = (0,0,255)
grey= (220, 220,220)
pygame.init()
# Set the width and height of the screen [width,height]
size=[1000,800]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("SPACE INVADERS + GALAGA")
clock = pygame.time.Clock()
background_position = [0,0]
background_image = pygame.image.load("black.jpg").convert()
player_image = pygame.image.load("galaga.jpg").convert()
spaceinvaderenemy = pygame.image.load("spaceinvaderenemy.jpg").convert()
player_image.set_colorkey(black)
x = 0
y = 0
player_image_y = 897
player_image_x = 500
enemy_x = 100
enemy_y = 0
enemy_a = 200
enemy_b = 0
enemy_c = 300
enemy_d = 0
enemy_e = 400
enemy_f = 0
enemy_g = 0
enemy_m = 0
#Second Row
enemy_j = 0
enemy_k = 75
enemy_1 = 100
enemy_2 = 75
enemy_3 = 200
enemy_4 = 75
enemy_5 = 300
enemy_6 = 75
enemy_7 = 400
enemy_8 = 75
enemy_x_direction = "right"
enemy_c_direction = "right"
enemy_a_direction = "right"
enemy_e_direction = "right"
enemy_g_direction = "right"
#Second row
enemy_j_direction = "right"
enemy_1_direction = "right"
enemy_3_direction = "right"
enemy_5_direction = "right"
enemy_7_direction = "right"
done = False
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
class Bullet():
def __init__(self, surface, x_coord, y_coord):
self.surface = surface
self.x = x_coord
self.y = y_coord
return
def update(self, y_amount=5):
self.y += y_amount
self.surface.set_at((self.x, self.y),(255,255,255))
return
bullets_array = []
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
click_sound = pygame.mixer.Sound ("laser5.ogg")
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
click_sound.play()
if event.type == pygame.MOUSEBUTTONDOWN:
bullets_array.append(background_image)
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
yellow =( 255, 255, 0)
blue = (0,0,255)
grey= (220, 220,220)
pygame.init()
# Set the width and height of the screen [width,height]
size=[1000,800]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("SPACE INVADERS + GALAGA")
clock = pygame.time.Clock()
background_position = [0,0]
background_image = pygame.image.load("black.jpg").convert()
player_image = pygame.image.load("galaga.jpg").convert()
spaceinvaderenemy = pygame.image.load("spaceinvaderenemy.jpg").convert()
player_image.set_colorkey(black)
x = 0
y = 0
player_image_y = 897
player_image_x = 500
enemy_x = 100
enemy_y = 0
enemy_a = 200
enemy_b = 0
enemy_c = 300
enemy_d = 0
enemy_e = 400
enemy_f = 0
enemy_g = 0
enemy_m = 0
#Second Row
enemy_j = 0
enemy_k = 75
enemy_1 = 100
enemy_2 = 75
enemy_3 = 200
enemy_4 = 75
enemy_5 = 300
enemy_6 = 75
enemy_7 = 400
enemy_8 = 75
enemy_x_direction = "right"
enemy_c_direction = "right"
enemy_a_direction = "right"
enemy_e_direction = "right"
enemy_g_direction = "right"
#Second row
enemy_j_direction = "right"
enemy_1_direction = "right"
enemy_3_direction = "right"
enemy_5_direction = "right"
enemy_7_direction = "right"
done = False
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
class Bullet():
def __init__(self, surface, x_coord, y_coord):
self.surface = surface
self.x = x_coord
self.y = y_coord
return
def update(self, y_amount=5):
self.y += y_amount
self.surface.set_at((self.x, self.y),(255,255,255))
return
bullets_array = []
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
click_sound = pygame.mixer.Sound("laser5.ogg")
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True= "right"
for bullet in bullets_array:
bullet.update()
screen.blit(background_image, background_position)
screen.blit(spaceinvaderenemy, [enemy_g, enemy_m])
screen.blit(spaceinvaderenemy, [enemy_x, enemy_y])
screen.blit(spaceinvaderenemy, [enemy_a, enemy_b])
screen.blit(spaceinvaderenemy, [enemy_c, enemy_d])
screen.blit(spaceinvaderenemy, [enemy_e, enemy_f])
#Second Row Of Enemies
screen.blit(spaceinvaderenemy, [enemy_j, enemy_k])
screen.blit(spaceinvaderenemy, [enemy_1, enemy_2])
screen.blit(spaceinvaderenemy, [enemy_3, enemy_4])
screen.blit(spaceinvaderenemy, [enemy_5, enemy_6])
screen.blit(spaceinvaderenemy, [enemy_7, enemy_8])
screen.blit(player_image, [player_image_x, player_image_y])
player_position = pygame.mouse.get_pos()
x = player_position[0]
y = player_position[1]
if enemy_c == 801:
enemy_c_direction = "left"
if enemy_c == 306:
enemy_c_direction = "right"
if enemy_c_direction == "right":
enemy_c += 3
if enemy_c_direction == "left":
enemy_c -= 3
if enemy_a == 701:
enemy_a_direction = "left"
if enemy_a == 206:
enemy_a_direction = "right"
if enemy_a_direction == "right":
enemy_a += 3
if enemy_a_direction == "left":
enemy_a -= 3
if enemy_x == 601:
enemy_x_direction = "left"
if enemy_x == 106:
enemy_x_direction = "right"
if enemy_x_direction == "right":
enemy_x += 3
if enemy_x_direction == "left":
enemy_x -= 3
if enemy_g == 501:
enemy_g_direction = "left"
if enemy_g == 6:
enemy_g_direction = "right"
if enemy_g_direction == "right":
enemy_g += 3
if enemy_g_direction == "left":
enemy_g -= 3
if enemy_e == 901:
enemy_e_direction = "left"
if enemy_e == 406:
enemy_e_direction = "right"
if enemy_e_direction == "right":
enemy_e += 3
if enemy_e_direction == "left":
enemy_e -= 3
#SECOND ROW OF ENEMIES
if enemy_j == 501:
enemy_j_direction = "left"
if enemy_j == 6:
enemy_j_direction = "right"
if enemy_j_direction == "right":
enemy_j += 3
if enemy_j_direction == "left":
enemy_j -= 3
if enemy_1 == 601:
enemy_1_direction = "left"
if enemy_1 == 106:
enemy_1_direction = "right"
if enemy_1_direction == "right":
enemy_1 += 3
if enemy_1_direction == "left":
enemy_1 -= 3
if enemy_3 == 701:
enemy_3_direction = "left"
if enemy_3 == 206:
enemy_3_direction = "right"
if enemy_3_direction == "right":
enemy_3 += 3
if enemy_3_direction == "left":
enemy_3 -= 3
if enemy_5 == 801:
enemy_5_direction = "left"
if enemy_5 == 306:
enemy_5_direction = "right"
if enemy_5_direction == "right":
enemy_5 += 3
if enemy_5_direction == "left":
enemy_5 -= 3
if enemy_7 == 901:
enemy_7_direction = "left"
if enemy_7 == 406:
enemy_7_direction = "right"
if enemy_7_direction == "right":
enemy_7 += 3
if enemy_7_direction == "left":
enemy_7 -= 3
print(enemy_e)
screen.blit(player_image, [x, y])
pygame.display.flip()
# Limit to 20 frames per second
clock.tick(50)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()