我是python的新手,只学习了最基本的代码,但是我的学校希望我们编写游戏代码。即使我不应该从其他地方获得大多数代码,因为我们尚未学习编写游戏代码所需的95%的信息。
我仍然需要向游戏中添加更多内容,例如滚动地图/相机,钥匙和需要钥匙才能解锁的门,但我认为这会问得太多,所以我只想问最重要的部分是敌人的精灵。
目标:
添加跟随玩家的敌方精灵
简化代码(删除/缩短不必要的代码。)
改进我的游戏(感谢您提出的建议!)
import pygame
import random
import os
import time
pygame.init()
pygame.mixer.init()
clock=pygame.time.Clock()
win = pygame.display.set_mode((800,800))
pygame.display.set_caption("Essential Python CA1")
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
class Player(pygame.sprite.Sprite):
# Set speed vector
move_x = 0
move_y = 0
walls = None
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20, 20])
self.image.fill(red)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
def move(self, x, y):
self.move_x += x
self.move_y += y
def update(self):
self.rect.x += self.move_x
block_collide = pygame.sprite.spritecollide(self, self.walls, False)
for block in block_collide:
if self.move_x > 0:
self.rect.right = block.rect.left
else:
self.rect.left = block.rect.right
self.rect.y += self.move_y
block_collide = pygame.sprite.spritecollide(self, self.walls, False)
for block in block_collide:
if self.move_y > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(red)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
all_sprite_list = pygame.sprite.Group()
wall_list = pygame.sprite.Group()
wall = Wall(0, 0, 10, 800)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(40, 40, 10, 75)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(50, 40, 190, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(790, 10, 10, 800)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(10, 0, 800, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(10, 790, 800, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(50, 750, 170, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(40, 145, 10, 615)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(80, 710, 310, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(250, 750, 110, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(390, 680, 10, 80)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(400, 750, 200, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(430, 710, 10, 50)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(470, 710, 200, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(630, 750, 130, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(750, 40, 10, 720)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(550, 40, 210, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(270, 40, 250, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(410, 80, 310, 50)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(80, 80, 300, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(370, 40, 10, 90)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(130, 120, 240, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(300, 160, 450, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(50, 160, 220, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(700, 710, 50, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(80, 670, 670, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(80, 160, 10, 510)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(80, 200, 100, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(210, 200, 120, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(270, 200, 10, 90)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(120, 240, 150, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(80, 280, 250, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(360, 200, 100, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(310, 240, 150, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(460, 160, 10, 130)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(360, 280, 100, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
# Create the player paddle object
player = Player(10, 10)
player.walls = wall_list
all_sprite_list.add(player)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.move(-1, 0)
elif event.key == pygame.K_RIGHT:
player.move(1, 0)
elif event.key == pygame.K_UP:
player.move(0, -1)
elif event.key == pygame.K_DOWN:
player.move(0, 1)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.move(1, 0)
elif event.key == pygame.K_RIGHT:
player.move(-1, 0)
elif event.key == pygame.K_UP:
player.move(0, 1)
elif event.key == pygame.K_DOWN:
player.move(0, -1)
all_sprite_list.update()
win.fill(black)
all_sprite_list.draw(win)
pygame.display.flip()
clock.tick(100)
pygame.quit()