【发布时间】:2016-08-05 14:50:22
【问题描述】:
我正在使用 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()
【问题讨论】:
-
整个代码在哪里?
-
@QIstudio 见上文
-
确保问题中的代码是正确的。目前,您的代码存在多个语法错误,无法测试。
-
@Ted Klein Bergman 我编辑了它。
标签: python python-2.7 pygame