我正试图在 pygame 中重现这款经典足球游戏,但它对我来说开始变得有点复杂。
如果您运行该程序,您会看到我已经绘制了所有内容,并且有一个主要的可移动玩家 (QB) 和其他还不能移动的静态坐着玩家。
我最终不得不将每个对方球员单独和随机地移向 QB,我开始通过建立函数来解决这个问题,但首先想弄清楚如何...
-
-
有一条线,你会在游戏中注意到,当你向线移动时,它会把 QB 变成白色,如果这有意义的话,我需要把它放在 QB 在他的座位上变成白色的地方。因此,当您朝直线方向按向右箭头时,他不会向前移动,但游戏会识别出他正在尝试并在原地被拦截。
我只是不明白如何让它做到这一点......尝试了一些没有奏效的不同方法。
您不必为我完成所有代码,只需给我一些指导即可。
import pygame, sys
# Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
field = (17,0,0)
green = (0,80,15)
blue = (0,0,215)
# Program Parameters
pygame.init()
pygame.display.set_caption('Classic Football')
size = [700,400]
display = pygame.display.set_mode(size)
clock = pygame.time.Clock()
# Movement Parameters
pygame.key.set_repeat(0)
x = 215
y = 223
step = 50
frame_count = 0
frame_rate = 60
second = 0
minute = 5
hour = 1
side = True
hike = False
time = "1 5. 0"
score1 = "0 0"
score2 = "0 0"
lineman1x1x = 265
lineman1x1y = 174
lineman1x2x = 265
lineman1x2y = 224
lineman1x3x = 265
lineman1x3y = 274
fieldman1x1x = 465
fieldman1x1y = 174
fieldman1x2x = 365
fieldman1x2y = 224
fieldman1x3x = 565
fieldman1x3y = 274
lineman2x1x = 415
lineman2x1y = 174
lineman2x2x = 415
lineman2x2y = 224
lineman2x3x = 415
lineman2x3y = 274
fieldman2x1x = 215
fieldman2x1y = 174
fieldman2x2x = 315
fieldman2x2y = 224
fieldman2x3x = 115
fieldman2x3y = 274
playerhit = False
# Functions
def player1():
pygame.draw.rect(display, red, ((x, y), (22, 6)), 0)
def player2():
pygame.draw.rect(display, red, ((x+250, y), (22, 6)), 0)
def lineman1x1():
pygame.draw.rect(display, red, ((lineman1x1x, lineman1x1y), (22, 4)), 0)
def lineman1x2():
pygame.draw.rect(display, red, ((lineman1x2x, lineman1x2y), (22, 4)), 0)
def lineman1x3():
pygame.draw.rect(display, red, ((lineman1x3x, lineman1x3y), (22, 4)), 0)
def fieldman1x1():
pygame.draw.rect(display, red, ((fieldman1x1x, fieldman1x1y), (22, 4)), 0)
def fieldman1x2():
pygame.draw.rect(display, red, ((fieldman1x2x, fieldman1x2y), (22, 4)), 0)
def fieldman1x3():
pygame.draw.rect(display, red, ((fieldman1x3x, fieldman1x3y), (22, 4)), 0)
def lineman2x1():
pygame.draw.rect(display, red, ((lineman2x1x, lineman2x1y), (22, 4)), 0)
def lineman2x2():
pygame.draw.rect(display, red, ((lineman2x2x, lineman2x2y), (22, 4)), 0)
def lineman2x3():
pygame.draw.rect(display, red, ((lineman2x3x, lineman2x3y), (22, 4)), 0)
def fieldman2x1():
pygame.draw.rect(display, red, ((fieldman2x1x, fieldman2x1y), (22, 4)), 0)
def fieldman2x2():
pygame.draw.rect(display, red, ((fieldman2x2x, fieldman2x2y), (22, 4)), 0)
def fieldman2x3():
pygame.draw.rect(display, red, ((fieldman2x3x, fieldman2x3y), (22, 4)), 0)
# Main Loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x -= step
hike = True
if event.key == pygame.K_RIGHT:
x += step
if event.key == pygame.K_UP:
y -= step
hike = True
if event.key == pygame.K_DOWN:
y += step
hike = True
# Movement Restrictions
if side == True:
if (x < 100): x = 115
elif (x > 565): x = 115
if (y < 150): y = 173
elif (y > 300): y = 273
if side == False:
if (x < (-150)): x = 315
elif (x > 315): x = 315
if (y < 150): y = 173
elif (y > 300): y = 273
# Game Graphics
display.fill(green)
# Clock
hour = int(time[0])
minute = int(time[2])
second = int(time[5])
if hike == True:
frame_count += 1
if second > 0 and frame_count == 64:
frame_count = 0
second -= 1
if second == 0 and minute > 0 and frame_count == 64:
frame_count = 0
second = 9
minute -= 1
if minute == 0 and hour > 0 and frame_count == 64:
frame_count = 0
minute = 9
second = 9
hour -= 1
time = str(hour) + " " + str(minute) + ". " + str(second)
# Counters
pygame.draw.rect(display, white, [150,25,400,100])
pygame.draw.rect(display, field, [150,50,134,50])
pygame.draw.rect(display, white, [150,50,134,50], 2)
pygame.draw.rect(display, field, [float(283.33333333333334),50,134,50])
pygame.draw.rect(display, white, [float(283.33333333333334),50,134,50], 2)
pygame.draw.rect(display, field, [float(416.33333333333334),50,134,50])
pygame.draw.rect(display, white, [float(416.33333333333334),50,133,50], 2)
font = pygame.font.SysFont('Calibri', 14, True, False)
text = font.render("DOWN",True,blue)
display.blit(text, [198, 33])
text = font.render("FIELD POSITION",True,blue)
display.blit(text, [305, 33])
text = font.render("YARDS TO GO",True,blue)
display.blit(text, [442, 33])
text = font.render("HOME",True,blue)
display.blit(text, [200, 105])
text = font.render("TIME REMAINING",True,blue)
display.blit(text, [300, 105])
text = font.render("VISITORS",True,blue)
display.blit(text, [456, 105])
font = pygame.font.SysFont('DS-Digital', 50, False, False)
text = font.render(score1,True,red)
display.blit(text, [188, 50])
text = font.render(time,True,red)
display.blit(text, [300, 50])
text = font.render(score2,True,red)
display.blit(text, [453, 50])
# Field Grid
pygame.draw.rect(display, field, [100,150,500,150])
for b in range(0,300,150):
pygame.draw.line(display, white, (100,150 + b), (600,150 + b), 2)
for a in range (0,550,50):
pygame.draw.line(display, white, (100 + a,150), (100 + a,300), 2)
for a in range (0,450,50):
pygame.draw.line(display, white, (140 + a,200), (160 + a,200), 2)
for a in range (0,450,50):
pygame.draw.line(display, white, (140 + a,250), (160 + a,250), 2)
for a in range (0,980,490):
pygame.draw.line(display, white, (100 + a,200), (110 + a,200), 2)
for a in range (0,980,490):
pygame.draw.line(display, white, (100 + a,250), (110 + a,250), 2)
# End Zones
pygame.draw.rect(display, blue, [80,150,21,151])
pygame.draw.rect(display, white, [80,150,21,151], 2)
pygame.draw.rect(display, blue, [600,150,20,151])
pygame.draw.rect(display, white, [600,150,20,151], 2)
# Goal Posts
pygame.draw.line(display, white, (50,200), (80,210), 2)
pygame.draw.line(display, white, (50,250), (80,240), 2)
pygame.draw.line(display, white, (70,208), (70,242), 2)
pygame.draw.line(display, white, (650,200), (620,210), 2)
pygame.draw.line(display, white, (650,250), (620,240), 2)
pygame.draw.line(display, white, (630,208), (630,243), 2)
# Players
if side == True:
player1()
lineman1x1()
lineman1x2()
lineman1x3()
fieldman1x1()
fieldman1x2()
fieldman1x3()
import random
move = random.randint(0, 9)
if lineman1x1x > x:
if move == 0 or 3 or 5 or 8:
lineman1x1x - step
elif move == 0 or 3 or 5 or 8:
lineman1x1x + step
if lineman1x1y > y:
if move == 1 or 4 or 7 or 9:
lineman1x1y - step
elif move == 1 or 4 or 7 or 9:
lineman1x1y + step
if x == lineman1x1x:
playerhit = True
if playerhit == True:
pygame.draw.rect(display, white, ((x, y), (22, 6)), 0)
if side == False:
player2()
lineman2x1()
lineman2x2()
lineman2x3()
fieldman2x1()
fieldman2x2()
fieldman2x3()
#FPS
pygame.display.update()
clock.tick(60)
我是一名优秀的程序员,十分优秀!