我不知道在哪里调用函数。 Pygame

时间:2019-02-27 08:00:13

标签: python pygame

我正在重制蛇游戏。并且具有移动功能,不知道在哪里调用它。蛇是绿色的直肠

该函数位于while循环之前。 是要使蛇的其他部分移到最后一个部分的位置。

这是到目前为止的代码:

public long d = 0;
public long u = 0
private void getSpeed() {
    totalDownload += ((((TrafficStats.getMobileRxBytes() - d) / 1024) / 1024));
    totalUpload += ((((TrafficStats.getMobileTxBytes() - u) / 1024) / 1024));

    total += totalDownload + totalUpload;

    tvDownload.setText( totalDownload + "Mb" );
    tvUpload.setText( totalUpload + " Mb" );
    tvTotal.setText( total + "Mb" );
    Log.d( "getSpeed", "totalData: " + totalDownload );

    d = TrafficStats.getMobileRxBytes();
    u = TrafficStats.getMobileTxBytes();

}

我还不太确定如何使用函数,因为我仅学习python已有几周了。

1 个答案:

答案 0 :(得分:2)

添加绘制蛇的新功能:

def drawSnake():
    for p in snake_parts: 
        screen.blit(shake, p.topleft)

您当然可以绘制图像,而不是重新绘制图像。 snakepygame.Surface类型的图片:

def drawSnake():
    for p in snake_parts: 
        pygame.draw.rect(screen, RED, p)

将头添加到主循环之前(snake_parts之前)的列表while True:中:

snake_parts.append(pygame.Rect(x,y,tilesize,tilesize))
ax, ay = random.choice(ran), random.choice(ran)

使用drawSnake绘制蛇形并更新之前的零件位置:

slither( (x, y) )
if ap:
    pygame.draw.rect(screen, RED, pygame.Rect(ax,ay,tilesize,tilesize))
if x == ax and y == ay:
    pygame.draw.rect(screen, RED, pygame.Rect(ax,ay,tilesize,tilesize))
    ax, ay = random.choice(ran), random.choice(ran)
    sa += 1
    snake_parts.append(pygame.Rect(x, y, tilesize, tilesize))

drawSnake()
pygame.display.update()

slither中有一个错字。它必须是snake_parts[0].center而不是snake_parts[0].centre。 但是无论如何,在您的情况下,蛇的pars的起源是topleft,而不是center。更改功能slither

def slither( new_head_coord ):
    for i in range( len(snake_parts)-1, 0, -1 ):
        snake_parts[i] = snake_parts[i-1].copy()
    snake_parts[0].topleft = new_head_coord

在主循环中仅执行1个事件循环:

例如

run = True
while run:
    sx, sy = x, y
    for row in range(sizex):
        for column in range(sizey):
            screen.blit(tile,(column*tilesize, row*tilesize,tilesize,tilesize))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            run = False 
        if event.type == KEYDOWN:
            if event.key == K_UP:
                vel_y = -25
                vel_x = 0
            # [...]

演示

import pygame, sys, random
from pygame.locals import *

pygame.init()
movement_x = movement_y = 0
RED = (240, 0, 0)
GREEN = (0, 255, 0)
ran = [0,25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475]
sizex, sizey = 500, 500
tilesize = 25
screen = pygame.display.set_mode((sizex,sizey))
pygame.display.set_caption('Snake')
tile = pygame.Surface((tilesize, tilesize))
tile.fill((0, 0, 64))
tile = pygame.transform.scale(tile, (tilesize, tilesize))
x2 = 0
pag = 0
clock = pygame.time.Clock()
sx, sy = 0, 0
vel_x, vel_y = 0, 0
x, y = 0, 0
sa = 0
ap = True

snake_parts = []   # /N/ Pygame rects 

def slither( new_head_coord ):
    for i in range( len(snake_parts)-1, 0, -1 ):
        snake_parts[i] = snake_parts[i-1].copy()
    snake_parts[0].topleft = new_head_coord

def drawSnake():
    for p in snake_parts: 
        pygame.draw.rect(screen, GREEN, p)

snake_parts.append(pygame.Rect(x,y,tilesize,tilesize))
ax, ay = random.choice(ran), random.choice(ran)
run = True
while run:
    sx, sy = x, y
    for row in range(sizex):
        for column in range(sizey):
            screen.blit(tile,(column*tilesize, row*tilesize,tilesize,tilesize))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            run = False 
        if event.type == KEYDOWN:
            if event.key == K_UP:
                vel_x, vel_y = 0, -25
            elif event.key == K_DOWN:
                vel_x, vel_y = 0, 25
            elif event.key == K_LEFT:
                vel_x, vel_y = -25, 0
            elif event.key == K_RIGHT:
                vel_x, vel_y = 25, 0
            elif event.key == K_y:
                pag = 1
            elif event.key == K_n:
                pag = 2

    inBounds = pygame.Rect(0, 0, sizex, sizey).collidepoint(x+vel_x, y+vel_y)
    if inBounds:
        y += vel_y
        x += vel_x
    else:
        basicFont = pygame.font.SysFont(None, 48)
        text = basicFont.render('Game Over! Play again? y/n', True, GREEN, RED)
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery
        pygame.draw.rect(screen, RED, (textRect.left - 20, textRect.top - 20, textRect.width + 40, textRect.height + 40))
        screen.blit(text, textRect)
        ax, ay = -25, -25
        x, y = -25, -25
        if pag == 1:
            pag = 0
            inBounds = True
            x, y = 0, 0
            vel_x, vel_y = 0, 0
            ax, ay = random.choice(ran), random.choice(ran)
            pygame.draw.rect(screen, RED, pygame.Rect(ax,ay,tilesize,tilesize))
        if pag == 2:
            pygame.quit()
            sys.exit()

    slither( (x, y) )
    if ap:
        pygame.draw.rect(screen, RED, pygame.Rect(ax,ay,tilesize,tilesize))
    if x == ax and y == ay:
        pygame.draw.rect(screen, RED, pygame.Rect(ax,ay,tilesize,tilesize))
        ax, ay = random.choice(ran), random.choice(ran)
        sa += 1
        snake_parts.append(pygame.Rect(x, y, tilesize, tilesize))
    drawSnake()
    pygame.display.update()
    clock.tick(100)