Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pygame中使用函数移动精灵_Python_Python 2.7_Pygame - Fatal编程技术网

Python 在pygame中使用函数移动精灵

Python 在pygame中使用函数移动精灵,python,python-2.7,pygame,Python,Python 2.7,Pygame,我使用spawn()类在敌人中繁殖,在spawn(0)类中,我尝试使用函数move()移动敌人。当我运行程序时,僵尸繁殖,但不移动 如果有人想知道的话,下面是完整的代码。我只是对spawn()类有问题,没有其他问题 import pygame import time import random import math from pygame.locals import * pygame.init() ###############################################

我使用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()
导入pygame
导入时间
随机输入
输入数学
从pygame.locals导入*
pygame.init()
#################################################################################
红色=(255,0,0)
绿色=(0255,0)
蓝色=(0,0255)
黑色=(0,0,0)
白色=(255255)
灰色=(125125)
#################################################################################
长度=1200
wid=600
FPS=60
尺寸=30
clock=pygame.time.clock()
font=pygame.font.SysFont('calibri',20)
def消息(消息、颜色、位置):
screen\u text=font.render(味精、真、彩色)
屏幕。blit(屏幕文本,loc)
def旋转中心(图像、角度):
orig_rect=image.get_rect()
rot_image=pygame.transform.rotate(图像,角度)
rot_rect=orig_rect.copy()
rot_rect.center=rot_image.get_rect().center
rot_image=rot_image.subground(rot_rect.copy())
返回rot_图像
def quit():
pygame.quit()
screen=pygame.display.set_模式((长,宽),0,32)
background=pygame.image.load(“background.png”)
################################################################################
def gameLoop():
类繁殖:
def uuu init uuuu(自、x、y、px、py、角度):
self.x=x
self.y=y
self.px=px
self.py=py
自我角度=角度
self.i=pygame.image.load(“zombie.png”)
self.i2=旋转中心(self.i,self.angle)
如果self.x>self.px:
self.x-=2
如果self.xself.py:
self.y-=2
如果self.y0:
u=浇口
spr_变化-=1
x_变化=-u
#y_变化=0
如果x<0:
x_变化=u
如果event.key==pygame.K\u d:
如果event.key==pygame.K_空间和spr>0:
u=浇口
spr_变化-=1
x_变化=u
#y_变化=0
如果x>长度-尺寸:
x_变化=-u
如果event.key==pygame.K_w:
如果event.key==pygame.K_空间和spr>0:
u=浇口
spr_变化-=1
y_change=-u
#x_变化=0
如果y<0:
改变=改变
如果event.key==pygame.K_:
如果event.key==pygame.K_空间和spr>0:
u=浇口
spr_变化-=1
改变=改变
#x_变化=0
如果y>wid-大小:
y_change=-u
如果event.key==pygame.K_空间和spr>0:
u=浇口
spr_变化-=1
如果event.key==pygame.K_a和event.key==pygame.K_w:
如果x<0且y<0:
x_变化=u
改变=改变
如果event.key==pygame.K_a和event.key==pygame.K_s:
如果x<0且y>wid-尺寸:
x_变化=u
y_change=-u
如果event.key==pygame.K_d和event.key==pygame.K_w:
如果x>长度尺寸且y<0:
x_变化=-u
改变=改变
如果event.key==pygame.K_d和event.key==pygame.K_s:
如果x>长度尺寸,y>宽度尺寸:
x_变化=-u
y_change=-u
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_a:
x_变化=0
如果event.key==pygame.K\u d:
x_ch