我正在做一个类似道奇的游戏。到目前为止,我创造了一个敌人和一个玩家。我把循环变成了一个函数,但是当我运行程序时,字符不会移动。在我把循环变成函数之前,字符确实在移动。
#this where I am initializing and defining my variables
import pygame
import sys
from pygame.locals import *
pygame.init()
display_height=500
display_width=1000
red=(255,0,0)
white=(255,255,255)
black=(0,0,0)
blue=(0,0,255)
green=(0,255,0)
v=500+100
FPS=30
dropper_y=30
dropper_x=500
hero_y=400
baddie_thickness=40
v_change=0
def dropper(amount, baddie_list):
for x in baddie_list:
dodger_x=500
dodger_y=30
fps=pygame.time.Clock()
set_Display=pygame.display.set_mode((display_width,display_height))
set_Display.fill(black)
hero=pygame.image.load('hero.png')
dropper=pygame.image.load('droppers1.png')
movement='null'
move_left='ahadun'
pixRate=8
#this is the enemy class
class baddie:
def __init__(self):
self.x=dropper_x
self.y=dropper_y
self.image=pygame.draw.rect(set_Display,green,(self.x,self.y,baddie_thickness,baddie_thickness))
def render(self):
self.rend=pygame.draw.rect(set_Display,green,(self.x,self.y,baddie_thickness,baddie_thickness))
#this is the player class
class goodie:
def __init__(self):
self.x=v
self.y=hero_y
self.image=pygame.draw.rect(set_Display,blue,(self.x,self.y,40,40))
def render(self):
self.rend=pygame.draw.rect(set_Display,blue,(self.x,self.y,40,40))
#these are text functions but have not been put to use
def text_objects(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText=pygame.font.Font('freesansansbold.ttf',100)
TextSurf,TextRect = text_objects(text,largeText)
TextRect.center= ((display_width/2,display_height/2))
set_Display.blit(TexttSurf, TextRect)
time.sleep(2)
pygame.display.update()
#this is a quit function
def end():
pygame.quit()
sys.exit()
#this the loop function
def gameloop():
while True:
good=goodie()
good.render()
bad=baddie()
bad.render()
dropper_y=30
fps=pygame.time.Clock()
movement='null'
move_left='ahadun'
pixRate=8
FPS=30
dropper_y=30
dropper_x=500
hero_y=400
baddie_thickness=40
v_change=0
#This is where the movement of the enemy is done
if movement=='null':
dropper_y+=pixRate
if dropper_y>500:
dropper_y=30
pixRate+=0.5
set_Display.fill(black)
bad.render()
good.render()
for event in pygame.event.get():
set_Display.fill(black)
good.render()
bad.render()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#This is where the player moves from key events
keys = pygame.key.get_pressed()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
v_change-=15
elif event.key == pygame.K_RIGHT:
v_change+=15
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
v_change+=0
elif event.key == pygame.K_LEFT:
v_change-=0
if movement=='null':
dropper_y+=1
if dropper_y>500:
dropper_y=30
#This is collision detection but does not work properly
if v>=dropper_x and v <= dropper_x+baddie_thickness:
if hero_y>=dropper_y and hero_y <= dropper_y+baddie_thickness:
print 'oh no'
end()
#end()
baddie_list=[]
baddie_list.append(dropper_x)
print (good.x)
good.x+=v_change
pygame.display.update()
fps.tick(FPS)
#I tried calling my enemy and player class here but it did not work
good=goodie()
bad=baddie()
good.render
good.x+=v_change
bad.render
gameloop()
pygame.quit()
quit()
Tags:
你的代码一团糟。在
每次在
while True
中设置dropper_y = 30
(甚至两次),hero_y = 40
等,这样对象就不能移动了。在编辑:
顺便说一下:
pygame.draw.rect
在屏幕上显示矩形。你不能把这个矩形赋给变量-它没有意义。最好使用self.rect = pygame.Rect(x, y, width, height)
来保持矩形的大小和位置,并在pygame.draw.rect(display, self.color, self.rect)
中使用它编辑:
如果所有游戏都使用类,则函数中的
mainloop
是可用的。在我的版本有排列好的代码但没有函数。我无法理解你代码中的一些行。在
有一个}}
PEP8
文档,其中有一些关于函数/类/变量名的建议,关于可读性的空行,comma
后面和{==
+=
^{{>
等等编辑:使用类
^{pr2}$相关问题 更多 >
编程相关推荐