i would like when you cross their line the game closes and when the enemy hits your line the game restarts, any ideas on how to code that in? you don't have to rewrite the code just help me with some ideas on how i could add it in
here is the current code
import random
import pygame, sys
from pygame.locals import *
import time
GREEN=(0,125,0)
BLUE=(0,0,125)
Yplayer = 225/2
Xplayer = 350
playerlinesavedata = []
playercurrent = []
Xplayerchange=0
Yplayerchange=0
Yenemy = 450
Xenemy = 300
enemylinesavedata = []
enemycurrent = []
Xenemychange=0
Yenemychange=0
windowX = 900
windowY = 600
pygame.init()
DISPLAY=pygame.display.set_mode((windowX,windowY),0,32)
def enemymovement():
global Xplayer,Yplayer,Xenemy,Yenemy
enemy1 = True
enemydirection = random.randint(1,4)
# enemy moves toward player
if Xenemy > Xplayer: enemydirection = 1
elif Xenemy < Xplayer: enemydirection = 2
elif Yenemy > Yplayer: enemydirection = 3
elif Yenemy < Yplayer: enemydirection = 4
while enemy1 == True:
if enemydirection == 1:
Xenemychange= -random.randint(1,2)
Xenemy+=Xenemychange
if enemydirection == 2:
Xenemychange= random.randint(1,2)
Xenemy+=Xenemychange
if enemydirection == 3:
Yenemychange= -random.randint(1,2)
Yenemy+=Yenemychange
if enemydirection == 4:
Yenemychange= random.randint(1,2)
Yenemy+=Yenemychange
enemylinesavedata.append([Xenemy,Yenemy])
enemy()
break
def enemy():
for XnY in enemylinesavedata:
pygame.draw.rect(DISPLAY,BLUE, [XnY[0],XnY[1],5,5])
def player():
for XnY in playerlinesavedata:
pygame.draw.rect(DISPLAY,GREEN,[XnY[0],XnY[1],5,5])
def main():
global Xplayer,Yplayer,Xenemy,Yenemy,Xplayerchange,Yplayerchange
WHITE=(255,255,255)
BLUE=(0,0,255)
DISPLAY.fill(WHITE)
pygame.display.update()
while True:
Yplayer+=Yplayerchange
Xplayer+=Xplayerchange
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_d:
Xplayerchange+=1
if event.key==K_a:
Xplayerchange-=1
if event.key==K_w:
Yplayerchange-=1
if event.key==K_s:
Yplayerchange+=1
if event.type==KEYUP:
if event.key==K_a or K_d:
Yplayerchange=0
if event.key==K_w or K_s:
Xplayerchange=0
playercurrent = []
playercurrent.append(Xplayer)
playercurrent.append(Yplayer)
playerlinesavedata.append(playercurrent)
Yplayer+=Yplayerchange
Xplayer+=Xplayerchange
enemymovement()
Yplayer+=Yplayerchange
Xplayer+=Xplayerchange
DISPLAY.fill(WHITE)
player()
enemy()
pygame.display.update()
main()
i am putting this text down here so i can post this question. if your still reading this them im sorry for the amount of code but i really want to get a game like this working
Aucun commentaire:
Enregistrer un commentaire