pygame中的循环函数不生成游戏m中的角色

2024-04-24 23:59:30 发布

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

我正在做一个类似道奇的游戏。到目前为止,我创造了一个敌人和一个玩家。我把循环变成了一个函数,但是当我运行程序时,字符不会移动。在我把循环变成函数之前,字符确实在移动。
#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: selfeventifdefdisplayrenderchangepygame
1条回答
网友
1楼 · 发布于 2024-04-24 23:59:30

你的代码一团糟。在

每次在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后面和{}==+=^{{}>等等

#  - imports  -

import pygame
import sys

#  - constants - uppercase  -

HEIGHT = 500
WIDTH  = 1000

RED   = (255,  0,  0)
WHITE = (255,255,255)
BLACK = (  0,  0,  0)
BLUE  = (  0,  0,255)
GREEN = (  0,255,  0)

FPS = 30

BADDIE_THICKNESS = 40

DROPPER_Y = 30
DROPPER_X = 500
HERO_Y = 400

#  - classes - CamelCase   

class Baddie:
    '''enemy class''' # docstring
                      # empty line for readability
    def __init__(self, display, x=DROPPER_X, y=DROPPER_Y, color=GREEN):
        self.display = display
        self.rect = pygame.Rect(x, y, BADDIE_THICKNESS, BADDIE_THICKNESS)
        self.color = color
        #self.change = [0, 0]
                      # empty line for readability
    def render(self):
        pygame.draw.rect(self.display, self.color, self.rect)

    def update(self):
        self.rect.y += 1
        if self.rect.y > 500:
            self.rect.y = 30
                      # two empty lines for readability        

class Goodie:
    '''player class'''

    def __init__(self, display, x=0, y=HERO_Y, color=BLUE):
        self.display = display
        self.rect = pygame.Rect(x, y, 40, 40)
        self.color = color
        self.change = 0

    def render(self):
        pygame.draw.rect(self.display, self.color, self.rect)

    def update(self):
        self.rect.x += self.change

        # stay on screen
        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > WIDTH:
            self.rect.right = WIDTH

#  - functions - lowercase with _  -

def create_center_text(display, font, text):

    text = font.render(text, True, WHITE)

    rect = text.get_rect()
    rect.center = display.get_rect().center

    #rect = text.get_rect(center=display.get_rect().center)

    return text, rect

#                                   -
#  - main  -
#                                   -

#  - init  -

pygame.init()

display = pygame.display.set_mode((WIDTH,HEIGHT))

# created only once
good = Goodie(display)

# create only once
bads = []

for x in range(1, 11):
   bads.append(Baddie(display, x*90, x*30))

# created only once
font_large = pygame.font.Font(None, 100)

# created only once
game_over_text, game_over_rect = create_center_text(display, font_large, "GAME OVER")

#  - mainloop  -

# created only once
fps = pygame.time.Clock()

game_over = False
running = True

while running:

    #  - events  -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False # exit `gameloop`

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False # exit `gameloop`

            elif event.key == pygame.K_LEFT:
                good.change = -15
            elif event.key == pygame.K_RIGHT:
                good.change = +15

        elif event.type == pygame.KEYUP:
            if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                good.change = 0 # a += 0 is useless because it is a=a+0

    #  - updates  -

    if not game_over:

        # move good
        good.update()

        # move bads
        for bad in bads:
            bad.update()

        # check colision
        for bad in bads:
            if good.rect.colliderect(bad.rect):
                print 'oh no'
                game_over = True # exit `gameloop`

    #  - draws  -

    display.fill(BLACK)

    for bad in bads:
        bad.render()

    good.render()

    if game_over:
        display.blit(game_over_text, game_over_rect)

    pygame.display.update()

    #  - FPS  -

    fps.tick(FPS)

#  - end  -

pygame.quit()
sys.exit()

编辑:使用类

^{pr2}$

相关问题 更多 >