Help with python py.game

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 7
Reputation: AnonSurf is an unknown quantity at this point 
Solved Threads: 0
AnonSurf AnonSurf is offline Offline
Newbie Poster

Help with python py.game

 
0
  #1
Oct 20th, 2009
Having trouble getting code to allow the cars to go off screen then come back on the opposite side of the screen! for top to bottom and side to side!
-The code i was trying was
  1. if self.position > rect.top
  2. self.position == rect.bottom
not sure if this is gonna work im new soo need some help thanks!!

-Heres the game code
  1. # Initialization
  2. import pygame, math, sys, random
  3. from pygame.locals import *
  4. screen = pygame.display.set_mode((1020,680))
  5. clock = pygame.time.Clock()
  6.  
  7. class CarSprite(pygame.sprite.Sprite):
  8. MAX_FORWARD_SPEED = 10
  9. MAX_REVERSE_SPEED = 10
  10. ACCELERATION = 2
  11. TURN_SPEED = 5
  12.  
  13. def __init__(self, image, position):
  14. pygame.sprite.Sprite.__init__(self)
  15. self.src_image = pygame.image.load(image)
  16. self.position = position
  17. self.speed = self.direction = 0
  18. self.k_left = self.k_right = self.k_down = self.k_up = 0
  19.  
  20. def update(self, deltat):
  21. # SIMULATION
  22. self.speed += (self.k_up + self.k_down)
  23. if self.speed > self.MAX_FORWARD_SPEED:
  24. self.speed = self.MAX_FORWARD_SPEED
  25. if self.speed < -self.MAX_REVERSE_SPEED:
  26. self.speed = -self.MAX_REVERSE_SPEED
  27. self.direction += (self.k_right + self.k_left)
  28. x, y = self.position
  29. rad = self.direction * math.pi / 180
  30. x += -self.speed*math.sin(rad)
  31. y += -self.speed*math.cos(rad)
  32. self.position = (x, y)
  33. self.image = pygame.transform.rotate(self.src_image, self.direction)
  34. self.rect = self.image.get_rect()
  35. self.rect.center = self.position
  36.  
  37. class PadSprite(pygame.sprite.Sprite):
  38. normal = pygame.image.load('bumper.png')
  39. hit = pygame.image.load('bumperHit.png')
  40. def __init__(self, number, position):
  41. pygame.sprite.Sprite.__init__(self)
  42. self.number = number
  43. self.rect = pygame.Rect(self.normal.get_rect())
  44. self.rect.center = position
  45. self.image = self.normal
  46.  
  47. # CREATE A CAR AND RUN
  48.  
  49. rect = screen.get_rect()
  50. background = pygame.image.load('ground.png')
  51. screen.blit(background, (0,0))
  52. car = CarSprite('car.png', rect.center)
  53. car_group = pygame.sprite.RenderPlain(car)
  54. car2 = CarSprite('car2.png', rect.center)
  55. car2_group = pygame.sprite.RenderPlain(car2)
  56. pads = [
  57. PadSprite(1, (random.randint(5,999), random.randint(10,675))),#add numbers back in
  58. PadSprite(2, (random.randint(5,999), random.randint(10,675))),
  59. PadSprite(3, (random.randint(5,999), random.randint(10,675))),
  60. ]
  61. pads2 = [
  62. PadSprite(1, (random.randint(5,999), random.randint(10,675))),
  63. PadSprite(2, (random.randint(5,999), random.randint(10,675))),
  64. PadSprite(3, (random.randint(5,999), random.randint(10,675))),
  65. ]
  66. pads3 = [
  67. PadSprite(1, (random.randint(5,999), random.randint(10,675))),
  68. PadSprite(2, (random.randint(5,999), random.randint(10,675))),
  69. PadSprite(3, (random.randint(5,999), random.randint(10,675))),
  70. ]
  71. current_pad_number = 0
  72. current_pad2_number = 0
  73. current_pad3_number = 0
  74. pad_group = pygame.sprite.RenderPlain(*pads)
  75. pad2_group = pygame.sprite.RenderPlain(*pads2)
  76. pad3_group = pygame.sprite.RenderPlain(*pads3)
  77. screen.blit(background, (0,0))
  78. screen.blit(background, (0,0))
  79. screen.blit(background, (0,0))
  80.  
  81.  
  82. while 1:
  83. # USER INPUT
  84. deltat = clock.tick(30)
  85. for event in pygame.event.get():
  86. if not hasattr(event, 'key'): continue
  87. down = event.type == KEYDOWN
  88. if event.key == K_RIGHT: car.k_right = down * -5
  89. elif event.key == K_LEFT: car.k_left = down * 5
  90. elif event.key == K_UP: car.k_up = down * 2
  91. elif event.key == K_DOWN: car.k_down = down * -2
  92. elif event.key == K_SPACE: car.position = (550,300)
  93. elif event.key == K_d: car2.k_right = down * -5
  94. elif event.key == K_a: car2.k_left = down * 5
  95. elif event.key == K_w: car2.k_up = down *2
  96. elif event.key == K_s: car2.k_down = down * -2
  97. elif event.key == K_b: car2.position = (550,300)
  98. elif event.key == K_ESCAPE: sys.exit(0)
  99.  
  100. # RENDERING
  101. #screen.fill((0,0,0))
  102. pad_group.clear(screen, background)
  103. pad2_group.clear(screen, background)
  104. pad3_group.clear(screen, background)
  105. car_group.clear(screen,background)
  106. car2_group.clear(screen,background)
  107. car2_group.update(deltat)
  108. car_group.update(deltat)
  109. collisions = pygame.sprite.spritecollide(car, pad_group, False)
  110. if collisions:
  111. pad = collisions[0]
  112. if pad.number == current_pad_number + 1:
  113. pad.image = pad.hit
  114. current_pad_number += 1
  115. elif current_pad_number ==3:
  116. for pad in pad_group: pad.image = pad.normal
  117. current_pad_number = 0
  118. collisions2 = pygame.sprite.spritecollide(car2, pad2_group, False)
  119. if collisions2:
  120. pad2 = collisions2[0]
  121. if pad2.number == current_pad2_number + 1:
  122. pad2.image = pad2.hit
  123. current_pad2_number += 1
  124. elif current_pad2_number ==3:
  125. for pad2 in pad2_group: pad2.image = pad.normal
  126. current_pad2_number = 0
  127. #collisions3 = pygame.sprite.spritecollide(car_group, pad3_group, False)
  128. #if collisions3:
  129. # pad3 = collisions3[0]
  130. # if pad2.number == current_pad2_number + 1:
  131. # pad2.image = pad2.hit
  132. # current_pad2_number += 1
  133. #elif current_pad2_number ==3:
  134. # for pad2 in pad2_group: pad2.image = pad.normal
  135. # current_pad2_number = 0
  136.  
  137. pad_group.draw(screen)
  138. pad2_group.draw(screen)
  139. pad3_group.draw(screen)
  140. car_group.draw(screen)
  141. car2_group.draw(screen)
  142. pygame.display.flip()
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 909
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345
 
0
  #2
Oct 20th, 2009
For moving something you have to do something like
  1. self.rect = self.rect.move(self.whereyouwantittogo)
As you can see that is just an example, its not using any of the proper variables that are seen in your program. But im sure you get the gist
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 48
Reputation: fallopiano is an unknown quantity at this point 
Solved Threads: 4
fallopiano's Avatar
fallopiano fallopiano is offline Offline
Light Poster
 
0
  #3
Oct 20th, 2009
try:

  1. if car_pos.x >= the_screens_length:
  2. car_pos.x = 0
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: AnonSurf is an unknown quantity at this point 
Solved Threads: 0
AnonSurf AnonSurf is offline Offline
Newbie Poster
 
0
  #4
Oct 21st, 2009
I just need the "PacMan" effect like if you go off the screen it comes back on the opposite side
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: AnonSurf is an unknown quantity at this point 
Solved Threads: 0
AnonSurf AnonSurf is offline Offline
Newbie Poster
 
0
  #5
Oct 21st, 2009
i tried both codes but nothing is working still
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 229
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 57
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #6
Oct 21st, 2009
hi
I don't know if this will help you but I did something similar to what you want for a Snake game and I used something like the following piece of code:

  1. from Tkinter import *
  2. import time
  3.  
  4. root = Tk()
  5. c = Canvas( root, width = 400, height = 200 ); c.pack()
  6.  
  7. x, y = 150, 100
  8. xInc = 5; yInc = 0
  9. c.create_text( 200, 80, text = "control the ball using arrow keys", font = "Arial" )
  10.  
  11. def keyboard( event ):
  12. global x, y, xInc, yInc
  13. key = event.keysym
  14. if key == "Right":
  15. xInc = 5; yInc = 0
  16. if key == "Left":
  17. xInc = -5; yInc = 0
  18. if key == "Up":
  19. yInc = -5; xInc = 0
  20. if key == "Down":
  21. yInc = 5; xInc = 0
  22.  
  23. def main():
  24. global x, y, xInc, yInc
  25. ball = c.create_oval( x, y, x + 10, y + 10, fill = 'black' )
  26.  
  27. while True:
  28. time.sleep( 0.05 )
  29. x += xInc; y += yInc
  30. c.delete( ball )
  31. ball = c.create_oval( x, y, x + 10, y + 10, fill = 'black' )
  32. if x >= 400:
  33. x = 0
  34. elif x <= 0:
  35. x = 400
  36. if y <= 0:
  37. y = 200
  38. elif y >= 200:
  39. y = 0
  40. c.update()
  41.  
  42. root.bind( "<Any-KeyPress>", keyboard )
  43. main()
  44. mainloop()

P.S. I'm not using pygame, so don't know the drawing stuff there at all but I guess it'll be something similar. Hope this helps
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: AnonSurf is an unknown quantity at this point 
Solved Threads: 0
AnonSurf AnonSurf is offline Offline
Newbie Poster
 
0
  #7
Oct 22nd, 2009
still havent got it working
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: AnonSurf is an unknown quantity at this point 
Solved Threads: 0
AnonSurf AnonSurf is offline Offline
Newbie Poster
 
0
  #8
Oct 22nd, 2009
Got it working
put
  1. if x >= 1020:
  2. x = 0
  3. elif x <= 0:
  4. x = 1020
  5. if y >= 680:
  6. y = 0
  7. elif y <= 0:
  8. y = 680
in the car update method
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC