Я переделываю игру Snake with pygame.
Как определить, находится ли квадрат за пределами экрана?Я использую x =
и y =
для перемещения квадрата.
Пока этот код:
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,500]
ax = 0
ay = 0
x = 0
y = 0
sizex = 500
sizey = 500
tilesize = 25
screen = pygame.display.set_mode((sizex,sizey))
pygame.display.set_caption('Snake')
pygame.display.set_icon(pygame.image.load('images/tile.png'))
tile = pygame.image.load('images/tile.png')
tile = pygame.transform.scale(tile, (tilesize, tilesize))
clock = pygame.time.Clock()
vel_x = 0
vel_y = 0
ap = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
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 == KEYDOWN:
if event.key == K_UP:
vel_y = -25
vel_x = 0
elif event.key == K_DOWN:
vel_y = 25
vel_x = 0
elif event.key == K_LEFT:
vel_x = - 25
vel_y = 0
elif event.key == K_RIGHT:
vel_x= 25
vel_y = 0
if ap:
pygame.draw.rect(screen, GREEN, pygame.Rect(ax,ay,tilesize,tilesize))
y += vel_y
x += vel_x
if x == ax and y == ay:
pygame.draw.rect(screen, GREEN, pygame.Rect(ax,ay,tilesize,tilesize))
ax = random.choice(ran)
ay = random.choice(ran)
pygame.draw.rect(screen, RED, pygame.Rect(x,y,tilesize,tilesize))
pygame.display.flip()
clock.tick(100)