用pygam中的函数移动精灵

2024-04-20 05:00:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用spawn()类在敌人中繁殖,在spawn(0)类中,我尝试使用move()函数移动敌人。当我运行程序时,僵尸会繁殖,但不会移动。你知道吗

下面是完整的代码,如果有人想知道。我只是在spawn()类上有问题,其他什么都没有。你知道吗

import pygame
import time
import random
import math
from pygame.locals import *

pygame.init()
#################################################################################

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
black = (0,0,0)
white = (255,255,255)
grey = (125,125,125)

#################################################################################
leng = 1200
wid = 600
FPS = 60
size = 30

clock = pygame.time.Clock()
font = pygame.font.SysFont('calibri',20)

def message(msg,colour,loc):
    screen_text = font.render(msg, True, colour)
    screen.blit(screen_text, loc)

def rot_center(image, angle):
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image


def quit():
    pygame.quit()

screen=pygame.display.set_mode((leng,wid),0,32)
background = pygame.image.load("background.png")
################################################################################
def gameLoop():

    class spawn:

        def __init__(self,x,y, px,py,angle):

            self.x=x

            self.y=y

            self.px = px

            self.py = py

            self.angle = angle

            self.i = pygame.image.load("zombie.png")

            self.i2 = rot_center(self.i, self.angle)

            if self.x > self.px:
                self.x -= 2 
            if self.x < self.px:
                self.x += 2 

            if self.y > self.py:
                self.y -= 2 
            if self.y < self.py:
                self.y += 2 


        def render(self):

            screen.blit(self.i2, (self.x,self.y))

    x = leng/2
    y = wid/2

    player = pygame.image.load("player.png")

    x_change = 0
    y_change = 0

    spr = 50
    spr_change = 0

    u = 2
    spru = u*1.5
    zu = u

    cursor = pygame.image.load("cursor.gif") 
    mouseX = 0
    mouseY = 0
    mouse_click = 0
    mouse_rev = 0
    mm = 0
    mr = 0

    points = 0
    accuracy = 0
    shots = 0
    kills = 0
    health = 10

    boost = pygame.image.load("boost.png")
    boost_x = random.randint(0, leng-20)
    boost_y = random.randint(0, wid-20)

    gun = pygame.image.load("gun.png")
    bullet = pygame.image.load("bullet.png")
    bullet_x = random.randint(0, leng-20)
    bullet_y = random.randint(0, wid-20)
    ammo = 12

    zombie = pygame.image.load("zombie.png")
    zombie_x = random.randint(0,leng-30)
    zombie_y = random.randint(0,wid-30)
    z_angle = 0
    z_rect = 0
################################################################################
    apple = pygame.image.load("cubeberry.png")
    apple_x = random.randint(0, leng-20)
    apple_y = random.randint(0, wid-20)
##################################################################################    
    while True:

        mouseX, mouseY = pygame.mouse.get_pos()
        mouse_click, mm, mr = pygame.mouse.get_pressed()

        angle = math.degrees(math.atan2(mouseX-x, mouseY-y))+90
        z_angle = math.degrees(math.atan2(x-zombie_x, y-zombie_y))+90

        for event in pygame.event.get():
            if event.type == QUIT:
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    if event.key == pygame.K_SPACE and spr > 0:
                        u = spru
                        spr_change -= 1
                    x_change = -u
                    #y_change = 0
                    if x < 0:
                        x_change = u

                if event.key == pygame.K_d:
                    if event.key == pygame.K_SPACE and spr > 0:
                        u = spru
                        spr_change -= 1
                    x_change = u
                    #y_change = 0
                    if x > leng- size:
                        x_change = -u

                if event.key == pygame.K_w:
                    if event.key == pygame.K_SPACE and spr > 0:
                        u = spru
                        spr_change -= 1
                    y_change = -u
                    #x_change = 0
                    if y < 0:
                        y_change = u

                if event.key == pygame.K_s:
                    if event.key == pygame.K_SPACE and spr > 0:
                        u = spru
                        spr_change -= 1
                    y_change = u
                    #x_change = 0
                    if y > wid - size:
                        y_change = -u

                if event.key == pygame.K_SPACE and spr > 0:
                    u = spru
                    spr_change -= 1

                if event.key == pygame.K_a and event.key == pygame.K_w:
                    if x < 0 and y < 0:
                        x_change = u
                        y_change = u

                if event.key == pygame.K_a and event.key == pygame.K_s:
                    if x < 0 and y > wid - size:
                        x_change = u
                        y_change = -u

                if event.key == pygame.K_d and event.key == pygame.K_w:
                    if x > leng-size and y < 0:
                        x_change = -u
                        y_change = u

                if event.key == pygame.K_d and event.key == pygame.K_s:
                    if x > leng-size and y > wid-size:
                        x_change = -u
                        y_change = -u


            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a:
                    x_change = 0
                if event.key == pygame.K_d:
                    x_change = 0
                if event.key == pygame.K_w:
                    y_change = 0
                if event.key == pygame.K_s:
                    y_change = 0
                if event.key == pygame.K_SPACE:
                    u = spru/2
                    spr_change = 0
##########################################################################################
        if spr <= 0 :
            spr_change = 0

        if x < 0:
            x_change = u
            x += x_change
            x_change = 0

        if x > leng-size:
            x_change = -u
            x += x_change
            x_change = 0

        if y < 0:
            y_change = u
            y += y_change
            y_change = 0

        if y > wid-size:
            y_change = -u
            y += y_change
            y_change = 0

        x += x_change
        y += y_change
        spr += spr_change
#############################################################################
        if apple_x <= x <= apple_x + size and apple_y <= y <= apple_y + size:
            apple_x = random.randint(0, leng-20)
            apple_y = random.randint(0, wid-20)
            points += 100
            health += 2

        if x <= apple_x <= x + size and y <= apple_y <= y + size:
            apple_x = random.randint(0, leng-20)
            apple_y = random.randint(0, wid-20)
            points += 100
            health += 2
################################################################################
        if bullet_x <= x <= bullet_x + size and bullet_y <= y <= bullet_y + size:
            bullet_x = random.randint(0, leng-20)
            bullet_y = random.randint(0, wid-20)
            points += 50
            ammo += 4

        if x <= bullet_x <= x + size and y <= bullet_y <= y + size:
            bullet_x = random.randint(0, leng-20)
            bullet_y = random.randint(0, wid-20)
            points += 50
            ammo += 4
##################################################################################          
        if mouse_click == 1 and ammo >0 and mouse_rev == 0:
            ammo -= 1
            shots += 1
            mouse_rev = 1

        if mouse_click == 0:
            mouse_rev = 0

        if mouse_rev == 1 and zombie_x <= mouseX <= zombie_x + size and zombie_y <= mouseY <= zombie_y+ size and ammo >=0:
            zombie_x = random.randint(0, leng-20)
            zombie_y = random.randint(0, wid-20)
            mouse_rev = 1
            kills += 1
            points += 250

###################################################################################
        if boost_x <= x <= boost_x + size and boost_y <= y <= boost_y + size:
            boost_x = random.randint(0, leng-20)
            boost_y = random.randint(0, wid-20)
            points += 50
            spr += 20

        if x <= boost_x <= x + size and y <= boost_y <= y + size:
            boost_x = random.randint(0, leng-20)
            boost_y = random.randint(0, wid-20)
            points += 50
            spr += 20
################################################################################

        if x <= zombie_x <= x + size and y <= zombie_y <= y + size:
            points -= 5
            health -= 0.1

        if zombie_x <= x <= zombie_x + size and zombie_y <= y <= zombie_y + size:
            points -= 5
            health -= 0.1

        if not shots == 0:
            accuracy = 100 * float(kills)/float(shots)
        else:
            accuracy = 0
###################################################################################            
        #player = pygame.image.load("player.png")
        rect_player = rot_center(player, angle)
        gun = pygame.image.load("gun.png")
        gun = pygame.transform.rotate(gun, angle)
        #zombie = pygame.image.load("zombie.png")
        #z_rect = rot_center(zombie, z_angle)

        z1 = spawn(zombie_x, zombie_y,x,y,z_angle)
##################################################################################

        screen.blit(pygame.transform.scale(background,(leng,wid)),(0,0))
        #pygame.display.flip()
        screen.blit(apple, (apple_x,apple_y))
        screen.blit(rect_player, (x,y))
        screen.blit(gun, (x+10,y))
        screen.blit(boost, (boost_x,boost_y))
        #screen.blit(z_rect, (zombie_x,zombie_y))
        z1.render()
        screen.blit(bullet, (bullet_x,bullet_y))
        screen.blit(cursor, (mouseX -42.5, mouseY - 42.5))
        message("Points: "+str(points), red, [0,0])
        message("Ammo: " +str(ammo), black, [0,20])
        message("Sprint: " +str(spr), blue, [0,40])
        message("Accuracy: " +str(int(accuracy)), red, [0, 60])
        message("Health: " + str(int(health)), blue,[0,80])
        #pygame.display.update()
        pygame.display.flip()
        clock.tick(FPS)



gameLoop()

Tags: andkeyimageselfeventsizeifrandom
2条回答

您的代码很难阅读,您应该考虑以下几点:

在函数中划分代码

到目前为止,有一个函数包含将近300行代码。试着把代码分成只做一件事的小函数。你的前两个太好了!也许你的事件循环可以进入一个函数,渲染到另一个函数等等。你知道吗

小函数不仅易于阅读,而且当您需要重复与已编写的代码类似的代码时,它们非常有用。如果代码看起来很相似,只需将相同的放在函数体中,不同的放在函数的参数中。你知道吗

使用描述性名称

我看了你的代码一个多小时了,仍然不知道spr、spru、zu、mm、mr是什么意思,也不知道它们的用途。尝试使用更好地描述变量的名称,以便于其他人阅读您的代码。例如,lengthwidth是比lengwid更好的名字,因为它们说明了它们是什么。不要试图缩短已经很短的单词!你知道吗

另外,确保类的名称以大写字母开头。这有助于程序员识别什么是类,什么是函数。因此,将您的生成命名为生成。你知道吗

遵循conventions让其他人更容易理解您的代码并更好地帮助您。不仅是命名约定,还包括在操作符周围放置空格等

尝试使用类

类就像是变量的集合。这可能是非常有用的,而不是创建4-5个变量,分别相关,还可以重用代码!如果想创建更多的僵尸或苹果,可能需要类,这样可以提高可读性。你知道吗

回答你的问题

现在,您的代码不起作用,因为您的代码中没有任何名为move的方法或函数。编辑之前的错误是语法错误,因为您错误地定义了参数。要修复该类并使其与其余代码一起工作,需要对其他代码进行大量更改。老实说,我认为你的所有代码都需要重写,才能在游戏中做更多的事情。你知道吗

尝试学习更多关于函数、循环和类的知识。我很抱歉,但我相信你需要更多的知识,这些进一步的进展。你知道吗

你为什么不写这个?self.x,self.y = zombie_x,zombie_y

编辑:正如ninmokey所提到的,move不是类spawn的函数。把它放在课堂上,它就会起作用。你知道吗

相关问题 更多 >