gpt4 book ai didi

python - 如何让敌人出现?

转载 作者:行者123 更新时间:2023-12-01 08:46:38 25 4
gpt4 key购买 nike

当游戏第一次开始时,玩家和敌人都会出现,但当玩家移动时,敌人就会消失。

import pygame
import os
import random
from pygame.locals import * # Constants
import math
import sys
import random

pygame.init()

screen=pygame.display.set_mode((1280,720)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255)) # fill the background white
White = (255,255,255)

screen.blit(background, (0, 0))

class Player(pygame.sprite.Sprite):

def __init__(self):

self.rect = pygame.draw.rect(screen, (0,0,128), (50,560,50,25)) #(colour)(x-position,y-position,width,height)
self.dist = 100

def draw_rect(self,x,y): # This is my code which should make the player move
screen.blit(background, (0, 0)) #If this isn't included then when the rectangle moves it's old positon will still be on the screen
self.rect = self.rect.move(x*self.dist, y*self.dist); pygame.draw.rect(screen, (0, 0, 128), self.rect)
pygame.display.update()

def handle_keys(self): # code to make the character move when the arrow keys are pressed
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.draw_rect(-0.05,0)
elif keys[K_RIGHT]:
self.draw_rect(0.05,0)
elif keys[K_UP]:
self.draw_rect(0,-0.05)
elif keys[K_DOWN]:
self.draw_rect(0,0.05)
elif keys[K_SPACE]:
self.draw_rect(0.4,-0.9)
if self.rect.right > 1280: # These are to stop the player from going out of the boundary
self.rect.right = 1280
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > 720:
self.rect.bottom = 720
if self.rect.top < 0:
self.rect.top = 0

玩家的移动效果非常好。

class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
def __init__(self):
x = random.randint(50,450)
self.rect = pygame.draw.rect(screen, (128,0,0), (300,x,50,25))

敌人只是在随机位置生成,并且当玩家四处移动时仍然应该出现。我尝试做一些事情,但没有成功。

clock = pygame.time.Clock()  # A clock to limit the frame rate.
player = Player()
enemy = Enemy()

def main(): #my main loop
running = True
while running:
player.handle_keys()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
clock.tick(60) # Limit the frame rate to 60 FPS.
pygame.display.flip() #updates the whole screen

if __name__ == '__main__': main()

最佳答案

这是因为你只绘制了一次敌人的Rect:在Enemy__init__函数中。此外,您还可以在 Player 类的不同位置进行大量绘图,例如清除屏幕。别再这样做了。

您已经子类化 Sprite ,因此请按预期使用此类。 Sprite 有一个 imagerect 来定义它们的外观和位置,您可以将它们添加到 Group 中。它负责调用它们的 update 函数并将它们绘制到屏幕上。

您的代码应如下所示:

import pygame
import random
from pygame.locals import * # Constants

pygame.init()

screen=pygame.display.set_mode((1280,720)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255)) # fill the background white

class Player(pygame.sprite.Sprite):

def __init__(self):
super().__init__()
self.image = pygame.Surface((50,25))
self.image.fill((0,0,128))
self.rect = self.image.get_rect(topleft=(50,560))
self.dist = 100

def update(self): # code to make the character move when the arrow keys are pressed

keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.rect.move_ip(-1,0)
elif keys[K_RIGHT]:
self.rect.move_ip(1,0)
elif keys[K_UP]:
self.rect.move_ip(0,-1)
elif keys[K_DOWN]:
self.rect.move_ip(0,1)
self.rect.clamp_ip(screen_rect)

class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
def __init__(self):
super().__init__()
x = random.randint(50,450)
self.image = pygame.Surface((50,25))
self.image.fill((128,0,0))
self.rect = self.image.get_rect(topleft=(300, x))

clock = pygame.time.Clock() # A clock to limit the frame rate.
player = Player()
enemy = Enemy()
sprites = pygame.sprite.Group(player, enemy)

def main(): #my main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sprites.update()
screen.blit(background, (0, 0))
sprites.draw(screen)
clock.tick(60) # Limit the frame rate to 60 FPS.
pygame.display.flip() #updates the whole screen

if __name__ == '__main__':
main()

关于python - 如何让敌人出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53281253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com